{"size": 2393, "ext": "js", "max_stars_repo_path": "router/auth/auth-router.js", "max_stars_repo_name": "raythurman2386/gigapet-backend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "router/auth/auth-router.js", "max_issues_repo_name": "raythurman2386/gigapet-backend", "max_issues_repo_issues_event_min_datetime": "2021-05-11T02:49:51.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T06:16:58.000Z", "max_forks_repo_path": "router/auth/auth-router.js", "max_forks_repo_name": "raythurman2386/gigapet-backend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 26.8876404494, "max_line_length": 80, "alphanum_fraction": 0.5737567906}
const authRouter = require('express').Router()
const bcrypt = require('bcryptjs')
const generateToken = require('../../token/generateToken')
const { Parent } = require('../../models/Model')
const { validateRegister, validateLogin } = require('../../middleware/validate')
const sendgrid = require('../../utils/sendgrid')
const twilio = require('../../utils/twilio')
const {
  accountLimiter,
  resetLimiter
} = require('../../middleware/accountLimiter')

authRouter
  .post(
    '/register',
    accountLimiter,
    validateRegister(),
    async (req, res, next) => {
      try {
        let user = req.body
        const hashPw = await bcrypt.hash(user.password, 12)
        user.password = hashPw

        await Parent.add(user)
        return res
          .status(201)
          .json({ message: 'You have been successfully registered' })
      } catch (error) {
        console.log(error)
        next(error)
      }
    }
  )

  .post(
    '/login',
    accountLimiter,
    validateLogin(),
    twilio(),
    async (req, res, next) => {
      try {
        const { username, password } = req.body
        const user = await Parent.findBy({ username })
        const verifyPw = await bcrypt.compare(password, user.password)

        if (user && verifyPw) {
          const token = generateToken(user)
          return res.status(200).json({
            message: `Welcome ${user.username}`,
            parent_name: user.parent_name,
            token
          })
        } else {
          return res.status(401).json({
            message: 'Invalid Credentials'
          })
        }
      } catch (error) {
        next(error)
      }
    }
  )

  .post('/initiate-reset', accountLimiter, sendgrid(), (req, res, next) => {
    try {
      return res.status(200).json({ message: 'Please check your email' })
    } catch (error) {
      next(error)
    }
  })

  .post('/reset-password', resetLimiter, async (req, res, next) => {
    try {
      const { email, new_password } = req.body
      let user = await Parent.findBy({ email: email })
      const hashPw = await bcrypt.hash(new_password, 12)
      let updatedParent = {
        ...user,
        password: hashPw
      }
      await Parent.update(user.id, updatedParent)
      return res.status(200).json({ message: 'Password successfully updated' })
    } catch (error) {
      next(error)
    }
  })

module.exports = authRouter
