-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
135 lines (114 loc) · 3.47 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
125
126
127
128
129
130
131
132
133
134
135
import 'next-auth/jwt';
import NextAuth, { NextAuthConfig } from 'next-auth';
import Discord, { DiscordProfile } from 'next-auth/providers/discord';
import * as Sentry from '@sentry/nextjs';
import {
APIGuild,
OAuth2Scopes,
Routes,
RESTJSONErrorCodes,
} from 'discord-api-types/v10';
import { DiscordAPIError } from '@discordjs/rest';
import { discordBotClient, discordUserClient } from './discord';
import { serverConstants } from './config/constants.server';
declare module 'next-auth' {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface Profile extends DiscordProfile {}
interface User {
permissions: string;
}
}
declare module 'next-auth/jwt' {
/** Returned by the `jwt` callback and `auth`, when using JWT sessions */
// eslint-disable-next-line no-shadow
interface JWT {
id: string;
permissions: string;
}
}
export const config = {
debug: /\*|nextauth/.test(process.env.DEBUG ?? ''),
callbacks: {
async signIn({ account, profile }) {
if (!profile?.id) {
throw new Error('Discord user not found');
}
Sentry.setUser({
id: profile.id,
username: profile?.name ?? 'Unknown',
});
if (!account?.access_token) {
throw new Error('Access token not found');
}
const { guildId } = serverConstants.discord;
try {
await discordBotClient.get(Routes.guildMember(guildId, profile.id));
} catch (error) {
if (
error instanceof DiscordAPIError &&
error.code === RESTJSONErrorCodes.UnknownMember
) {
Sentry.addBreadcrumb({
data: {
id: profile.id,
username: profile.username,
},
category: 'Auth',
message: `User attempted to log in but was not found in the Discord server`,
level: 'error',
});
}
throw error;
}
return true;
},
/* eslint-disable no-param-reassign */
async jwt({ profile, token, account }) {
const { guildId } = serverConstants.discord;
if (profile?.id) {
token.id = profile.id;
}
if (account?.access_token) {
try {
const userGuildsResponse = (await discordUserClient(
account.access_token,
).get(Routes.userGuilds(), {
authPrefix: 'Bearer',
})) as APIGuild[];
const { permissions } =
userGuildsResponse.find(({ id }) => id === guildId) ?? {};
if (!permissions) {
throw new Error('No permissions found for user');
}
token.permissions = permissions;
} catch (error) {
console.error(error);
Sentry.captureException(error);
}
}
return token;
},
session({ session, token }) {
session.user.id = token.id;
session.user.permissions = token.permissions;
Sentry.setUser({
id: token.id,
username: token.name ?? undefined,
});
return session;
},
},
/* eslint-enable no-param-reassign */
logger: {
error: (error) => {
console.error(error);
Sentry.captureException(error);
},
},
providers: [
Discord<DiscordProfile>({
authorization: `https://discord.com/api/${Routes.oauth2Authorization()}?scope=${OAuth2Scopes.Identify}+${OAuth2Scopes.Guilds}+${OAuth2Scopes.GuildsMembersRead}`,
}),
],
} satisfies NextAuthConfig;
export const { handlers, signIn, signOut, auth } = NextAuth(config);