-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
next-auth.ts
55 lines (46 loc) 路 1.45 KB
/
next-auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import NextAuth from 'next-auth';
import { getServerConfig } from '@/config/server';
import { ssoProviders } from './sso-providers';
const { NEXTAUTH_SECRET, ENABLE_OAUTH_SSO, SSO_PROVIDERS } = getServerConfig();
export const initSSOProviders = () => {
return ENABLE_OAUTH_SSO
? SSO_PROVIDERS.split(/[,锛宂/).map((provider) => {
const validProvider = ssoProviders.find((item) => item.id === provider);
if (validProvider) return validProvider.provider;
throw new Error(`[NextAuth] provider ${provider} is not supported`);
})
: [];
};
const nextAuth = NextAuth({
callbacks: {
// Note: Data processing order of callback: authorize --> jwt --> session
async jwt({ token, account }) {
// Auth.js will process the `providerAccountId` automatically
// ref: https://authjs.dev/reference/core/types#provideraccountid
if (account) {
token.userId = account.providerAccountId;
}
return token;
},
async session({ session, token }) {
// Pick userid from token
if (session.user) {
session.user.id = token.userId ?? session.user.id;
}
return session;
},
},
providers: initSSOProviders(),
secret: NEXTAUTH_SECRET,
trustHost: true,
});
export const {
handlers: { GET, POST },
auth,
} = nextAuth;
declare module '@auth/core/jwt' {
// Returned by the `jwt` callback and `auth`, when using JWT sessions
interface JWT {
userId?: string;
}
}