2025-01-21 22:50:16 +00:00
|
|
|
import 'next-auth/jwt';
|
2025-02-12 21:51:44 +00:00
|
|
|
import NextAuth, { DefaultSession } from "next-auth"
|
2025-01-16 23:24:13 +00:00
|
|
|
import GitHub from "next-auth/providers/github"
|
2025-02-10 22:31:38 +00:00
|
|
|
import Google from "next-auth/providers/google"
|
2025-01-16 23:24:13 +00:00
|
|
|
import { PrismaAdapter } from "@auth/prisma-adapter"
|
|
|
|
|
import { prisma } from "@/prisma";
|
2025-02-12 21:51:44 +00:00
|
|
|
import { AUTH_GITHUB_CLIENT_ID, AUTH_GITHUB_CLIENT_SECRET, AUTH_GOOGLE_CLIENT_ID, AUTH_GOOGLE_CLIENT_SECRET, AUTH_SECRET, AUTH_URL } from "./lib/environment";
|
2025-01-21 22:50:16 +00:00
|
|
|
import { User } from '@sourcebot/db';
|
2025-02-12 21:51:44 +00:00
|
|
|
import 'next-auth/jwt';
|
|
|
|
|
import type { Provider } from "next-auth/providers";
|
2025-01-21 22:50:16 +00:00
|
|
|
|
|
|
|
|
declare module 'next-auth' {
|
|
|
|
|
interface Session {
|
|
|
|
|
user: {
|
|
|
|
|
id: string;
|
|
|
|
|
} & DefaultSession['user'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
declare module 'next-auth/jwt' {
|
|
|
|
|
interface JWT {
|
2025-02-12 21:51:44 +00:00
|
|
|
userId: string
|
2025-01-21 22:50:16 +00:00
|
|
|
}
|
2025-02-12 21:51:44 +00:00
|
|
|
}
|
2025-01-16 23:24:13 +00:00
|
|
|
|
|
|
|
|
const providers: Provider[] = [
|
|
|
|
|
GitHub({
|
|
|
|
|
clientId: AUTH_GITHUB_CLIENT_ID,
|
|
|
|
|
clientSecret: AUTH_GITHUB_CLIENT_SECRET,
|
|
|
|
|
}),
|
2025-02-10 22:31:38 +00:00
|
|
|
Google({
|
|
|
|
|
clientId: AUTH_GOOGLE_CLIENT_ID!,
|
|
|
|
|
clientSecret: AUTH_GOOGLE_CLIENT_SECRET!,
|
|
|
|
|
})
|
2025-01-16 23:24:13 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// @see: https://authjs.dev/guides/pages/signin
|
|
|
|
|
export const providerMap = providers
|
|
|
|
|
.map((provider) => {
|
|
|
|
|
if (typeof provider === "function") {
|
|
|
|
|
const providerData = provider()
|
|
|
|
|
return { id: providerData.id, name: providerData.name }
|
|
|
|
|
} else {
|
|
|
|
|
return { id: provider.id, name: provider.name }
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.filter((provider) => provider.id !== "credentials");
|
|
|
|
|
|
2025-01-21 22:50:16 +00:00
|
|
|
|
2025-02-12 21:51:44 +00:00
|
|
|
const useSecureCookies = AUTH_URL.startsWith("https://");
|
|
|
|
|
const hostName = new URL(AUTH_URL).hostname;
|
2025-01-16 23:24:13 +00:00
|
|
|
|
|
|
|
|
export const { handlers, signIn, signOut, auth } = NextAuth({
|
|
|
|
|
secret: AUTH_SECRET,
|
|
|
|
|
adapter: PrismaAdapter(prisma),
|
|
|
|
|
session: {
|
|
|
|
|
strategy: "jwt",
|
|
|
|
|
},
|
2025-01-21 22:50:16 +00:00
|
|
|
callbacks: {
|
|
|
|
|
async jwt({ token, user: _user }) {
|
|
|
|
|
const user = _user as User | undefined;
|
|
|
|
|
// @note: `user` will be available on signUp or signIn triggers.
|
|
|
|
|
// Cache the userId in the JWT for later use.
|
|
|
|
|
if (user) {
|
|
|
|
|
token.userId = user.id;
|
|
|
|
|
}
|
|
|
|
|
return token;
|
|
|
|
|
},
|
|
|
|
|
async session({ session, token }) {
|
|
|
|
|
// @WARNING: Anything stored in the session will be sent over
|
|
|
|
|
// to the client.
|
|
|
|
|
session.user = {
|
|
|
|
|
...session.user,
|
|
|
|
|
// Propogate the userId to the session.
|
|
|
|
|
id: token.userId,
|
|
|
|
|
}
|
|
|
|
|
return session;
|
2025-02-12 21:51:44 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
cookies: {
|
|
|
|
|
sessionToken: {
|
|
|
|
|
name: `${useSecureCookies ? '__Secure-' : ''}authjs.session-token`,
|
|
|
|
|
options: {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: 'lax',
|
|
|
|
|
path: '/',
|
|
|
|
|
secure: useSecureCookies,
|
|
|
|
|
domain: `.${hostName}`
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
callbackUrl: {
|
|
|
|
|
name: `${useSecureCookies ? '__Secure-' : ''}authjs.callback-url`,
|
|
|
|
|
options: {
|
|
|
|
|
sameSite: 'lax',
|
|
|
|
|
path: '/',
|
|
|
|
|
secure: useSecureCookies,
|
|
|
|
|
domain: `.${hostName}`
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
csrfToken: {
|
|
|
|
|
name: `${useSecureCookies ? '__Host-' : ''}authjs.csrf-token`,
|
|
|
|
|
options: {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: 'lax',
|
|
|
|
|
path: '/',
|
|
|
|
|
secure: useSecureCookies,
|
|
|
|
|
domain: `.${hostName}`
|
|
|
|
|
}
|
2025-01-21 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
},
|
2025-01-16 23:24:13 +00:00
|
|
|
providers: providers,
|
|
|
|
|
pages: {
|
|
|
|
|
signIn: "/login"
|
|
|
|
|
}
|
2025-01-21 22:50:16 +00:00
|
|
|
});
|