From 7e016ad147c4b1d016e519f13cf14b8f02d52685 Mon Sep 17 00:00:00 2001 From: QiTao Weng Date: Sun, 10 Apr 2022 00:25:44 -0500 Subject: [PATCH] Reformatting, removing auth0, add login status checks for redirects and logout button visibility, and fixing api call parameters --- src/App.vue | 50 ++++++++++++-- src/components/Login.vue | 143 ++++++++++++++++++++++++++++++--------- src/main.ts | 9 --- src/pages/Editor.vue | 34 +++++++++- src/pages/Scratch.vue | 65 +++++++++--------- src/support/auth.ts | 10 +++ 6 files changed, 232 insertions(+), 79 deletions(-) create mode 100644 src/support/auth.ts diff --git a/src/App.vue b/src/App.vue index 5544ba7..c61b401 100644 --- a/src/App.vue +++ b/src/App.vue @@ -4,10 +4,51 @@ import Home from "./pages/Home.vue"; // Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup import { MathStatement } from "./support/parse"; import { MathPage } from "./support/page"; -import { ref } from "vue"; +import { onMounted, ref } from "vue"; +import { checkLoggedIn, loggedOut, loggedIn } from "./support/auth"; +import router from "./router"; (window as any).Stmt = MathStatement; (window as any).Pg = MathPage; + +const status = ref(checkLoggedIn()) + +; (async () => { + const response = await fetch('/api/login/status/', { + method: 'GET', + headers: { + 'Accept': 'application/json', + }, + }) + const res = (await response.json()) as unknown as any + if (res.data.hasUser) { + loggedIn() + } + status.value = checkLoggedIn() + })() + +onMounted(() => { + status.value = checkLoggedIn() + console.log(status.value) +}) + +router.afterEach(() => { + status.value = checkLoggedIn() + console.log(status.value) +}) + +const logout = async () => { + const response = await fetch('/api/logout/', { + method: 'POST', + headers: { + 'Accept': 'application/json', + }, + }) + loggedOut() + status.value = checkLoggedIn() + router.push({path: '/'}) +} +