Noah
Noah
PPrisma
Created by Noah on 12/9/2024 in #help-and-questions
Retrieve user information
I try to create a user and store it in my db from the infomations that the user will enter on login information import { Request, Response } from 'express'; import bcrypt from 'bcryptjs'; import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); interface CreateUserRequest { email: string; userPassword: string; } export default async function handler(req: Request, res: Response) { if (req.method === 'POST') { const { email, userPassword }: CreateUserRequest = req.body; if (!email || !userPassword) { return res.status(400).json({ message: 'Email et mot de passe sont requis' }); } try { const hashedPassword = await bcrypt.hash(userPassword, 10);
const product = await prisma.user.create({ data: { email, userPassword: hashedPassword }, }); res.status(201).json(product); } catch (error) { console.error('Erreur de création de l'utilisateur:', error); res.status(500).json({ message: 'Erreur de création de l'utilisateur', error: error.message }); } } else { // Si la méthode HTTP n'est pas POST res.status(405).json({ message: 'Méthode non autorisée' }); } }
6 replies