Skip to content

Commit

Permalink
beginnings of auth
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Mar 13, 2024
1 parent 65ab277 commit c38c797
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/components/app.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
<script lang="ts" setup>
import { ref } from "vue";
import { getLoginToken } from "../lib/auth";
import {
getDiscordGuilds,
getDiscordToken,
getDiscordUser,
} from "../lib/discord";
import DiscordAvatar from "./discord-avatar.vue";
// make sure we're logged into discord
const token = getDiscordToken();
// get discord user info
const user = ref(await getDiscordUser(token));
// log into realm
getLoginToken(user.value, token);
// get list of guilds user is in
const guilds = ref(
(await getDiscordGuilds(token)).sort((a, b) => a.name.localeCompare(b.name)),
);
// TODO use login token to filter list of guilds to those shared w/ bot
</script>

<template>
Expand Down
21 changes: 21 additions & 0 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { User, getNewDiscordToken } from "./discord";

// @ts-ignore
const realmApi = import.meta.env.VITE_APP_REALM_API;

export const getLoginToken = async (user: User, token: string) => {
try {
return await fetch(`${realmApi}/auth/login`, {
body: JSON.stringify({
token: token,
user_id: user.id,
}),
headers: { "Content-Type": "application/json" },
method: "POST",
})
.then((r) => r.json())
.then((d) => d.token);
} catch (ex) {
getNewDiscordToken();
}
};
18 changes: 12 additions & 6 deletions src/lib/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ export interface User extends NamedThing {
username: string;
}

export const getDiscordToken = () => {
export const getNewDiscordToken = () => {
// @ts-ignore
const clientId = import.meta.env.VITE_APP_CLIENT_ID;
const oauthScope = ["identify", "guilds", "guilds.members.read"];
const redirectUri = window.location.href;

Cookies.remove("token");
window.location.assign(
`https://discord.com/oauth2/authorize?client_id=${clientId}&response_type=token&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${oauthScope.join("+")}`,
);

throw "Retrieving auth token";
};

export const getDiscordToken = () => {
const urlMatch = /\baccess_token=([^&]+)/i.exec(window.location.hash);
const urlToken = urlMatch ? urlMatch[1] : null;

Expand All @@ -31,11 +40,8 @@ export const getDiscordToken = () => {
const token = urlToken ?? Cookies.get("token");

if (!token) {
window.location.assign(
`https://discord.com/oauth2/authorize?client_id=${clientId}&response_type=token&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${oauthScope.join("+")}`,
);

throw "Retrieving auth token";
getNewDiscordToken();
return "";
}

return token;
Expand Down

0 comments on commit c38c797

Please sign in to comment.