Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
import NextAuth from "next-auth";
import { authOptions } from "@/lib/auth";
import { NextRequest } from "next/server";
import { cookies } from "next/headers";

const handler = NextAuth(authOptions);
const handler = async (req: NextRequest, ctx: { params: Promise<{ nextauth: string[] }> }) => {
let rememberMe = false;
const pathname = req.nextUrl.pathname;

if (req.method === "POST" && pathname.endsWith("/signin/credentials")) {
try {
const contentType = req.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
const clone = req.clone();
const body = await clone.json();
rememberMe = body.rememberMe === "true" || body.rememberMe === true;
} else {
const clone = req.clone();
const formData = await clone.formData();
const remVal = formData.get("rememberMe");
rememberMe = remVal === "true" || remVal === "on";
}
} catch (e) {
console.error("Error parsing form data in auth wrapper:", e);
}
} else if (pathname.includes("/callback/") && !pathname.endsWith("/callback/credentials")) {
// Default OAuth logins to be remembered (30 days)
rememberMe = true;
} else if (req.method === "POST" && pathname.endsWith("/signout")) {
const cookieStore = await cookies();
cookieStore.delete("remember-me");
} else {
const cookieStore = await cookies();
rememberMe = cookieStore.get("remember-me")?.value === "true";
}

const maxAge = rememberMe ? 30 * 24 * 60 * 60 : 24 * 60 * 60;

return NextAuth(req, ctx, {
...authOptions,
session: {
...authOptions.session,
maxAge,
},
});
};

export { handler as GET, handler as POST };
1 change: 1 addition & 0 deletions app/components/ScrollReveal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function ScrollReveal({ children, className, delay = 0 }: ScrollRevealPro

useEffect(() => {
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
// eslint-disable-next-line react-hooks/set-state-in-effect
setVisible(true);
return;
}
Expand Down
25 changes: 25 additions & 0 deletions lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Credentials from "next-auth/providers/credentials";
import { PrismaAdapter } from "@auth/prisma-adapter";
import bcrypt from "bcryptjs";
import type { NextAuthOptions } from "next-auth";
import { cookies } from "next/headers";

import prisma from "@/lib/prisma";
import { isUserSessionInvalidated } from "@/lib/sessionInvalidation";
Expand Down Expand Up @@ -59,6 +60,7 @@ export const authOptions: NextAuthOptions = {
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
rememberMe: { label: "Remember Me", type: "text" },
},

async authorize(credentials) {
Expand Down Expand Up @@ -94,6 +96,29 @@ events: {
},
},
callbacks: {
async signIn({ account, credentials }) {
const cookieStore = await cookies();
if (account?.provider === "credentials" && credentials) {
const rememberMe = credentials.rememberMe === "true" || credentials.rememberMe === true || credentials.rememberMe === "on";
cookieStore.set("remember-me", rememberMe ? "true" : "false", {
maxAge: rememberMe ? 30 * 24 * 60 * 60 : 24 * 60 * 60,
path: "/",
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
});
} else if (account?.provider && account.provider !== "credentials") {
// Default OAuth logins to be remembered (30 days)
cookieStore.set("remember-me", "true", {
maxAge: 30 * 24 * 60 * 60,
path: "/",
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
});
}
return true;
},
async jwt({ token, trigger, session, user, account, profile }) {
// Immediately invalidate token if user account was deleted
if (token.sub && (await isUserSessionInvalidated(token.sub))) {
Expand Down