verifyOTP.js 847 B

1234567891011121314151617181920212223242526272829303132333435
  1. const { validation } = require('./validation')
  2. const response = require('../utils/responseHandler')
  3. const jwt = require('jsonwebtoken')
  4. module.exports = [
  5. validation((req) => req.body, { otp: 'string' }),
  6. (req, res, next) => {
  7. if (!req.cookies['sidali-otp']) {
  8. return response.error(res, {
  9. code: 401,
  10. message: 'Unauthorized',
  11. })
  12. }
  13. const token = req.cookies['sidali-otp']
  14. jwt.verify(token, process.env.SRU51, async (err, data) => {
  15. if (err) {
  16. return response.error(res, {
  17. code: 401,
  18. message: 'Unauthorized',
  19. })
  20. }
  21. if (req.body.otp !== data.otp) {
  22. return response.error(res, {
  23. message: 'OTP tidak valid',
  24. code: 401
  25. })
  26. }
  27. req.no_hp = data.no_hp
  28. res.clearCookie('sidali-otp')
  29. return next()
  30. })
  31. }
  32. ]