|
1 | 1 | import { Request, Response } from 'express'; |
2 | 2 | import { User } from '../models/user_model'; |
3 | | -import { hashPassword } from '../utils/password_hash'; |
| 3 | +import { hashPassword, comparePassword } from '../utils/password_hash'; |
4 | 4 |
|
5 | 5 | // Email validation regex |
6 | 6 | const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/; |
@@ -90,3 +90,78 @@ export const signup = async (req: Request, res: Response) => { |
90 | 90 | }; |
91 | 91 |
|
92 | 92 |
|
| 93 | +/** |
| 94 | + * POST /api/auth/login |
| 95 | + * Authenticate user and log them in |
| 96 | + * |
| 97 | + * @param req.body.email - User's email |
| 98 | + * @param req.body.password - User's plain text password |
| 99 | + * @returns 200 with userId on success |
| 100 | + */ |
| 101 | +export const login = async (req: Request, res: Response) => { |
| 102 | + try { |
| 103 | + // 1 - extract data from req body |
| 104 | + const { email, password } = req.body as { |
| 105 | + email?: string; |
| 106 | + password?: string; |
| 107 | + }; |
| 108 | + |
| 109 | + // 2 - validate required fields |
| 110 | + if (!email || !password) { |
| 111 | + return res.status(400).json({ |
| 112 | + error: 'Email and password are required' |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + // 3 - normalize email |
| 117 | + const normalizedEmail = email.toLowerCase().trim(); |
| 118 | + |
| 119 | + // 4 - validate email format |
| 120 | + if (!EMAIL_REGEX.test(normalizedEmail)) { |
| 121 | + return res.status(400).json({ |
| 122 | + error: 'Invalid email format' |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + // 5 - find user by email |
| 127 | + const user = await User.findOne({ email: normalizedEmail }) |
| 128 | + .select('+passwordHash'); // need this since queries don't return passwordHash by default |
| 129 | + |
| 130 | + // 6 - check if user exists |
| 131 | + if (!user) { |
| 132 | + // for security purposes I won't include whether email exists or not |
| 133 | + return res.status(401).json({ |
| 134 | + error: 'Invalid credentials' |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + // 7 - verify password |
| 139 | + const isPasswordValid = await comparePassword(password, user.passwordHash); |
| 140 | + |
| 141 | + if (!isPasswordValid) { |
| 142 | + return res.status(401).json({ |
| 143 | + error: 'Invalid credentials' |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + // 8 - update lastLogin stamp |
| 148 | + user.lastLogin = new Date(); |
| 149 | + await user.save(); // save the updated user |
| 150 | + |
| 151 | + // 9 - return success |
| 152 | + res.status(200).json({ |
| 153 | + message: 'Login successful', |
| 154 | + userId: user._id |
| 155 | + // TODO: figure out a way to add JWT token here |
| 156 | + }); |
| 157 | + |
| 158 | + } catch (err: any) { |
| 159 | + console.error('POST /api/auth/login error:', err); |
| 160 | + |
| 161 | + // Generic error Response |
| 162 | + res.status(500).json({ |
| 163 | + error: 'Login failed' |
| 164 | + }); |
| 165 | + } |
| 166 | +}; |
| 167 | + |
0 commit comments