Skip to content

Commit c69c2fb

Browse files
committed
fix: replace session regen with logout
Tests were failing due to a race condition when regenerating the session, fixed altogether by logging out instead
1 parent cf5ef99 commit c69c2fb

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/service/routes/auth.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ router.post('/logout', (req: Request, res: Response, next: NextFunction) => {
178178
res.send({ isAuth: req.isAuthenticated(), user: req.user });
179179
});
180180

181-
router.post('/change-password', async (req: Request, res: Response) => {
181+
router.post('/change-password', async (req: Request, res: Response, next: NextFunction) => {
182182
if (!req.user) {
183183
res
184184
.status(401)
@@ -243,16 +243,11 @@ router.post('/change-password', async (req: Request, res: Response) => {
243243
mustChangePassword: false,
244244
});
245245

246-
await new Promise<void>((resolve, reject) => {
247-
req.session?.regenerate((err: unknown) => {
248-
if (err) {
249-
reject(err);
250-
} else {
251-
resolve();
252-
}
253-
});
246+
// Logout is added by passport and not present in test environment
247+
req.logout?.((err: unknown) => {
248+
if (err) return next(err);
254249
});
255-
250+
res.clearCookie('connect.sid');
256251
(req.user as User).mustChangePassword = false;
257252

258253
res.status(200).send({ message: 'Password updated successfully' }).end();

test/services/routes/auth.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ describe('Auth API', () => {
298298
newPassword: 'new-password-123',
299299
});
300300

301+
expect(res.body).toEqual({
302+
message: 'Password updated successfully',
303+
});
301304
expect(res.status).toBe(200);
302305
expect(updateUserSpy).toHaveBeenCalledWith({
303306
username: 'alice',

0 commit comments

Comments
 (0)