mirror of
https://github.com/fergalmoran/kidarr-server.git
synced 2025-12-30 05:18:49 +00:00
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { DrizzleAdapter } from "@auth/drizzle-adapter";
|
|
import {
|
|
getServerSession,
|
|
type DefaultSession,
|
|
type NextAuthOptions,
|
|
} from "next-auth";
|
|
import GoogleProvider from "next-auth/providers/google";
|
|
|
|
import { env } from "@/env";
|
|
import { db } from "@/server/db";
|
|
import { Adapter } from "next-auth/adapters";
|
|
/**
|
|
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
|
|
* object and keep type safety.
|
|
*
|
|
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
|
|
*/
|
|
declare module "next-auth" {
|
|
interface Session extends DefaultSession {
|
|
user: {
|
|
id: string;
|
|
// ...other properties
|
|
// role: UserRole;
|
|
} & DefaultSession["user"];
|
|
}
|
|
|
|
// interface User {
|
|
// // ...other properties
|
|
// // role: UserRole;
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
|
|
*
|
|
* @see https://next-auth.js.org/configuration/options
|
|
*/
|
|
export const authOptions: NextAuthOptions = {
|
|
adapter: DrizzleAdapter(db) as Adapter,
|
|
callbacks: {
|
|
session: async ({ session, token }) => {
|
|
if (session?.user) {
|
|
session.user.id = token.sub ?? "";
|
|
}
|
|
return session;
|
|
},
|
|
jwt: async ({ user, token }) => {
|
|
if (user) {
|
|
token.uid = user.id;
|
|
}
|
|
return token;
|
|
},
|
|
},
|
|
session: {
|
|
strategy: "jwt",
|
|
},
|
|
providers: [
|
|
GoogleProvider({
|
|
clientId: env.GOOGLE_CLIENT_ID,
|
|
clientSecret: env.GOOGLE_CLIENT_SECRET,
|
|
}),
|
|
],
|
|
};
|
|
|
|
/**
|
|
* Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
|
|
*
|
|
* @see https://next-auth.js.org/configuration/nextjs
|
|
*/
|
|
export const getServerAuthSession = () => getServerSession(authOptions);
|