ChatGPT-Next-Web/app/auth.ts
DirkSchlossmacher c62d317504 auth log2
2023-11-13 09:12:43 +01:00

104 lines
2.4 KiB
TypeScript

import {
getServerSession,
type DefaultSession,
type NextAuthOptions,
} from "next-auth";
import AzureADProvider from "next-auth/providers/azure-ad";
/**
* 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 = {
callbacks: {
session: ({ session, token }) => ({
...session,
user: {
...session.user,
id: token.sub,
},
}),
},
providers: [
AzureADProvider({
clientId: process.env.AZURE_AD_CLIENT_ID ?? "",
clientSecret: process.env.AZURE_AD_CLIENT_SECRET ?? "",
tenantId: process.env.AZURE_AD_TENANT_ID ?? "",
}),
],
pages: {
signIn: "/login",
signOut: "/login",
error: "/login",
},
};
*/
export const authOptions: NextAuthOptions = {
callbacks: {
async session({ session, token }) {
console.log('Session callback');
console.log('Session:', session);
console.log('Token:', token);
console.log('User:', user);
console.log('Account:', account);
console.log('Profile:', profile);
const userId = token.sub ?? 'default-sub-value';
// Extend session object here
session.user = {
...session.user,
id: token.sub,
};
return session;
},
// Add other callbacks with async as needed
},
providers: [
AzureADProvider({
clientId: process.env.AZURE_AD_CLIENT_ID ?? "",
clientSecret: process.env.AZURE_AD_CLIENT_SECRET ?? "",
tenantId: process.env.AZURE_AD_TENANT_ID ?? "",
}),
],
pages: {
signIn: "/login",
signOut: "/login",
error: "/login",
},
// ... other options if any
};
/**
* 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);