Files
spotlightcam/backend/src/routes/users.js
Radosław Gierwiało 833818f17d fix: include emailVerified field in /api/users/me endpoint
The verification banner was reappearing after login/refresh because
the /api/users/me endpoint was not returning the emailVerified field.

This ensures the frontend always has access to the current email
verification status when loading user data.
2025-11-13 19:03:39 +01:00

68 lines
1.6 KiB
JavaScript

const express = require('express');
const { authenticate } = require('../middleware/auth');
const { prisma } = require('../utils/db');
const router = express.Router();
// GET /api/users/me - Get current authenticated user
router.get('/me', authenticate, async (req, res, next) => {
try {
// req.user is set by authenticate middleware
const user = await prisma.user.findUnique({
where: { id: req.user.id },
select: {
id: true,
username: true,
email: true,
emailVerified: true,
avatar: true,
createdAt: true,
updatedAt: true,
_count: {
select: {
matchesAsUser1: true,
matchesAsUser2: true,
ratingsReceived: true,
},
},
},
});
if (!user) {
return res.status(404).json({
success: false,
error: 'User not found',
});
}
// Calculate total matches
const totalMatches = user._count.matchesAsUser1 + user._count.matchesAsUser2;
// Calculate average rating
const ratings = await prisma.rating.findMany({
where: { ratedId: user.id },
select: { score: true },
});
const averageRating = ratings.length > 0
? ratings.reduce((sum, r) => sum + r.score, 0) / ratings.length
: 0;
res.json({
success: true,
data: {
...user,
stats: {
matchesCount: totalMatches,
ratingsCount: user._count.ratingsReceived,
rating: averageRating.toFixed(1),
},
},
});
} catch (error) {
next(error);
}
});
module.exports = router;