mirror of
https://github.com/payloadcms/payload.git
synced 2026-09-14 20:07:19 +08:00
ca6f849b53
Exposes a new argument to authentication strategies which allows the author to determine if this auth strategy has the capability of setting response headers or not. This is useful because some auth strategies may want to set headers, but in Next.js server components (AKA the admin panel), it's not possible to set headers. It is, however, possible to set headers within API responses and similar contexts. So, an author might decide to only run operations that require setting headers (i.e. refreshing an access token) if the auth strategy is being executed in contexts where setting headers is possible.
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import type { AuthStrategyFunctionArgs, AuthStrategyResult } from './index.js'
|
|
|
|
import { logError } from '../utilities/logError.js'
|
|
import { mergeHeaders } from '../utilities/mergeHeaders.js'
|
|
export const executeAuthStrategies = async (
|
|
args: AuthStrategyFunctionArgs,
|
|
): Promise<AuthStrategyResult> => {
|
|
let result: AuthStrategyResult = { user: null }
|
|
|
|
if (!args.payload.authStrategies?.length) {
|
|
return result
|
|
}
|
|
|
|
for (const strategy of args.payload.authStrategies) {
|
|
// add the configured AuthStrategy `name` to the strategy function args
|
|
args.strategyName = strategy.name
|
|
args.isGraphQL = Boolean(args.isGraphQL)
|
|
args.canSetHeaders = Boolean(args.canSetHeaders)
|
|
|
|
try {
|
|
const authResult = await strategy.authenticate(args)
|
|
if (authResult.responseHeaders) {
|
|
authResult.responseHeaders = mergeHeaders(
|
|
result.responseHeaders || new Headers(),
|
|
authResult.responseHeaders || new Headers(),
|
|
)
|
|
}
|
|
result = authResult
|
|
} catch (err) {
|
|
logError({ err, payload: args.payload })
|
|
}
|
|
|
|
if (result.user) {
|
|
return result
|
|
}
|
|
}
|
|
return result
|
|
}
|