-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
124 lines (113 loc) · 3.93 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import NextAuth from "next-auth";
import type { JWT } from "next-auth/jwt";
async function refreshAccessToken(token: JWT) {
try {
const response = await fetch(`${process.env.LOGTO_URL}/oidc/token`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
client_id: process.env.LOGTO_CLIENT_ID || "",
client_secret: process.env.LOGTO_CLIENT_SECRET || "",
grant_type: "refresh_token",
refresh_token: token.refreshToken as string,
}),
});
const refreshedTokens = await response.json();
if (!response.ok) {
throw refreshedTokens;
}
return {
...token,
accessToken: refreshedTokens.access_token,
accessTokenExpires: Date.now() + refreshedTokens.expires_in * 1000,
refreshToken: refreshedTokens.refresh_token ?? token.refreshToken, // Fall back to old refresh token
};
} catch (error) {
console.log(error);
return {
...token,
error: "RefreshAccessTokenError",
};
}
}
export const { handlers, signIn, signOut, auth } = NextAuth({
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async jwt({ token, account, user }) {
if (account && user) {
return {
accessToken: account.access_token,
accessTokenExpires: account.expires_at
? account.expires_at * 1000
: undefined,
refreshToken: account.refresh_token,
user,
};
}
if (Date.now() < (token.accessTokenExpires as number)) {
return token;
}
return refreshAccessToken(token);
},
async session({ session, token }) {
if (token.error) {
return { ...session, error: token.error };
}
try {
const response = await fetch(
`${process.env.LOGTO_URL}/oidc/me`,
{
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
},
);
if (response.ok) {
const userInfo = await response.json();
session.user = {
id: userInfo.sub,
name: userInfo.name ?? userInfo.username,
email: userInfo.email,
image: userInfo.picture,
emailVerified: userInfo.email_verified,
};
} else {
console.error(
"Failed to fetch user info:",
response.statusText,
);
}
} catch (error) {
console.error("Error fetching user info:", error);
}
(session as any).accessToken = token.accessToken;
return session;
},
},
providers: [
{
id: "logto",
name: "Logto",
type: "oidc",
issuer: `${process.env.LOGTO_URL}/oidc`,
authorization: {
params: {
scope: "openid offline_access profile email identities",
},
},
clientId: process.env.LOGTO_CLIENT_ID,
clientSecret: process.env.LOGTO_CLIENT_SECRET,
token: true,
profile(profile) {
return {
id: profile.sub,
name: profile.name ?? profile.username,
email: profile.email,
image: profile.picture,
};
},
},
],
});