forked from e2b-dev/fragments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
82 lines (75 loc) · 2.63 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
import NextAuth from 'next-auth';
import AzureAd from "next-auth/providers/azure-ad"
import type { NextAuthConfig, Session } from 'next-auth';
export const config = {
providers: [
AzureAd({
clientId: process.env.AUTH_MICROSOFT_ENTRA_ID_ID,
clientSecret: process.env.AUTH_MICROSOFT_ENTRA_ID_SECRET,
tenantId: process.env.AUTH_MICROSOFT_ENTRA_ID_TENANT_ID,
authorization: {
params: {
scope: "openid profile email offline_access",
},
},
})
],
callbacks: {
async jwt({ token, user, account }) {
if (account && user) {
return {
...token,
access_token: account.access_token,
issued_at: Date.now(),
expires_at: Date.now() + Number(account.expires_in) * 1000,
refresh_token: account.refresh_token,
sub: user.id,
};
} else if (Date.now() < Number(token.expires_at)) {
return token;
} else {
try {
const response = await fetch('https://login.microsoftonline.com/0c4da9c5-40ea-4e7d-9c7a-e7308d4f8e38/oauth2/v2.0/token', {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
client_id: process.env.AUTH_MICROSOFT_ENTRA_ID_ID as string,
client_secret: process.env.AUTH_MICROSOFT_ENTRA_ID_SECRET as string,
grant_type: 'refresh_token',
refresh_token: token.refresh_token as string,
}),
method: 'POST',
});
const tokens = await response.json();
if (!response.ok) throw tokens;
return {
...token,
access_token: tokens.access_token,
expires_at: Date.now() + Number(tokens.expires_in) * 1000,
refresh_token: tokens.refresh_token ?? token.refresh_token,
};
} catch (error) {
console.error('Error refreshing access token', error);
return { ...token, error: 'RefreshAccessTokenError' as const };
}
}
},
async session({ session, token }) {
return {
...session,
accessToken: String(token.access_token),
refreshToken: String(token.refresh_token),
accessTokenExpiresAt: Number(token.expires_at),
accessTokenIssuedAt: Number(token.issued_at),
userId: String(token.sub),
} satisfies EnrichedSession;
},
},
};
export interface EnrichedSession extends Session {
accessToken: string;
refreshToken: string;
accessTokenExpiresAt: number;
accessTokenIssuedAt: number;
userId: string;
}
export const { handlers, auth, signIn, signOut } = NextAuth(config);