|
| 1 | +import { redirect, type ActionFunctionArgs } from "@remix-run/server-runtime"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { prisma } from "~/db.server"; |
| 4 | +import { |
| 5 | + commitSession as commitMessageSession, |
| 6 | + getSession as getMessageSession, |
| 7 | + setErrorMessage, |
| 8 | + setSuccessMessage, |
| 9 | +} from "~/models/message.server"; |
| 10 | +import { requireUserId } from "~/services/session.server"; |
| 11 | +import { |
| 12 | + commitAuthenticatedSession, |
| 13 | + getAllowedSessionOptions, |
| 14 | + getEffectiveSessionDuration, |
| 15 | + isAllowedSessionDuration, |
| 16 | +} from "~/services/sessionDuration.server"; |
| 17 | +import { getUserSession } from "~/services/sessionStorage.server"; |
| 18 | + |
| 19 | +const FormSchema = z.object({ |
| 20 | + sessionDuration: z.coerce.number().int().positive(), |
| 21 | +}); |
| 22 | + |
| 23 | +const REDIRECT_PATH = "/account/security"; |
| 24 | + |
| 25 | +async function redirectWithError(request: Request, message: string) { |
| 26 | + const messageSession = await getMessageSession(request.headers.get("cookie")); |
| 27 | + setErrorMessage(messageSession, message); |
| 28 | + return redirect(REDIRECT_PATH, { |
| 29 | + headers: { "Set-Cookie": await commitMessageSession(messageSession) }, |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +export async function action({ request }: ActionFunctionArgs) { |
| 34 | + const userId = await requireUserId(request); |
| 35 | + |
| 36 | + const formData = await request.formData(); |
| 37 | + const parsed = FormSchema.safeParse(Object.fromEntries(formData)); |
| 38 | + |
| 39 | + if (!parsed.success) { |
| 40 | + return redirectWithError(request, "Invalid session duration value."); |
| 41 | + } |
| 42 | + |
| 43 | + const { sessionDuration } = parsed.data; |
| 44 | + |
| 45 | + if (!isAllowedSessionDuration(sessionDuration)) { |
| 46 | + return redirectWithError(request, "Invalid session duration value."); |
| 47 | + } |
| 48 | + |
| 49 | + const { orgCapSeconds, userSettingSeconds } = await getEffectiveSessionDuration(userId); |
| 50 | + const allowed = getAllowedSessionOptions(orgCapSeconds, userSettingSeconds); |
| 51 | + if (!allowed.some((o) => o.value === sessionDuration)) { |
| 52 | + return redirectWithError( |
| 53 | + request, |
| 54 | + "Your organization's policy does not allow that session duration." |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + await prisma.user.update({ |
| 59 | + where: { id: userId }, |
| 60 | + data: { sessionDuration }, |
| 61 | + }); |
| 62 | + |
| 63 | + // Re-issue the cookie with the new maxAge and reset issuedAt so the user |
| 64 | + // gets a fresh window matching their new selection right away. |
| 65 | + const authSession = await getUserSession(request); |
| 66 | + const authCookie = await commitAuthenticatedSession(authSession, userId); |
| 67 | + |
| 68 | + const messageSession = await getMessageSession(request.headers.get("cookie")); |
| 69 | + setSuccessMessage(messageSession, "Session duration updated."); |
| 70 | + const messageCookie = await commitMessageSession(messageSession); |
| 71 | + |
| 72 | + const headers = new Headers(); |
| 73 | + headers.append("Set-Cookie", authCookie); |
| 74 | + headers.append("Set-Cookie", messageCookie); |
| 75 | + |
| 76 | + return redirect(REDIRECT_PATH, { headers }); |
| 77 | +} |
0 commit comments