index.js 1.2 KB
Newer Older
abergavenny's avatar
abergavenny committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import express from 'express'
import { checkSchema } from 'express-validator'
import rateLimit from 'express-rate-limit'

import { confirm, login, register, resetPassword, updatePassword } from '../../controllers/authentication.js'
import asyncHandler from '../../middleware/asyncHandler.js'
import { confirmSchema, loginSchema, registerSchema, updatePasswordSchema } from '../../schemas/authentication.js'
import { emailSchema } from '../../schemas/index.js'
import { warning } from '../../helpers/index.js'

const router = express.Router()
const authLimiter = rateLimit({
  windowMs: 15 * 60 * 1000,
  max: 25,
  standardHeaders: true,
  legacyHeaders: false,
  message: async (req, res) => {
    return warning(res, { code: 'TOO_MANY_REQUESTS' })
  }
})

router.use(authLimiter)

router.get('/confirm/:token', checkSchema(confirmSchema), asyncHandler(confirm))

router.post('/login', checkSchema(loginSchema), asyncHandler(login))
router.post('/register', checkSchema(registerSchema), asyncHandler(register))
router.post('/reset-password', checkSchema(emailSchema), asyncHandler(resetPassword))
router.post('/update-password/:token', checkSchema(updatePasswordSchema), asyncHandler(updatePassword))

export default router