|
1 | 1 | import { Request, Response } from 'express';
|
2 | 2 | import asyncHandler from '../util/catchAsync';
|
| 3 | +import UserModel from '../models/userSchema'; |
| 4 | +import jwt from 'jsonwebtoken'; |
| 5 | +import bcrypt from 'bcryptjs'; |
| 6 | +import validator from 'validator'; |
| 7 | +import { CookieOptions } from '../interfaces/cookieOption'; |
| 8 | +import config from '../config/config'; |
3 | 9 |
|
4 | 10 | export const testRoute = asyncHandler(async (req: Request, res: Response) => {
|
5 | 11 | res.json({ success: true });
|
6 | 12 | });
|
| 13 | + |
| 14 | +export const login = asyncHandler(async (req: Request, res: Response) => { |
| 15 | + try { |
| 16 | + const { email, password } = req.body; |
| 17 | + const user = await UserModel.findOne({ email }); |
| 18 | + if (!user) { |
| 19 | + return res.status(404).json({ |
| 20 | + success: false, |
| 21 | + message: 'Invalid credentials', |
| 22 | + }); |
| 23 | + } |
| 24 | + const isPasswordMatch = await bcrypt.compare(password, user.password); |
| 25 | + if (!isPasswordMatch) { |
| 26 | + return res.status(401).json({ |
| 27 | + success: false, |
| 28 | + message: 'Invalid credentials', |
| 29 | + }); |
| 30 | + } |
| 31 | + const token = jwt.sign({ userId: user._id }, config.JWT_SECRET); |
| 32 | + const expireTime: number = parseInt(config.JWT_COOKIE_EXPIRES_IN); |
| 33 | + |
| 34 | + const cookieOptions: CookieOptions = { |
| 35 | + expires: new Date(Date.now() + expireTime * 24 * 60 * 60 * 1000), |
| 36 | + httpOnly: true, |
| 37 | + secure: req.secure || req.headers['x-forwarded-proto'] === 'https', |
| 38 | + sameSite: 'strict', |
| 39 | + }; |
| 40 | + res.cookie('jwt', token, cookieOptions); |
| 41 | + user.password = undefined; |
| 42 | + user.cpassword = undefined; |
| 43 | + res.setHeader('Authorization', `Bearer ${token}`); |
| 44 | + |
| 45 | + res.status(200).json({ success: true, data: user, jwt_token: token }); |
| 46 | + } catch (error) { |
| 47 | + console.error('Login error:', error); |
| 48 | + res.status(500).json({ |
| 49 | + success: false, |
| 50 | + message: 'Internal server error', |
| 51 | + }); |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +export const register = asyncHandler(async (req: Request, res: Response) => { |
| 56 | + const { name, email, password, cpassword } = req.body; |
| 57 | + |
| 58 | + if (!name || !email || !password || !cpassword || !validator.isEmail(email)) { |
| 59 | + return res |
| 60 | + .status(400) |
| 61 | + .json({ message: 'Invalid input data!', success: false }); |
| 62 | + } |
| 63 | + const checkUser = await UserModel.findOne({ email }); |
| 64 | + if (checkUser) { |
| 65 | + return res |
| 66 | + .status(409) |
| 67 | + .json({ success: false, message: 'User already exists!' }); |
| 68 | + } |
| 69 | + try { |
| 70 | + const user = new UserModel({ |
| 71 | + name, |
| 72 | + email, |
| 73 | + password, |
| 74 | + cpassword, |
| 75 | + }); |
| 76 | + await user.save(); |
| 77 | + return res.status(201).json({ |
| 78 | + message: 'Registration successful!', |
| 79 | + success: true, |
| 80 | + userId: user._id, |
| 81 | + }); |
| 82 | + } catch (error) { |
| 83 | + console.error('Registration error:', error); |
| 84 | + return res |
| 85 | + .status(500) |
| 86 | + .json({ message: 'Registration failed!', success: false }); |
| 87 | + } |
| 88 | +}); |
0 commit comments