📚

JWT Authentication【6Remove Cookie】

2022/10/08に公開

JWT Authentication【6Remove Cookie】

YouTube: https://youtu.be/N1WfT3nGGL4

{
    "email": "john@mail.com",
    "password": "john"
}
authControllers.ts
import {Request, Response} from 'express'
import { prisma } from '../utils/prismaClient'
import bcrypt from 'bcrypt'
import jwt from 'jsonwebtoken'
import { jwtSecretKey } from '../utils/jwtSecretKey'
import cookie from 'cookie'

export const register = async (req: Request, res: Response) => {
  const { email, name, password, confirm_password } = req.body

  if (password !== confirm_password ) {
    res.status(400).json({
      'message': "Password do not match confirm password."
    })
    return
  }

  const hashedPassword = await bcrypt.hash(password, 10)

  try {
    const user = await prisma.user.create({
      data: {
        email: email,
        name: name,
        password: hashedPassword
      },
      select: {
        id: true,
        email: true,
        name: true,
      }
    })
    res.status(200).json(user)
  } catch (error) {
    res.status(500).json({"error": error})
  }
}

export const login = async (req: Request, res: Response) => {
  const { email, password } = req.body

  try {
    const user = await prisma.user.findUnique({
      where: {
        email: email
      },
      select: {
        id: true,
        name: true,
        email: true,
        password: true
      }
    })

    if (user === null) {
      return res.status(404).json({error: "user do not exist"})
    }

    const compared = await bcrypt.compare(password, user.password)

    if (!compared) {
      return res.status(400).json({error: "password wrong"})
    }

    const token = jwt.sign({userId: user.id}, jwtSecretKey, {algorithm: 'HS256'})

    if (token) {
      res.setHeader('Set-Cookie', cookie.serialize('jwt', token, {
        httpOnly: true,
        secure: req.app.get('env') !== 'development'
        // secure: true,
        // sameSite: "none"
      }))
    }

    const resUser = {
      id: user.id,
      name: user.name,
      email: user.email
    }

    res.status(200).json(resUser)

  } catch (error: any) {
    res.status(500).json({error: error})
  }
}

export const logout = async (req: Request, res: Response) => {

  try {
    res.setHeader('Set-Cookie', cookie.serialize('jwt', '', {
      httpOnly: true,
      secure: req.app.get('env') !== 'development',
      // secure: true,
      // sameSite: "none"
    }))

    res.status(200).json({message: 'logout success'})
  } catch (error: any) {
    res.status(500).json({error: error})
  }
}
auth.ts
import { Router } from "express";
import { login, logout, register } from "../controllers/authControllers";

const router = Router()

router.post('/register', register)
router.post('/login', login)
router.post('/logout', logout)

export default router

Discussion