DISCORD AUTH!, but some endpoints are broken

master
Patrick Cleavelin 2023-03-01 01:29:52 -06:00
parent 01dbdbe1f4
commit b9e9822ca6
4 changed files with 44 additions and 15 deletions

View File

@ -1,10 +1,12 @@
<script> <script>
import { member, intros } from './store.ts'; import { member, intros } from './store.ts';
const login = async (username) => { const authorizeUri = "https://discord.com/api/oauth2/authorize?client_id=577634620728934400&redirect_uri=http%3A%2F%2Flocalhost%3A5173%2Fauth&response_type=code&scope=identify%20guilds%20guilds.members.read";
await member._fakeLogin(username);
await intros.fetchIntros($member.guilds); const login = async (username) => {
window.location = authorizeUri;
//await member._fakeLogin(username);
//await intros.fetchIntros($member.guilds);
} }
let loginPromise = null; let loginPromise = null;
@ -13,5 +15,4 @@
</script> </script>
<p style="color:red;">You need to login first</p> <p style="color:red;">You need to login first</p>
<input bind:value={enteredUsername}>
<button on:click={() => loginPromise = login(enteredUsername)}>Login</button> <button on:click={() => loginPromise = login(enteredUsername)}>Login</button>

View File

@ -8,8 +8,8 @@
const apiAddIntro = async (guild, channel, username, selectedIntros) => { const apiAddIntro = async (guild, channel, username, selectedIntros) => {
for (const intro of selectedIntros) { for (const intro of selectedIntros) {
const response = await fetch( const response = await fetch(
`http://localhost:7756/intros/${guild}/${channel}/${$member.username}/${intro}`, `http://localhost:7756/intros/${guild}/${channel}/${intro}`,
{ method: 'POST' } { method: 'POST', headers: {"token": $member.token} }
); );
if (!response.ok) { if (!response.ok) {
const body = await response.json(); const body = await response.json();
@ -23,8 +23,8 @@
const apiRemoveIntro = async (guild, channel, username, selectedIntros) => { const apiRemoveIntro = async (guild, channel, username, selectedIntros) => {
for (const intro of selectedIntros) { for (const intro of selectedIntros) {
const response = await fetch( const response = await fetch(
`http://localhost:7756/intros/${guild}/${channel}/${$member.username}/${intro}/remove`, `http://localhost:7756/intros/${guild}/${channel}/${intro}/remove`,
{ method: 'POST' } { method: 'POST', headers: {"token": $member.token} }
); );
if (!response.ok) { if (!response.ok) {
const body = await response.json(); const body = await response.json();

View File

@ -0,0 +1,28 @@
<script>
import { onMount } from 'svelte';
import { page } from '$app/stores';
import { goto } from '$app/navigation';
import { member, intros } from '../../store.ts';
const code = $page.url.searchParams.get('code');
let loginFailed = false;
onMount(async () => {
const response = await fetch(`http://localhost:7756/auth?code=${code}`);
const body = await response.json();
if (!response.ok) {
loginFailed = true
} else {
await member.login(body.token);
await intros.fetchIntros($member.guilds);
goto('/')
}
});
</script>
{#if loginFailed}
<p style='color: red'>Login failed</p>
{/if}

View File

@ -20,17 +20,17 @@ function createMemberStore(): MemberStore {
subscribe: subscribe, subscribe: subscribe,
set: set, set: set,
addIntro: (intro: IntroIndex) => { update((n) => n.intros.push(intro)); return intro }, addIntro: (intro: IntroIndex) => { update((n) => n.intros.push(intro)); return intro },
_fakeLogin: async (username) => { login: async (token) => {
const response = (await (await fetch(`http://localhost:7756/me/${username}`)).json()) const response = (await (await fetch(
'http://localhost:7756/me',
{ headers: {"token": token} })).json())
if (response === "NoUserFound") { if (response === "NoUserFound") {
return false; return;
} else { } else {
set(response.Me) set({ token: token, ...response.Me })
return true;
} }
}, }
} }
} }