Spaces:
Sleeping
Sleeping
File size: 15,168 Bytes
f4656a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | 'use strict';
const jwt = require('jsonwebtoken');
const passport = require('passport');
const { v4: uuidv4 } = require('uuid');
const UserModel = require('../models/user.model');
const { hashPassword, verifyPassword } = require('../models/user.model');
const EmailService = require('../services/email.service');
const { getDb, getDbType } = require('../config/database');
const logger = require('../utils/logger');
const JWT_SECRET = process.env.JWT_SECRET || 'dev-secret-change-in-production';
const JWT_EXPIRES = process.env.JWT_EXPIRES || '7d';
const OTP_TTL_MS = 10 * 60 * 1000; // 10 minutes
const MAX_OTP_ATTEMPTS = 5;
// Max 3 OTP requests per email per hour (enforced in-process; use Redis for multi-instance)
const otpRateMap = new Map(); // email β { count, resetAt }
function signToken(user) {
return jwt.sign(
{ id: user.id, email: user.email, name: user.name },
JWT_SECRET,
{ expiresIn: JWT_EXPIRES }
);
}
function generateOtp() {
return String(Math.floor(100000 + Math.random() * 900000));
}
function checkOtpRate(email) {
const now = Date.now();
const rec = otpRateMap.get(email);
if (!rec || now > rec.resetAt) {
otpRateMap.set(email, { count: 1, resetAt: now + 3600_000 });
return true;
}
if (rec.count >= 3) return false;
rec.count++;
return true;
}
function setAuthCookie(res, token) {
res.cookie('smart_token', token, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
});
}
function clearAuthCookie(res) {
res.clearCookie('smart_token', { httpOnly: true, sameSite: 'lax' });
}
// ββ DB helpers for temp_users βββββββββββββββββββββββββββββββββββββββββββββββββ
function dbRun(sql, params = []) {
const db = getDb();
if (getDbType() === 'sqlite') {
db.prepare(sql).run(...params);
}
}
function dbGet(sql, params = []) {
const db = getDb();
if (getDbType() === 'sqlite') {
return db.prepare(sql).get(...params);
}
return null;
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// Controller
// βββββββββββββββββββββββββββββββββββββββββββββ
const AuthController = {
/**
* POST /api/auth/signup (legacy β direct signup without OTP, kept for backward compat)
*/
async signup(req, res) {
const { name, email, password } = req.body;
try {
const existing = await UserModel.findByEmail(email);
if (existing) return res.status(409).json({ error: 'Email already registered' });
const passwordHash = await hashPassword(password);
const user = await UserModel.create({ name, email, passwordHash });
await UserModel.trackActivity({ userId: String(user.id), action: 'signup', data: { email: user.email } });
const token = signToken(user);
logger.info(`New user registered (direct): ${email}`);
setAuthCookie(res, token);
return res.status(201).json({ token, user: { id: user.id, name: user.name, email: user.email } });
} catch (err) {
logger.error('Signup error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
},
/**
* POST /api/auth/send-otp
* Step 1 of OTP signup: store temp user, send OTP email.
*/
async sendOtp(req, res) {
const { name, email, password } = req.body;
try {
// Rate limit
if (!checkOtpRate(email.toLowerCase())) {
return res.status(429).json({ error: 'Too many OTP requests. Try again in an hour.' });
}
// Check if already permanently registered
const existing = await UserModel.findByEmail(email);
if (existing) return res.status(409).json({ error: 'Email already registered. Please sign in.' });
const passwordHash = await hashPassword(password);
const otp = generateOtp();
const otpExpiresAt = new Date(Date.now() + OTP_TTL_MS).toISOString();
const tempId = uuidv4();
// Upsert temp_users (overwrite if same email retried)
dbRun(
`INSERT INTO temp_users (id, name, email, password_hash, otp, otp_expires_at, otp_attempts)
VALUES (?, ?, ?, ?, ?, ?, 0)
ON CONFLICT(email) DO UPDATE SET
id=excluded.id, name=excluded.name, password_hash=excluded.password_hash,
otp=excluded.otp, otp_expires_at=excluded.otp_expires_at, otp_attempts=0`,
[tempId, name, email.toLowerCase(), passwordHash, otp, otpExpiresAt]
);
// Get the actual stored id (may differ if email already existed)
const stored = dbGet('SELECT id FROM temp_users WHERE email = ?', [email.toLowerCase()]);
await EmailService.sendOtp(email, otp, name);
logger.info(`OTP sent for signup: ${email}`);
return res.status(200).json({ success: true, tempUserId: stored.id });
} catch (err) {
logger.error('send-otp error:', err);
return res.status(500).json({ error: 'Failed to send OTP. Please try again.' });
}
},
/**
* POST /api/auth/verify-otp
* Step 2: verify OTP, create permanent user, return JWT.
*/
async verifyOtp(req, res) {
const { tempUserId, otp } = req.body;
try {
const temp = dbGet('SELECT * FROM temp_users WHERE id = ?', [tempUserId]);
if (!temp) return res.status(400).json({ error: 'Session expired. Please start again.' });
// Check expiry
if (new Date(temp.otp_expires_at) < new Date()) {
dbRun('DELETE FROM temp_users WHERE id = ?', [tempUserId]);
return res.status(400).json({ error: 'OTP expired. Please request a new one.' });
}
// Check attempts
if (temp.otp_attempts >= MAX_OTP_ATTEMPTS) {
dbRun('DELETE FROM temp_users WHERE id = ?', [tempUserId]);
return res.status(400).json({ error: 'Too many failed attempts. Please start again.' });
}
// Verify OTP
if (temp.otp !== otp) {
dbRun('UPDATE temp_users SET otp_attempts = otp_attempts + 1 WHERE id = ?', [tempUserId]);
const remaining = MAX_OTP_ATTEMPTS - (temp.otp_attempts + 1);
return res.status(400).json({ error: `Incorrect code. ${remaining} attempt(s) remaining.` });
}
// Check email not taken (race condition guard)
const existing = await UserModel.findByEmail(temp.email);
if (existing) {
dbRun('DELETE FROM temp_users WHERE id = ?', [tempUserId]);
return res.status(409).json({ error: 'Email already registered. Please sign in.' });
}
// Create permanent user
const user = await UserModel.create({ name: temp.name, email: temp.email, passwordHash: temp.password_hash });
dbRun('DELETE FROM temp_users WHERE id = ?', [tempUserId]);
await UserModel.trackActivity({ userId: String(user.id), action: 'signup', data: { email: user.email, method: 'otp' } });
const token = signToken(user);
logger.info(`New user verified & created: ${temp.email}`);
setAuthCookie(res, token);
return res.status(201).json({ success: true, token, user: { id: user.id, name: user.name, email: user.email } });
} catch (err) {
logger.error('verify-otp error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
},
/**
* POST /api/auth/resend-otp
* Regenerate and resend OTP for an existing temp session.
*/
async resendOtp(req, res) {
const { tempUserId } = req.body;
try {
const temp = dbGet('SELECT * FROM temp_users WHERE id = ?', [tempUserId]);
if (!temp) return res.status(400).json({ error: 'Session not found. Please start again.' });
if (!checkOtpRate(temp.email)) {
return res.status(429).json({ error: 'Too many OTP requests. Try again in an hour.' });
}
const otp = generateOtp();
const otpExpiresAt = new Date(Date.now() + OTP_TTL_MS).toISOString();
dbRun('UPDATE temp_users SET otp = ?, otp_expires_at = ?, otp_attempts = 0 WHERE id = ?', [otp, otpExpiresAt, tempUserId]);
await EmailService.sendOtp(temp.email, otp, temp.name);
logger.info(`OTP resent for: ${temp.email}`);
return res.status(200).json({ success: true });
} catch (err) {
logger.error('resend-otp error:', err);
return res.status(500).json({ error: 'Failed to resend OTP.' });
}
},
/**
* POST /api/auth/login
*/
async login(req, res) {
const { email, password } = req.body;
try {
const user = await UserModel.findByEmail(email);
if (!user) return res.status(401).json({ error: 'Invalid email or password' });
const valid = await verifyPassword(password, user.password_hash);
if (!valid) return res.status(401).json({ error: 'Invalid email or password' });
await UserModel.updateLastSeen(user.id);
await UserModel.trackActivity({ userId: String(user.id), action: 'login', data: { email: user.email } });
const token = signToken(user);
logger.info(`User logged in: ${email}`);
setAuthCookie(res, token);
return res.status(200).json({ token, user: { id: user.id, name: user.name, email: user.email } });
} catch (err) {
logger.error('Login error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
},
/**
* POST /api/auth/logout
*/
async logout(req, res) {
try {
if (req.user) {
await UserModel.trackActivity({ userId: String(req.user.id), action: 'logout' });
}
clearAuthCookie(res);
return res.status(200).json({ message: 'Logged out successfully' });
} catch {
clearAuthCookie(res);
return res.status(200).json({ message: 'Logged out' });
}
},
/**
* GET /api/auth/me
*/
async me(req, res) {
try {
const user = await UserModel.findById(req.user.id);
if (!user) return res.status(404).json({ error: 'User not found' });
const activity = await UserModel.getActivitySummary(String(user.id));
return res.status(200).json({
user: { id: user.id, name: user.name, email: user.email, createdAt: user.created_at, lastSeen: user.last_seen_at },
recentActivity: activity.slice(0, 10),
});
} catch (err) {
logger.error('me error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
},
/**
* POST /api/auth/forgot-password
* Send OTP to registered email for password reset.
*/
async forgotPassword(req, res) {
const { email } = req.body;
try {
// Always return success to prevent email enumeration
const user = await UserModel.findByEmail(email);
if (!user) {
return res.status(200).json({ success: true, message: 'If this email is registered, you will receive a reset code.' });
}
if (!checkOtpRate(email.toLowerCase())) {
return res.status(429).json({ error: 'Too many requests. Try again in an hour.' });
}
const otp = generateOtp();
const otpExpiresAt = new Date(Date.now() + OTP_TTL_MS).toISOString();
const resetId = uuidv4();
dbRun(
`INSERT INTO password_resets (id, email, otp, otp_expires_at, otp_attempts) VALUES (?, ?, ?, ?, 0)`,
[resetId, email.toLowerCase(), otp, otpExpiresAt]
);
await EmailService.sendPasswordResetOtp(email, otp);
logger.info(`Password reset OTP sent to: ${email}`);
return res.status(200).json({ success: true, resetId });
} catch (err) {
logger.error('forgot-password error:', err);
return res.status(500).json({ error: 'Failed to send reset email.' });
}
},
/**
* GET /auth/google
* Initiates the Google OAuth 2.0 redirect flow via Passport.
* No session created β stateless JWT only.
*/
redirectToGoogle(req, res, next) {
return passport.authenticate('google', {
scope: ['profile', 'email'],
session: false,
})(req, res, next);
},
/**
* GET /auth/google/callback
* Called by Google after user grants/denies access.
* Passport has already verified the code and populated req.user.
* Generates a JWT, sets an httpOnly cookie, then redirects to the frontend
* /auth/callback page which persists the token in localStorage.
*/
handleGoogleCallback(req, res) {
if (!req.user) {
logger.warn('Google OAuth callback reached without req.user');
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3001';
return res.redirect(`${frontendUrl}/auth/callback?error=auth_failed`);
}
const user = req.user;
// Fire-and-forget activity tracking (non-blocking)
UserModel.updateLastSeen(user.id).catch((e) => logger.error('updateLastSeen error:', e));
UserModel.trackActivity({
userId: String(user.id),
action: 'login',
data: { method: 'google_oauth', email: user.email },
}).catch((e) => logger.error('trackActivity error:', e));
const token = signToken(user);
logger.info(`Google OAuth login: ${user.email}`);
setAuthCookie(res, token);
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3001';
return res.redirect(`${frontendUrl}/auth/callback?token=${token}`);
},
/**
* POST /api/auth/reset-password
* Verify OTP and update password.
*/
async resetPassword(req, res) {
const { resetId, otp, newPassword } = req.body;
try {
const record = dbGet('SELECT * FROM password_resets WHERE id = ?', [resetId]);
if (!record) return res.status(400).json({ error: 'Reset session expired. Please request again.' });
if (new Date(record.otp_expires_at) < new Date()) {
dbRun('DELETE FROM password_resets WHERE id = ?', [resetId]);
return res.status(400).json({ error: 'OTP expired. Please request a new one.' });
}
if (record.otp_attempts >= MAX_OTP_ATTEMPTS) {
dbRun('DELETE FROM password_resets WHERE id = ?', [resetId]);
return res.status(400).json({ error: 'Too many failed attempts. Please request a new reset.' });
}
if (record.otp !== otp) {
dbRun('UPDATE password_resets SET otp_attempts = otp_attempts + 1 WHERE id = ?', [resetId]);
const remaining = MAX_OTP_ATTEMPTS - (record.otp_attempts + 1);
return res.status(400).json({ error: `Incorrect code. ${remaining} attempt(s) remaining.` });
}
// Update user password
const newHash = await hashPassword(newPassword);
const db = getDb();
db.prepare(`UPDATE users SET password_hash = ?, updated_at = datetime('now') WHERE email = ?`).run(newHash, record.email);
dbRun('DELETE FROM password_resets WHERE id = ?', [resetId]);
logger.info(`Password reset successful: ${record.email}`);
return res.status(200).json({ success: true, message: 'Password updated successfully.' });
} catch (err) {
logger.error('reset-password error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
},
};
module.exports = AuthController;
|