@@ -6,6 +6,7 @@ import type {
66 AuthenticationMaxRetriesOptions ,
77 AuthenticationOptions ,
88} from "../types.js" ;
9+ import { INVALID_AUTH_CONFIG_ERROR , WrongArgumentError } from "../errors.js" ;
910
1011const getLoginPath = ( admin : AdminJS ) : string => {
1112 const { loginPath, rootPath } = admin . options ;
@@ -100,23 +101,37 @@ export const withLogin = (
100101 const context : AuthenticationContext = { req, res } ;
101102
102103 let adminUser ;
103- if ( provider ) {
104- adminUser = await provider . handleLogin (
105- {
106- headers : req . headers ,
107- query : req . query ,
108- params : req . params ,
109- data : req . fields ?? { } ,
110- } ,
111- context
112- ) ;
113- } else {
114- const { email, password } = req . fields as {
115- email : string ;
116- password : string ;
117- } ;
118- // "auth.authenticate" must always be defined if "auth.provider" isn't
119- adminUser = await auth . authenticate ! ( email , password , context ) ;
104+ try {
105+ if ( provider ) {
106+ adminUser = await provider . handleLogin (
107+ {
108+ headers : req . headers ,
109+ query : req . query ,
110+ params : req . params ,
111+ data : req . fields ?? { } ,
112+ } ,
113+ context
114+ ) ;
115+ } else if ( auth . authenticate ) {
116+ const { email, password } = req . fields as {
117+ email : string ;
118+ password : string ;
119+ } ;
120+ // "auth.authenticate" must always be defined if "auth.provider" isn't
121+ adminUser = await auth . authenticate ( email , password , context ) ;
122+ } else {
123+ throw new WrongArgumentError ( INVALID_AUTH_CONFIG_ERROR ) ;
124+ }
125+ } catch ( error ) {
126+ const errorMessage = error . message || error . error || "invalidCredentials" ;
127+
128+ const loginPage = await admin . renderLogin ( {
129+ action : admin . options . loginPath ,
130+ errorMessage,
131+ ...providerProps ,
132+ } ) ;
133+
134+ return res . status ( 400 ) . send ( loginPage ) ;
120135 }
121136
122137 if ( adminUser ) {
0 commit comments