chore: improve auth function
This commit is contained in:
@@ -1,36 +1,30 @@
|
||||
import { getPayload } from 'payload'
|
||||
import type { I18n } from '@payloadcms/translations'
|
||||
import { getAuthenticatedUser, getAccessResults } from 'payload/auth'
|
||||
import { SanitizedConfig } from 'payload/types'
|
||||
import { PayloadRequest } from 'payload/types'
|
||||
import { cache } from 'react'
|
||||
|
||||
export const auth = cache(
|
||||
async ({
|
||||
headers,
|
||||
config,
|
||||
|
||||
cookies,
|
||||
i18n,
|
||||
partialReq,
|
||||
}: {
|
||||
headers: any
|
||||
config: SanitizedConfig | Promise<SanitizedConfig>
|
||||
cookies: Map<string, string>
|
||||
i18n: I18n
|
||||
partialReq: Partial<PayloadRequest>
|
||||
}) => {
|
||||
const payload = await getPayload({ config })
|
||||
|
||||
const user = await getAuthenticatedUser({
|
||||
headers,
|
||||
payload,
|
||||
payload: partialReq.payload,
|
||||
cookies,
|
||||
})
|
||||
partialReq.user = user
|
||||
|
||||
const permissions = await getAccessResults({
|
||||
req: {
|
||||
user,
|
||||
payload,
|
||||
context: {},
|
||||
payloadAPI: 'REST',
|
||||
headers,
|
||||
i18n: undefined,
|
||||
t: undefined,
|
||||
},
|
||||
req: partialReq as PayloadRequest,
|
||||
})
|
||||
|
||||
return {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { auth } from './auth'
|
||||
|
||||
import { getPayload } from 'payload'
|
||||
import type {
|
||||
PayloadRequest,
|
||||
SanitizedCollectionConfig,
|
||||
SanitizedConfig,
|
||||
SanitizedGlobalConfig,
|
||||
@@ -45,19 +46,43 @@ export const initPage = async ({
|
||||
}> => {
|
||||
const headers = getHeaders()
|
||||
const cookies = parseCookies(headers)
|
||||
const language = getRequestLanguage({ cookies, headers })
|
||||
const config = await configPromise
|
||||
const payload = await getPayload({ config })
|
||||
|
||||
const { localization, routes, collections, globals } = config
|
||||
|
||||
const i18n = await initI18n({
|
||||
config: config.i18n,
|
||||
language,
|
||||
translations,
|
||||
context: 'client',
|
||||
})
|
||||
|
||||
const defaultLocale =
|
||||
localization && localization.defaultLocale ? localization.defaultLocale : 'en'
|
||||
const localeCode = localeParam || defaultLocale
|
||||
const locale = localization && findLocaleFromCode(localization, localeCode)
|
||||
|
||||
const partialReq: Partial<PayloadRequest> = {
|
||||
user: null,
|
||||
payload,
|
||||
payloadAPI: 'REST',
|
||||
headers,
|
||||
i18n,
|
||||
t: i18n.t,
|
||||
locale: locale ? locale?.code : undefined,
|
||||
fallbackLocale: locale ? locale?.fallbackLocale : undefined,
|
||||
context: {},
|
||||
}
|
||||
|
||||
const { permissions, user } = await auth({
|
||||
headers,
|
||||
config: configPromise,
|
||||
cookies,
|
||||
i18n,
|
||||
partialReq,
|
||||
})
|
||||
|
||||
const language = getRequestLanguage({ cookies, headers })
|
||||
|
||||
const config = await configPromise
|
||||
|
||||
const { localization, routes, collections, globals } = config
|
||||
|
||||
if (redirectUnauthenticatedUser && !user && route !== '/login') {
|
||||
const stringifiedSearchParams = Object.keys(searchParams ?? {}).length
|
||||
? `?${qs.stringify(searchParams)}`
|
||||
@@ -66,16 +91,6 @@ export const initPage = async ({
|
||||
redirect(`${routes.admin}/login?redirect=${routes.admin + route + stringifiedSearchParams}`)
|
||||
}
|
||||
|
||||
const payload = await getPayload({
|
||||
config,
|
||||
})
|
||||
|
||||
const i18n = await initI18n({
|
||||
config: config.i18n,
|
||||
language,
|
||||
translations,
|
||||
context: 'all',
|
||||
})
|
||||
let collectionConfig: SanitizedCollectionConfig
|
||||
let globalConfig: SanitizedGlobalConfig
|
||||
|
||||
@@ -87,13 +102,6 @@ export const initPage = async ({
|
||||
globalConfig = globals.find((global) => global.slug === globalSlug)
|
||||
}
|
||||
|
||||
const defaultLocale =
|
||||
localization && localization.defaultLocale ? localization.defaultLocale : 'en'
|
||||
|
||||
const localeCode = localeParam || defaultLocale
|
||||
|
||||
const locale = localization && findLocaleFromCode(localization, localeCode)
|
||||
|
||||
return {
|
||||
payload,
|
||||
permissions,
|
||||
|
||||
@@ -32,5 +32,6 @@
|
||||
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.d.ts", "src/**/*.json"],
|
||||
"ts-node": {
|
||||
"swc": true
|
||||
}
|
||||
},
|
||||
"references": [{ "path": "../translations" }]
|
||||
}
|
||||
|
||||
@@ -47,5 +47,5 @@ export type InitI18n = (args: {
|
||||
config: I18nOptions
|
||||
language?: string
|
||||
translations: Translations
|
||||
context: 'all' | 'api' | 'client'
|
||||
context: 'api' | 'client'
|
||||
}) => Promise<I18n>
|
||||
|
||||
Reference in New Issue
Block a user