🚧 register endpoint to return token
This commit is contained in:
		
							parent
							
								
									9f7482c002
								
							
						
					
					
						commit
						1519b2d0f1
					
				| @ -3,7 +3,7 @@ import { PrismaClient } from "@prisma/client"; | ||||
| import bcrypt from "bcrypt"; | ||||
| import jwt from "jsonwebtoken"; | ||||
| 
 | ||||
| const prisma = new PrismaClient(); | ||||
| const prisma = new PrismaClient() | ||||
| const SECRET_KEY = process.env.JWT_SECRET as string; | ||||
| 
 | ||||
| export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||||
| @ -17,7 +17,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) | ||||
|     const isMatch = await bcrypt.compare(password, user.password); | ||||
|     if (!isMatch) return res.status(401).json({ message: "Invalid credentials" }); | ||||
| 
 | ||||
|     const token = jwt.sign({ userId: user.id, email: user.email }, SECRET_KEY, { expiresIn: "1h" }); | ||||
|     const token = jwt.sign({ email: user.email }, SECRET_KEY, { expiresIn: "1d" }); | ||||
| 
 | ||||
|     res.setHeader("Set-Cookie", `token=${token}; HttpOnly; Path=/; Secure`); | ||||
|     res.json({ token }); | ||||
|  | ||||
| @ -1,8 +1,11 @@ | ||||
| import { NextApiRequest, NextApiResponse } from "next"; | ||||
| import { PrismaClient } from "@prisma/client"; | ||||
| import bcrypt from "bcrypt"; | ||||
| import jwt from "jsonwebtoken"; | ||||
| 
 | ||||
| const prisma = new PrismaClient() | ||||
| const SECRET_KEY = process.env.JWT_SECRET as string; | ||||
| 
 | ||||
| const prisma = new PrismaClient(); | ||||
| 
 | ||||
| export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||||
|     if (req.method !== "POST") return res.status(405).json({ message: "Method not allowed" }); | ||||
| @ -17,5 +20,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) | ||||
|         data: { email, password: hashedPassword }, | ||||
|     }); | ||||
| 
 | ||||
|     res.status(201).json({ message: "User registered", user }); | ||||
|     const token = jwt.sign({ email: user.email }, SECRET_KEY, { expiresIn: "1d" }); | ||||
| 
 | ||||
|     res.setHeader("Set-Cookie", `token=${token}; HttpOnly; Path=/; Secure`); | ||||
|     res.status(201).json({ message: "User registered", user, token }); | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 sam
						sam