roles-bot/apps/website/auth.config.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

import Discord from "@auth/core/providers/discord";
import { defineConfig } from "auth-astro";
2024-08-03 10:46:56 +02:00
import type { User } from "~/env";
2024-08-03 17:02:05 +02:00
import dotenv from "dotenv";
dotenv.config();
const { env } = process;
export default defineConfig({
2024-08-03 17:02:05 +02:00
secret: env.AUTH_SECRET,
2024-08-02 23:43:42 +02:00
providers: [
Discord({
2024-08-03 17:02:05 +02:00
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
2024-08-02 23:43:42 +02:00
authorization:
"https://discord.com/api/oauth2/authorize?scope=guilds+identify+email",
}),
],
callbacks: {
async jwt({ token, account, profile }) {
if (account && profile) {
token.accessToken = account.access_token;
token.id = profile.id;
2024-08-03 10:46:56 +02:00
token.name = profile.username as string;
token.global_name = profile.global_name;
2024-08-02 23:43:42 +02:00
}
return token;
},
async session({ session, token }) {
if (session.user) {
session.user.id = token.id as string;
2024-08-03 10:46:56 +02:00
(session.user as unknown as User).global_name =
token.global_name as string;
2024-08-02 23:43:42 +02:00
2024-08-03 14:42:34 +02:00
(session.user as unknown as User).discordAccessToken =
token.accessToken as string;
2024-08-02 23:43:42 +02:00
}
return session;
},
},
pages: {
signIn: "/auth/login",
signOut: "/auth/logout",
},
});