-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathauth.ts
More file actions
44 lines (42 loc) · 1.28 KB
/
Copy pathauth.ts
File metadata and controls
44 lines (42 loc) · 1.28 KB
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
import NextAuth from 'next-auth';
import type { Session } from 'next-auth';
import GitHub from 'next-auth/providers/github';
import { encryptToken } from '@/lib/crypto';
/** Shapes the session payload returned to clients — never includes token material. */
export function buildClientSession(
session: Session,
token: { ghToken?: unknown; username?: string }
): Session {
session.hasGitHubToken = Boolean(token.ghToken);
if (token.username) {
if (!session.user) {
session.user = {};
}
session.user.username = token.username;
}
return session;
}
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [
GitHub({
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
authorization: { params: { scope: 'read:user public_repo' } },
}),
],
session: { strategy: 'jwt' },
callbacks: {
async jwt({ token, account, profile }) {
if (account?.access_token) {
token.ghToken = await encryptToken(account.access_token);
}
if (profile && 'login' in profile && typeof profile.login === 'string') {
token.username = profile.login;
}
return token;
},
async session({ session, token }) {
return buildClientSession(session, token);
},
},
});