diff --git a/src/routes/auth/index.ts b/src/routes/auth/index.ts index 1752cae..f568428 100644 --- a/src/routes/auth/index.ts +++ b/src/routes/auth/index.ts @@ -37,7 +37,13 @@ router.post( const { status, response } = await authManager.loginUser(loginData); if (status === StatusCodes.OK) { - res.setHeader('Authorization', 'Bearer ' + response); + res.cookie('Authorization', 'Bearer ' + response, { + httpOnly: true, + secure: process.env.PRODUCTION === 'production', + sameSite: process.env.PRODUCTION === 'production' ? 'none' : 'lax', + maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days in milliseconds + path: '/', + }); res.status(status).send({ response: 'Logged in successfully' }); return; @@ -90,4 +96,26 @@ router.post( } ); +router.post( + '/logout', + validateAndExtractAuthToken(), + (req: Request, res: Response) => { + const userId = req.userId; + + if (!userId) { + res.status(StatusCodes.UNAUTHORIZED).send({ response: 'Unauthorized' }); + return; + } + + res.clearCookie('Authorization', { + httpOnly: true, + secure: process.env.PRODUCTION === 'production', + sameSite: process.env.PRODUCTION === 'production' ? 'none' : 'lax', + path: '/', + }); + + res.status(StatusCodes.OK).send({ response: 'Logged out successfully' }); + } +); + export default router;