|
| 1 | +import jwt from 'jsonwebtoken'; |
| 2 | +import bcrypt from 'bcrypt'; |
| 3 | +import express from 'express'; |
| 4 | +import fieldParsers from '../utils/field-parsers'; |
| 5 | +import logger from '../utils/logger'; |
| 6 | +import { User } from '../mongo'; |
| 7 | +import config from '../utils/config'; |
| 8 | + |
| 9 | +const router = express.Router(); |
| 10 | +const { JWT_SECRET } = config; |
| 11 | + |
| 12 | +router.post('/login', async (req, res) => { |
| 13 | + if (!JWT_SECRET) { |
| 14 | + return res.status(500).send({ error: 'JWT_SECRET is not set.' }); |
| 15 | + } |
| 16 | + |
| 17 | + const { body } = req; |
| 18 | + let loginFields: { |
| 19 | + username: string, |
| 20 | + password: string, |
| 21 | + }; |
| 22 | + |
| 23 | + try { |
| 24 | + loginFields = fieldParsers.proofLogInFields(body); |
| 25 | + } catch (error) { |
| 26 | + return res.status(400).send({ error: logger.getErrorMessage(error) }); |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + const user = await User.findOne({ username: loginFields.username }); |
| 31 | + const isUserValidated = !user |
| 32 | + ? false |
| 33 | + : await bcrypt.compare(loginFields.password, user.passwordHash); |
| 34 | + |
| 35 | + if (!isUserValidated) { |
| 36 | + return res.status(401).send({ |
| 37 | + error: 'Invalid username or password.', |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + const userTokenInfo = { |
| 42 | + username: user.username, |
| 43 | + id: user.id, |
| 44 | + }; |
| 45 | + |
| 46 | + const accessToken = jwt.sign( |
| 47 | + userTokenInfo, |
| 48 | + JWT_SECRET, |
| 49 | + { expiresIn: '15m' }, |
| 50 | + ); |
| 51 | + const refreshToken = jwt.sign( |
| 52 | + userTokenInfo, |
| 53 | + JWT_SECRET, |
| 54 | + { expiresIn: '30d' }, |
| 55 | + ); |
| 56 | + |
| 57 | + res.cookie('refreshToken', refreshToken, { |
| 58 | + expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30 days |
| 59 | + httpOnly: true, |
| 60 | + secure: process.env.NODE_ENV === 'production', |
| 61 | + }); |
| 62 | + |
| 63 | + return res.status(200).send({ accessToken, username: user.username }); |
| 64 | + } catch (error) { |
| 65 | + return res.status(500).send({ error: 'Something went wrong!' }); |
| 66 | + } |
| 67 | +}); |
| 68 | + |
| 69 | +router.post('/refresh', async (req, res, next) => { |
| 70 | + if (!JWT_SECRET) { |
| 71 | + return res.status(500).send({ error: 'JWT_SECRET is not set.' }); |
| 72 | + } |
| 73 | + |
| 74 | + let decodedRefreshToken; |
| 75 | + const { refreshToken } = req.cookies; |
| 76 | + |
| 77 | + if (refreshToken) { |
| 78 | + try { |
| 79 | + decodedRefreshToken = jwt.verify( |
| 80 | + refreshToken, |
| 81 | + JWT_SECRET, |
| 82 | + ) as jwt.JwtPayload; |
| 83 | + } catch (error) { |
| 84 | + res.clearCookie('refreshToken'); |
| 85 | + |
| 86 | + return next(error); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if ( |
| 91 | + !decodedRefreshToken |
| 92 | + || !decodedRefreshToken.id |
| 93 | + || !decodedRefreshToken.username |
| 94 | + ) { |
| 95 | + res.clearCookie('refreshToken'); |
| 96 | + |
| 97 | + return res.status(401).send({ |
| 98 | + error: 'refresh token missing or invalid.', |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + const user = await User.findById(decodedRefreshToken.id); |
| 103 | + |
| 104 | + if (!user) { |
| 105 | + res.clearCookie('refreshToken'); |
| 106 | + |
| 107 | + return res.status(401).send({ |
| 108 | + error: 'refresh token is not valid for any user.', |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + // generate new access token |
| 113 | + const userTokenInfo = { |
| 114 | + username: user.username, |
| 115 | + id: decodedRefreshToken.id, |
| 116 | + }; |
| 117 | + |
| 118 | + const newAccessToken = jwt.sign( |
| 119 | + userTokenInfo, |
| 120 | + JWT_SECRET, |
| 121 | + { expiresIn: '15m' }, |
| 122 | + ); |
| 123 | + |
| 124 | + return res.status(200).send( |
| 125 | + { |
| 126 | + accessToken: newAccessToken, |
| 127 | + username: user.username, |
| 128 | + }, |
| 129 | + ); |
| 130 | +}); |
| 131 | + |
| 132 | +router.post('/logout', (_req, res) => { |
| 133 | + res.clearCookie('refreshToken'); |
| 134 | + res.status(204).end(); |
| 135 | +}); |
| 136 | + |
| 137 | +export default router; |
0 commit comments