more refactor
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Has been cancelled

This commit is contained in:
2025-12-21 20:34:32 -08:00
parent 15f759cbc4
commit bc2c24bcff
46 changed files with 387 additions and 257 deletions

View File

@@ -105,30 +105,30 @@ router.post('/register', validateRequest(registerSchema), async (req: Request, r
// The createUser method in UserRepository now handles its own transaction.
const newUser = await userRepo.createUser(email, hashedPassword, { full_name, avatar_url }, req.log);
const userEmail = newUser.user.email || 'unknown';
const userId = newUser.user_id || 'unknown';
const userEmail = newUser.user.email;
const userId = newUser.user.user_id;
logger.info(`Successfully created new user in DB: ${userEmail} (ID: ${userId})`);
// Use the new standardized logging function
await adminRepo.logActivity({
userId: newUser.user_id,
userId: newUser.user.user_id,
action: 'user_registered',
displayText: `${userEmail} has registered.`,
icon: 'user-plus',
}, req.log);
const payload = { user_id: newUser.user_id, email: userEmail };
const payload = { user_id: newUser.user.user_id, email: userEmail };
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: '1h' });
const refreshToken = crypto.randomBytes(64).toString('hex');
await userRepo.saveRefreshToken(newUser.user_id, refreshToken, req.log);
await userRepo.saveRefreshToken(newUser.user.user_id, refreshToken, req.log);
res.cookie('refreshToken', refreshToken, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
});
return res.status(201).json({ message: 'User registered successfully!', user: payload, token });
return res.status(201).json({ message: 'User registered successfully!', userprofile: newUser, token });
} catch (error: unknown) {
if (error instanceof UniqueConstraintError) {
// If the email is a duplicate, return a 409 Conflict status.
@@ -168,12 +168,12 @@ router.post('/login', (req: Request, res: Response, next: NextFunction) => {
}
const userProfile = user as UserProfile;
const payload = { user_id: userProfile.user_id, email: userProfile.user.email, role: userProfile.role };
const payload = { user_id: userProfile.user.user_id, email: userProfile.user.email, role: userProfile.role };
const accessToken = jwt.sign(payload, JWT_SECRET, { expiresIn: '15m' });
try {
const refreshToken = crypto.randomBytes(64).toString('hex'); // This was a duplicate, fixed.
await userRepo.saveRefreshToken(userProfile.user_id, refreshToken, req.log);
await userRepo.saveRefreshToken(userProfile.user.user_id, refreshToken, req.log);
req.log.info(`JWT and refresh token issued for user: ${userProfile.user.email}`);
const cookieOptions = {
@@ -184,7 +184,7 @@ router.post('/login', (req: Request, res: Response, next: NextFunction) => {
res.cookie('refreshToken', refreshToken, cookieOptions);
// Return the full user profile object on login to avoid a second fetch on the client.
return res.json({ user: userProfile, token: accessToken });
return res.json({ userprofile: userProfile, token: accessToken });
} catch (tokenErr) {
req.log.error({ error: tokenErr }, `Failed to save refresh token during login for user: ${userProfile.user.email}`);
return next(tokenErr);