Skip to content

Commit fde4d87

Browse files
committed
fix: return errors to the frontend if thrown in auth provider
1 parent ef9c1ba commit fde4d87

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

src/authentication/login.handler.ts

+32-17
Original file line numberDiff line numberDiff line change
@@ -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

1011
const 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) {

src/authentication/logout.handler.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ export const withLogout = (
2222

2323
router.get(logoutPath, async (request, response) => {
2424
if (provider) {
25-
await provider.handleLogout({ req: request, res: response });
25+
try {
26+
await provider.handleLogout({ req: request, res: response });
27+
} catch (error) {
28+
console.error(error); // fail silently and still logout
29+
}
2630
}
2731

2832
request.session.destroy(() => {

src/buildAuthenticatedRouter.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { withLogin } from "./authentication/login.handler.js";
77
import { withLogout } from "./authentication/logout.handler.js";
88
import { withProtectedRoutesHandler } from "./authentication/protected-routes.handler.js";
99
import { buildAssets, buildRoutes, initializeAdmin } from "./buildRouter.js";
10-
import { OldBodyParserUsedError, WrongArgumentError } from "./errors.js";
10+
import {
11+
INVALID_AUTH_CONFIG_ERROR,
12+
MISSING_AUTH_CONFIG_ERROR,
13+
OldBodyParserUsedError,
14+
WrongArgumentError,
15+
} from "./errors.js";
1116
import { AuthenticationOptions, FormidableOptions } from "./types.js";
1217
import { withRefresh } from "./authentication/refresh.handler.js";
1318

14-
const MISSING_AUTH_CONFIG_ERROR =
15-
'You must configure either "authenticate" method or assign an auth "provider"';
16-
const INVALID_AUTH_CONFIG_ERROR =
17-
'You cannot configure both "authenticate" and "provider". "authenticate" will be removed in next major release.';
18-
1919
/**
2020
* @typedef {Function} Authenticate
2121
* @memberof module:@adminjs/express

src/errors.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
export const MISSING_AUTH_CONFIG_ERROR =
2+
'You must configure either "authenticate" method or assign an auth "provider"';
3+
export const INVALID_AUTH_CONFIG_ERROR =
4+
'You cannot configure both "authenticate" and "provider". "authenticate" will be removed in next major release.';
5+
16
export class WrongArgumentError extends Error {
27
constructor(message: string) {
38
super(message);

0 commit comments

Comments
 (0)