Skip to content

Commit 35f28ff

Browse files
fix(auth): handle GET OAuth callback with undefined req.body on Express 5 (#3492) (#3496)
Closes #3492
1 parent bcfe797 commit 35f28ff

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

modules/auth/controllers/auth.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ const checkOAuthUserProfile = async (profil, key, provider) => {
342342
const oauthCallback = async (req, res, next) => {
343343
const strategy = req.params.strategy;
344344
// app Auth with Strategy managed on client side
345-
if (req.body.strategy === false && req.body.key) {
345+
if (req.body?.strategy === false && req.body?.key) {
346346
const allowedKeys = ['id', 'sub', 'email'];
347347
if (!allowedKeys.includes(req.body.key)) {
348348
return responses.error(res, 422, 'Unprocessable Entity', 'Invalid provider key')();

modules/auth/tests/auth.integration.tests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,25 @@ describe('Auth integration tests:', () => {
626626
authenticateSpy.mockRestore();
627627
});
628628

629+
test('should handle GET callback when req.body is undefined (Express 5)', async () => {
630+
const authenticateSpy = jest.spyOn(passport, 'authenticate').mockImplementationOnce(
631+
(strategy, callback) => () => callback(null, { id: 'mock-get-cb-user' }),
632+
);
633+
const cookies = {};
634+
const redirectCalls = [];
635+
const mockReq = { params: { strategy: 'google' } };
636+
const mockRes = {
637+
cookie(name, val, opts) { cookies[name] = { val, opts }; return this; },
638+
redirect(code, url) { redirectCalls.push({ code, url }); },
639+
};
640+
641+
await AuthController.oauthCallback(mockReq, mockRes, () => {});
642+
643+
expect(cookies.TOKEN).toBeDefined();
644+
expect(redirectCalls[0]).toMatchObject({ code: 302 });
645+
authenticateSpy.mockRestore();
646+
});
647+
629648
test('should find an existing OAuth user via checkOAuthUserProfile', async () => {
630649
// Create an OAuth user directly first
631650
const createdUser = await UserService.create({

0 commit comments

Comments
 (0)