fix: safe auth strategy execution (#11515)
Previously when `authenticate` method from an authentication strategy failed it stopped execution of the current request in `createPayloadRequest` which isn't a good behavior. Right now it completely prevents the admin panel from loading: <img width="637" alt="image" src="https://github.com/user-attachments/assets/7a6ca006-7457-4f9f-8746-7b3f52d65583" /> Now, each `strategy.authenticate` call is wrapped into `try` / `catch`, if an error happens we use `logError` to correctly log that error by its logging level.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
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,
|
||||
@@ -14,14 +15,18 @@ export const executeAuthStrategies = async (
|
||||
// add the configured AuthStrategy `name` to the strategy function args
|
||||
args.strategyName = strategy.name
|
||||
|
||||
const authResult = await strategy.authenticate(args)
|
||||
if (authResult.responseHeaders) {
|
||||
authResult.responseHeaders = mergeHeaders(
|
||||
result.responseHeaders || new Headers(),
|
||||
authResult.responseHeaders || new Headers(),
|
||||
)
|
||||
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 })
|
||||
}
|
||||
result = authResult
|
||||
|
||||
if (result.user) {
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user