Skip to content

Commit f9fc168

Browse files
committed
fix: address revu-bot review comments on PR #3187
- middleware: treat a missing token.isAdmin as 'old token' and force re-login so the flag gets populated (otherwise existing users with a pre-PR JWT would be bounced to /mon-espace for up to 30 days). - auth/config: memoize the ADMIN_EMAILS set at module load instead of reparsing the env var on every sign-in. - admin/page: split the defense-in-depth check to redirect to /login when there is no session (avoids a redirect chain).
1 parent 725c973 commit f9fc168

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

packages/app/src/server/auth/config.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,15 @@ declare module "next-auth/jwt" {
154154
}
155155

156156
/**
157-
* Parse the `ADMIN_EMAILS` env var (comma-separated) into a normalized set.
157+
* Normalized set of admin emails parsed once from `ADMIN_EMAILS`.
158+
* The env var never changes at runtime, so we memoize it at module load.
158159
*/
159-
function getAdminEmails(): Set<string> {
160-
return new Set(
161-
(env.ADMIN_EMAILS ?? "")
162-
.split(",")
163-
.map((email) => email.trim().toLowerCase())
164-
.filter(Boolean),
165-
);
166-
}
160+
const ADMIN_EMAILS: Set<string> = new Set(
161+
(env.ADMIN_EMAILS ?? "")
162+
.split(",")
163+
.map((email) => email.trim().toLowerCase())
164+
.filter(Boolean),
165+
);
167166

168167
function getProviders(): Provider[] {
169168
const providers: Provider[] = [];
@@ -378,8 +377,7 @@ export const authConfig = {
378377

379378
// Sync the admin flag with `ADMIN_EMAILS` on every login.
380379
// Listing an email promotes the user; removing it demotes them.
381-
const adminEmails = getAdminEmails();
382-
const shouldBeAdmin = adminEmails.has(email.toLowerCase());
380+
const shouldBeAdmin = ADMIN_EMAILS.has(email.toLowerCase());
383381
if (shouldBeAdmin !== dbUser.isAdmin) {
384382
await db
385383
.update(users)

0 commit comments

Comments
 (0)