aboutsummaryrefslogtreecommitdiff
path: root/web/src/lib/auth.js
blob: 3f78f947e984e175bef2ea0352cda12d736f82d5 (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
33
34
35
36
37
38
39
import NextAuth from "next-auth"
import Discord from "next-auth/providers/discord"

export const { handlers, signIn, signOut, auth } = NextAuth({
    providers: [Discord({
        authorization: {
            profile(profile) {
                return {
                    id: profile.id,
                    name: profile.username,
                    image: profile.image_url
                }
            },
            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;
        }
    }
})