Pr comments

This commit is contained in:
Luke Vella 2025-04-23 16:29:38 +01:00
parent 027a19e827
commit c39004a998
No known key found for this signature in database
GPG key ID: 469CAD687F0D784C
2 changed files with 20 additions and 43 deletions

View file

@ -4,7 +4,6 @@ import { redirect } from "next/navigation";
import NextAuth from "next-auth"; import NextAuth from "next-auth";
import type { Provider } from "next-auth/providers"; import type { Provider } from "next-auth/providers";
import { cache } from "react"; import { cache } from "react";
import z from "zod";
import { CustomPrismaAdapter } from "./auth/adapters/prisma"; import { CustomPrismaAdapter } from "./auth/adapters/prisma";
import { isEmailBanned, isEmailBlocked } from "./auth/helpers/is-email-blocked"; import { isEmailBanned, isEmailBlocked } from "./auth/helpers/is-email-blocked";
@ -17,13 +16,6 @@ import { OIDCProvider } from "./auth/providers/oidc";
import { RegistrationTokenProvider } from "./auth/providers/registration-token"; import { RegistrationTokenProvider } from "./auth/providers/registration-token";
import { nextAuthConfig } from "./next-auth.config"; import { nextAuthConfig } from "./next-auth.config";
const sessionUpdateSchema = z.object({
locale: z.string().nullish(),
timeFormat: z.enum(["hours12", "hours24"]).nullish(),
timeZone: z.string().nullish(),
weekStart: z.number().nullish(),
});
const { const {
auth: originalAuth, auth: originalAuth,
handlers, handlers,
@ -162,43 +154,26 @@ const {
return true; return true;
}, },
async jwt({ token, session }) { async jwt({ token }) {
if (session) { const userId = token.sub;
const parsed = sessionUpdateSchema.safeParse(session); const isGuest = userId?.startsWith("guest-");
if (parsed.success) {
Object.entries(parsed.data).forEach(([key, value]) => {
token[key] = value;
});
} else {
console.error(parsed.error);
}
} else {
const userId = token.sub;
const isGuest = userId?.startsWith("guest-");
if (userId && !isGuest) { if (userId && !isGuest) {
const user = await prisma.user.findUnique({ const user = await prisma.user.findUnique({
where: { where: {
id: userId, id: userId,
}, },
select: { select: {
name: true, name: true,
email: true, email: true,
timeFormat: true, image: true,
timeZone: true, },
weekStart: true, });
image: true,
},
});
if (user) { if (user) {
token.name = user.name; token.name = user.name;
token.email = user.email; token.email = user.email;
token.picture = user.image; token.picture = user.image;
token.timeFormat = user.timeFormat;
token.timeZone = user.timeZone;
token.weekStart = user.weekStart;
}
} }
} }

View file

@ -120,6 +120,8 @@ export const auth = router({
name, name,
email, email,
timeZone: input.timeZone, timeZone: input.timeZone,
timeFormat: input.timeFormat,
weekStart: input.weekStart,
locale: input.locale, locale: input.locale,
}, },
}); });