Skip to content

Commit

Permalink
signin
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrixer committed Dec 14, 2021
1 parent 169b99d commit d864e1e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
5 changes: 0 additions & 5 deletions pages/api/hello.js

This file was deleted.

45 changes: 45 additions & 0 deletions pages/api/signin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import bcrypt from 'bcrypt'
import jwt from 'jsonwebtoken'
import cookie from 'cookie'
import { NextApiRequest, NextApiResponse } from 'next'
import prisma from '../../lib/prisma'

export default async (req: NextApiRequest, res: NextApiResponse) => {
const { email, password } = req.body

const user = await prisma.user.findUnique({
where: {
email,
},
})

if (user && bcrypt.compareSync(password, user.password)) {
const token = jwt.sign(
{
id: user.id,
email: user.email,
time: Date.now(),
},
'hello',
{
expiresIn: '8h',
}
)

res.setHeader(
'Set-Cookie',
cookie.serialize('TRAX_ACCESS_TOKEN', token, {
httpOnly: true,
maxAge: 8 * 60 * 60,
path: '/',
sameSite: 'lax',
secure: process.env.NODE_ENV === 'production',
})
)

res.json(user)
} else {
res.status(401)
res.json({ error: 'Email or Password is wrong' })
}
}

0 comments on commit d864e1e

Please sign in to comment.