diff --git a/frontend/src/includes/Auth.svelte b/frontend/src/includes/Auth.svelte index 87634b3..45971e3 100644 --- a/frontend/src/includes/Auth.svelte +++ b/frontend/src/includes/Auth.svelte @@ -19,8 +19,8 @@ } export function redirectLogin() { - // TODO: State - window.location.href = `https://discordapp.com/oauth2/authorize?response_type=code&redirect_uri=${OAUTH.redirectUri}&scope=identify%20guilds&client_id=${OAUTH.clientId}&state=` + let state = btoa(new URL(window.location.href).pathname); + window.location.href = `https://discordapp.com/oauth2/authorize?response_type=code&redirect_uri=${OAUTH.redirectUri}&scope=identify%20guilds&client_id=${OAUTH.clientId}&state=${state}`; } export function clearLocalStorage() { diff --git a/frontend/src/views/LoginCallback.svelte b/frontend/src/views/LoginCallback.svelte index 3f6b59b..845d3ea 100644 --- a/frontend/src/views/LoginCallback.svelte +++ b/frontend/src/views/LoginCallback.svelte @@ -3,6 +3,10 @@ import {redirectLogin, setToken} from '../includes/Auth.svelte' import {API_URL} from "../js/constants"; import {errorPage} from '../js/util' + import {navigateTo} from "svelte-router-spa"; + + export let currentRoute; + let state = currentRoute.queryParams.state; async function process() { const code = new URLSearchParams(window.location.search).get('code') @@ -18,15 +22,30 @@ return } - setToken(res.data.token) - window.location.href = '/' + setToken(res.data.token); + + let path = '/'; + + try { + if (state !== undefined && state.length > 0) { + path = atob(state); + + if (path === '/callback') { + path = '/'; + } + } + } catch (e) { + console.log(`Error parsing state: ${e}`) + } finally { + navigateTo(path); + } } process() \ No newline at end of file