From 89feed52d4c2772a4c7af922c6c3414a5dcd0082 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 23 Sep 2024 23:54:19 +0530 Subject: [PATCH] feat: register controller --- server/src/routes/auth.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.ts index e69de29b..34465bf9 100644 --- a/server/src/routes/auth.ts +++ b/server/src/routes/auth.ts @@ -0,0 +1,38 @@ +import { hashPassword, comparePassword } from '../utils/auth'; +import bcrypt from 'bcrypt'; +import jwt from 'jsonwebtoken'; +// Todo: DB + +const register = async (req, res) => { + try { + const { email, password } = req.body + + if (!email) return res.status(400).send('Email is required') + if (!password || password.length < 6) return res.status(400).send('Password is required and must be at least 6 characters') + + let userExist = await User.findOne({ email }).exec() + if (userExist) return res.status(400).send('User already exists') + + const hashedPassword = await hashPassword(password) + + // register user + const user = new User({ + email, + password: hashedPassword + }) + await user.save() + const token = jwt.sign({ + _id: user._id + }, process.env.JWT_SECRET as string, { + expiresIn: '3d' + }) + user.password = undefined + res.cookie('token', token, { + httpOnly: true + }) + res.json(user) + } catch (error) { + res.status(500).send(`Could not register user - ${error.message}`) + } +} +