Reformatting, removing auth0, add login status checks for redirects and logout button visibility, and fixing api call parameters

This commit is contained in:
QiTao Weng 2022-04-10 00:25:44 -05:00
parent 5dc568bc86
commit 7e016ad147
6 changed files with 232 additions and 79 deletions

View File

@ -4,10 +4,51 @@ import Home from "./pages/Home.vue";
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup // Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import { MathStatement } from "./support/parse"; import { MathStatement } from "./support/parse";
import { MathPage } from "./support/page"; 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).Stmt = MathStatement;
(window as any).Pg = MathPage; (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: '/'})
}
</script> </script>
<template> <template>
@ -17,15 +58,14 @@ import { ref } from "vue";
<q-toolbar-title> <q-toolbar-title>
<q-avatar> <q-avatar>
<img src="https://cdn.quasar.dev/logo-v2/svg/logo-mono-white.svg" /> <img src="https://cdn.quasar.dev/logo-v2/svg/logo-mono-white.svg" />
</q-avatar> </q-avatar>Title
Title
</q-toolbar-title> </q-toolbar-title>
</q-toolbar> </q-toolbar>
<q-tabs align="left"> <q-tabs>
<q-route-tab to="/Scratch" label="Scratch" /> <q-route-tab to="/Scratch" label="Scratch" />
<q-route-tab to="/Editor" label="Editor" /> <q-route-tab to="/Editor" label="Editor" />
<q-tab v-if="status" @click="logout()" label="Logout" />
</q-tabs> </q-tabs>
</q-header> </q-header>

View File

@ -1,12 +1,15 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue' import { onMounted, ref } from 'vue'
import validator from 'validator' import validator from 'validator'
import { checkLoggedIn, loggedIn } from '../support/auth'
import router from '../router'
const name = ref<string>('') const name = ref<string>('')
const email = ref<string>('') const email = ref<string>('')
const password = ref<string>('') const password = ref<string>('')
const passwordConfirm = ref<string>('') const passwordConfirm = ref<string>('')
const btnDisabled = ref<boolean>(true) const btnDisabled = ref<boolean>(true)
const isRegistration = ref<boolean>(false) const isRegistration = ref<boolean>(false)
const error = ref<string>('')
const checkForm = () => { const checkForm = () => {
btnDisabled.value = btnDisabled.value =
@ -17,10 +20,59 @@ const checkForm = () => {
|| email.value.length == 0 || email.value.length == 0
|| !validator.isEmail(email.value); || !validator.isEmail(email.value);
} }
const submit = async () => {
if (isRegistration.value == true) {
error.value = await register()
} else {
error.value = await login()
}
}
const login = async () => {
const response = await fetch('/api/login/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ email: email.value, password: password.value })
})
const user = (await response.json()) as unknown as any
if (!user.success) {
return user.message
}
loggedIn()
router.push({ path: '/Editor'})
}
const register = async () => {
const response = await fetch('/api/register', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ name:name.value, email:email.value, password:password.value })
})
const user = (await response.json()) as unknown as any
if (!user.success) {
return user.message
}
loggedIn()
router.push({ path: '/Editor'})
}
onMounted(() => {
if ( checkLoggedIn() ) {
router.push({ path: '/Editor'})
}
})
</script> </script>
<template> <template>
<q-card-section v-if="!isRegistration"> <q-card-section v-if="!isRegistration">
<q-form class="q-gutter-md"> <q-form class="q-gutter-md">
<q-input v-model="email" v-on:keyup="checkForm()" type="email" label="email" /> <q-input v-model="email" v-on:keyup="checkForm()" type="email" label="email" />
@ -32,16 +84,43 @@ const checkForm = () => {
<q-input v-model="name" v-on:keyup="checkForm()" type="text" label="name" /> <q-input v-model="name" v-on:keyup="checkForm()" type="text" label="name" />
<q-input v-model="email" v-on:keyup="checkForm()" type="email" label="email" /> <q-input v-model="email" v-on:keyup="checkForm()" type="email" label="email" />
<q-input v-model="password" v-on:keyup="checkForm()" type="password" label="password" /> <q-input v-model="password" v-on:keyup="checkForm()" type="password" label="password" />
<q-input v-model="passwordConfirm" v-on:keyup="checkForm()" type="password" label="password confirmation" /> <q-input
v-model="passwordConfirm"
v-on:keyup="checkForm()"
type="password"
label="password confirmation"
/>
</q-form> </q-form>
</q-card-section> </q-card-section>
<q-card-section v-if="error">
<p class="error">
<b>{{ error }}</b>
</p>
</q-card-section>
<q-card-actions class="q-px-md"> <q-card-actions class="q-px-md">
<q-btn unelevated color="primary" size="lg" :disable=btnDisabled class="full-width" :label="isRegistration? 'Register' : 'Login'" /> <q-btn
unelevated
color="primary"
size="lg"
:disable="btnDisabled"
class="full-width"
:label="isRegistration ? 'Register' : 'Login'"
@click="submit()"
/>
</q-card-actions> </q-card-actions>
<q-card-section class="text-center q-pa-none"> <q-card-section class="text-center q-pa-none">
<p class="text-grey-6">
<p class="text-grey-6">{{isRegistration? 'Have an account?' :'Not reigistered?'}} <a href="#" @click="isRegistration = !isRegistration; checkForm()">{{isRegistration? 'Login':'Created an Account'}}</a></p> {{ isRegistration ? 'Have an account?' : 'Not reigistered?' }}
<a
href="#"
@click="isRegistration = !isRegistration; checkForm()"
>{{ isRegistration ? 'Login' : 'Created an Account' }}</a>
</p>
</q-card-section> </q-card-section>
</template> </template>
<style>
.error {
color: red;
}
</style>

View File

@ -32,7 +32,6 @@ import 'quasar/src/css/index.sass'
-------------------------------------------------- --------------------------------------------------
*/ */
import { DraggablePlugin } from '@braks/revue-draggable' import { DraggablePlugin } from '@braks/revue-draggable'
import { createAuth0 } from '@auth0/auth0-vue'
import 'katex/dist/katex.min.css' import 'katex/dist/katex.min.css'
import 'katex/dist/contrib/auto-render.min' import 'katex/dist/contrib/auto-render.min'
@ -50,14 +49,6 @@ app.use(Quasar, {
plugins: {}, // import Quasar plugins and add here plugins: {}, // import Quasar plugins and add here
}) })
app.use(
createAuth0({
domain: 'dev-ge84r-eu.us.auth0.com',
client_id: 'zHjZGg1uPws0DkQg5bRdKcDX8m6AuTZl', // eslint-disable-line camelcase
redirect_uri: window.location.origin + '/api/login/callback', // eslint-disable-line camelcase
}),
)
app.use(router) app.use(router)
app.use(DraggablePlugin) app.use(DraggablePlugin)

View File

@ -12,6 +12,8 @@ import ExpressionEditor from './ExpressionEditor.vue'
import TextBox from '../components/TextBox.vue' import TextBox from '../components/TextBox.vue'
import {RichTextBox} from "../support/types"; import {RichTextBox} from "../support/types";
import { stepX, stepY } from "../support/const"; import { stepX, stepY } from "../support/const";
import { checkLoggedIn, loggedOut } from '../support/auth'
import router from '../router'
const math = new MathPage(uuidv4()); const math = new MathPage(uuidv4());
const statements = ref<MathStatement[]>([]); const statements = ref<MathStatement[]>([]);
@ -78,7 +80,9 @@ const updateStatements = () => {
statementsKey.value = uuidv4(); statementsKey.value = uuidv4();
}; };
onMounted(updateStatements) onMounted(() => {
updateStatements()
})
const saveNewVariable = (stmt: MathStatement) => { const saveNewVariable = (stmt: MathStatement) => {
math.addStatement(stmt); math.addStatement(stmt);
@ -123,6 +127,31 @@ function richUpdateValue() {
const removeRichTextBox = (id: number) => { const removeRichTextBox = (id: number) => {
richTextStatements.value.splice(id, 1); richTextStatements.value.splice(id, 1);
}; };
/*
Auth
*/
const status = ref(checkLoggedIn())
const logout = async () => {
const response = await fetch('/api/logout/', {
method: 'POST',
headers: {
'Accept': 'application/json',
},
})
loggedOut()
status.value = checkLoggedIn()
router.push('/')
}
onMounted(() => {
status.value = checkLoggedIn()
console.log(status.value)
if (status.value == false ) {
router.push({path: '/'})
}
})
</script> </script>
<template> <template>
@ -138,9 +167,10 @@ const removeRichTextBox = (id: number) => {
Title Title
</q-toolbar-title> </q-toolbar-title>
</q-toolbar> </q-toolbar>
<q-tabs align="left"> <q-tabs>
<q-route-tab to="/Scratch" label="Scratch" /> <q-route-tab to="/Scratch" label="Scratch" />
<q-route-tab to="/Editor" label="Editor" /> <q-route-tab to="/Editor" label="Editor" />
<q-tab v-if="status" @click="logout()" label="Logout" />
</q-tabs> </q-tabs>
</q-header> </q-header>

View File

@ -5,6 +5,8 @@
import { MathStatement } from '../support/parse' import { MathStatement } from '../support/parse'
import { onMounted, ref } from 'vue' import { onMounted, ref } from 'vue'
import Katex from '../components/Katex.vue' import Katex from '../components/Katex.vue'
import { checkLoggedIn, loggedOut } from '../support/auth'
import router from '../router'
const page = new MathPage(uuidv4()) const page = new MathPage(uuidv4())
const stmt1Id = page.addRaw('x = y+3/4') const stmt1Id = page.addRaw('x = y+3/4')
@ -35,6 +37,7 @@
key.value = uuidv4() key.value = uuidv4()
} }
} }
</script> </script>
<template> <template>

10
src/support/auth.ts Normal file
View File

@ -0,0 +1,10 @@
let isLoggedIn = false
export const checkLoggedIn = () => {
return isLoggedIn
}
export const loggedIn = () => {
isLoggedIn = true
}
export const loggedOut = () => {
isLoggedIn = false
}