mirror of
https://github.com/xHyroM/roles-bot.git
synced 2024-11-10 03:08:06 +01:00
feat: fetch guilds
This commit is contained in:
parent
7adb5cc016
commit
d018fbb1d6
3 changed files with 53 additions and 20 deletions
|
@ -2,22 +2,42 @@ import Discord from "@auth/core/providers/discord";
|
||||||
import { defineConfig } from "auth-astro";
|
import { defineConfig } from "auth-astro";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
providers: [
|
providers: [
|
||||||
Discord({
|
Discord({
|
||||||
clientId: import.meta.env.DISCORD_CLIENT_ID,
|
clientId: import.meta.env.DISCORD_CLIENT_ID,
|
||||||
clientSecret: import.meta.env.DISCORD_CLIENT_SECRET,
|
clientSecret: import.meta.env.DISCORD_CLIENT_SECRET,
|
||||||
}),
|
authorization:
|
||||||
],
|
"https://discord.com/api/oauth2/authorize?scope=guilds+identify+email",
|
||||||
callbacks: {
|
}),
|
||||||
session({ session, token }) {
|
],
|
||||||
if (session.user && token?.sub) {
|
callbacks: {
|
||||||
session.user.id = token.sub;
|
async jwt({ token, account, profile }) {
|
||||||
}
|
if (account && profile) {
|
||||||
return session;
|
token.accessToken = account.access_token;
|
||||||
},
|
token.id = profile.id;
|
||||||
},
|
}
|
||||||
pages: {
|
|
||||||
signIn: "/auth/login",
|
return token;
|
||||||
signOut: "/auth/logout",
|
},
|
||||||
},
|
async session({ session, token }) {
|
||||||
|
if (session.user) {
|
||||||
|
session.user.id = token.id as string;
|
||||||
|
|
||||||
|
const guilds = await fetch("https://discord.com/api/users/@me/guilds", {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token.accessToken as string}`,
|
||||||
|
"Cache-Control": "max-age=300",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
session.user.guilds = await guilds.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
return session;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pages: {
|
||||||
|
signIn: "/auth/login",
|
||||||
|
signOut: "/auth/logout",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
8
apps/website/src/env.d.ts
vendored
8
apps/website/src/env.d.ts
vendored
|
@ -1 +1,9 @@
|
||||||
/// <reference types="astro/client" />
|
/// <reference types="astro/client" />
|
||||||
|
|
||||||
|
import type { User as AuthCoreUser } from "@auth/core/types";
|
||||||
|
|
||||||
|
export type User = AuthCoreUser & {
|
||||||
|
guilds: {
|
||||||
|
name: string;
|
||||||
|
}[];
|
||||||
|
};
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
---
|
---
|
||||||
|
import type { User } from "~/env";
|
||||||
import { getSession } from "auth-astro/server";
|
import { getSession } from "auth-astro/server";
|
||||||
|
|
||||||
const session = await getSession(Astro.request);
|
const session = await getSession(Astro.request);
|
||||||
if (!session) {
|
if (!session || !session.user) {
|
||||||
return Astro.redirect("/auth/login");
|
return Astro.redirect("/auth/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const user = session.user as User;
|
||||||
|
console.log(user.guilds[0]);
|
||||||
---
|
---
|
||||||
|
|
||||||
<p>Welcome {session.user?.name}</p>
|
<p>Welcome {user.name}</p>
|
||||||
|
{user.guilds.map((g) => g.id).join("\n\n")}
|
||||||
|
|
Loading…
Reference in a new issue