aboutsummaryrefslogtreecommitdiff
path: root/web/src/lib/auth.js
blob: 28c34f10902a0681d7b9631e2771df1e5da4700b (plain) (blame)
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
import NextAuth from "next-auth"
import Discord from "next-auth/providers/discord"

export const { handlers, signIn, signOut, auth } = NextAuth({
    providers: [Discord({
        authorization: {
            url: "https://discord.com/api/oauth2/authorize",
            params: { scope: "identify guilds" },
        }
    })],
    callbacks: {
        async jwt({ token, account }) {
            // Persist the OAuth access_token to the token right after sign in
            if (account) {
                token.accessToken = account.access_token;
            }
            return token;
        },
        async session({ session, token }) {
            if (token.accessToken) {
                session.user = await fetch('https://discord.com/api/users/@me', {
                    headers: {
                        authorization: `Bearer ${token.accessToken}`
                    }
                }).then((r) => r.json());
                session.accessToken = token.accessToken;
            }

            return session;
        }
    }
})