π The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
Refactor login page api fetch
Smaug67399084fe843c74
diff --git a/frontend/pages/login.vue b/frontend/pages/login.vueindex 4cfe102e..79873b48 100644--- a/frontend/pages/login.vue+++ b/frontend/pages/login.vue@@ -1,174 +1,153 @@<template>- <div class="container">- <form @submit.prevent="login" class="body">- <p class="signup-link">Need an account? <a href="mailto:[email protected]">Sign up</a></p>- <div class="form-group">- <label for="username">Username</label>- <input type="username" id="username" v-model="username" :class="{ 'is-invalid': errors.username }">- <p v-if="errors.username" class="invalid-feedback">{{ errors.username }}</p>- </div>- <div class="form-group">- <label for="password">Password</label>- <div class="password-input">- <input :type="showPassword ? 'text' : 'password'" id="password" v-model="password"- :class="{ 'is-invalid': errors.password }">- <button type="button" class="password-toggle" @click="showPassword = !showPassword">{{ showPassword ? 'Hide' :- 'Show' }}</button>- </div>- <p v-if="errors.password" class="invalid-feedback">{{ errors.password }}</p>- </div>- <button type="submit" class="btn">Login</button>- <p v-if="errors.general" class="invalid-feedback">{{ errors.general }}</p>- <p class="forgot-password-link">Forgot your password? <a href="mailto:[email protected]">Click here</a></p>- </form>- </div>+ <div class="container">+ <form @submit.prevent="login" class="body">+ <p class="signup-link">Need an account? <a href="mailto:[email protected]">Sign up</a></p>+ <div class="form-group">+ <label for="username">Username</label>+ <input type="username" id="username" v-model="username" :class="{ 'is-invalid': errors.username }" />+ <p v-if="errors.username" class="invalid-feedback">{{ errors.username }}</p>+ </div>+ <div class="form-group">+ <label for="password">Password</label>+ <div class="password-input">+ <input :type="showPassword ? 'text' : 'password'" id="password" v-model="password" :class="{ 'is-invalid': errors.password }" />+ <button type="button" class="password-toggle" @click="showPassword = !showPassword">{{ showPassword ? 'Hide' : 'Show' }}</button>+ </div>+ <p v-if="errors.password" class="invalid-feedback">{{ errors.password }}</p>+ </div>+ <button type="submit" class="btn">Login</button>+ <p v-if="errors.general" class="invalid-feedback">{{ errors.general }}</p>+ <p class="forgot-password-link">Forgot your password? <a href="mailto:[email protected]">Click here</a></p>+ </form>+ </div></template><script setup lang="ts">-const router = useRouter()-const username = ref('')+const router = useRouter();+const username = ref('');const errors = ref({- username: '',- password: '',- general: ''-})-const showPassword = ref(false)-const password = ref('')+ username: '',+ password: '',+ general: '',+});+const showPassword = ref(false);+const password = ref('');+const userStore = useUserStore();function login() {- if (!username.value) errors.value.username = 'Username is required'- else errors.value.username = '';+ if (!username.value) errors.value.username = 'Username is required';+ else errors.value.username = '';- if (!password.value) errors.value.password = 'Password is required'- else errors.value.password = '';+ if (!password.value) errors.value.password = 'Password is required';+ else errors.value.password = '';- if (username.value && password.value) {- connect(username.value, password.value);- }+ if (username.value && password.value) {+ connect(username.value, password.value);+ }}async function connect(username: string, password: string) {- const content = JSON.stringify({- username: username,- password: password- });- const responce = await fetch(`${import.meta.env.VITE_BASE_API}/api/v1/auth`, {- method: "POST",- headers: {- Accept: "application/json",- "Content-Type": "application/json",- },- body: content,- credentials: "include",- });-- const result = await responce.json();- if (result.status == "success" && result.result.auth) {- logIn();- router.push("/dashboard");- }- else errors.value.general = result.message;-+ const result = await userStore.login(username, password);+ if (result == true) router.push('/dashboard');+ else errors.value.general = String(result);}</script><style scoped lang="scss">.container {- display: flex;- flex-direction: column;- align-items: center;- height: 100%;- justify-content: center;+ display: flex;+ flex-direction: column;+ align-items: center;+ height: 100%;+ justify-content: center;}.body {- display: flex;- flex-direction: column;- align-items: center;- width: 100%;- max-width: 620px;- padding: 3rem;- background-color: var(--bg-contrast);- border-radius: 15px;+ display: flex;+ flex-direction: column;+ align-items: center;+ width: 100%;+ max-width: 620px;+ padding: 3rem;+ background-color: var(--bg-contrast);+ border-radius: 15px;}.form-group {- display: flex;- flex-direction: column;- width: 100%;- margin-bottom: 1rem;+ display: flex;+ flex-direction: column;+ width: 100%;+ margin-bottom: 1rem;}.signup-link {- font-size: 0.8rem;- margin-top: -0.5rem;- margin-bottom: 1rem;- text-align: right;- text-decoration: none;-- &:hover {- text-decoration: underline;- }+ font-size: 0.8rem;+ margin-top: -0.5rem;+ margin-bottom: 1rem;+ text-align: right;+ text-decoration: none;++ &:hover {+ text-decoration: underline;+ }}-.password-input {- position: relative;+ position: relative;}.password-toggle {- position: absolute;- top: 50%;- right: 0;- transform: translateY(-50%);- font-size: 0.8rem;- padding: 0.2rem;- background-color: transparent;- border: none;- cursor: pointer;- outline: none;-- &:hover {- text-decoration: underline;- }-+ position: absolute;+ top: 50%;+ right: 0;+ transform: translateY(-50%);+ font-size: 0.8rem;+ padding: 0.2rem;+ background-color: transparent;+ border: none;+ cursor: pointer;+ outline: none;++ &:hover {+ text-decoration: underline;+ }}.btn {- font-size: 1.2rem;- border-radius: 50px;- width: 100%;- background-color: $primary-300;-- &:hover {- background: $primary-500;- transform: none;- }+ font-size: 1.2rem;+ border-radius: 50px;+ width: 100%;+ background-color: $primary-300;++ &:hover {+ background: $primary-500;+ transform: none;+ }}input {- background: var(--bg-contrast-2);- outline: none;+ background: var(--bg-contrast-2);+ outline: none;}.is-invalid {- border-color: $red !important;+ border-color: $red !important;}.invalid-feedback {- font-size: 0.8rem;- color: $red;+ font-size: 0.8rem;+ color: $red;}.forgot-password-link {- margin-top: 20px;- text-align: center;- font-size: 14px;+ margin-top: 20px;+ text-align: center;+ font-size: 14px;- a {- text-decoration: underline;- }+ a {+ text-decoration: underline;+ }- a:hover {- text-decoration: none;- }+ a:hover {+ text-decoration: none;+ }}-</style>\ No newline at end of file+</style>diff --git a/frontend/stores/_utils.ts b/frontend/stores/_utils.tsindex 92337215..a00ed332 100644--- a/frontend/stores/_utils.ts+++ b/frontend/stores/_utils.ts@@ -42,14 +42,6 @@ export async function makeRequest<T>(route: string, method: string, body: Object}}-export function logIn() {- if (!import.meta.client) return;- localStorage.setItem('isLoggedIn', 'true');-}-export function logOut() {- if (!import.meta.client) return;- localStorage.removeItem('isLoggedIn');-}function treatQueue(access_token: boolean = true) {console.log(`[API] Treating queue with access_token: ${access_token}`);@@ -57,7 +49,7 @@ function treatQueue(access_token: boolean = true) {for (const request of requestQueue) {request.resolve({ status: 'error', message: 'Failed to fetch.' });}- logOut();+ useUserStore().logout();navigateTo('/login');} else {for (const request of requestQueue) {diff --git a/frontend/stores/user.store.ts b/frontend/stores/user.store.tsindex 03b522c5..df1b51fa 100644--- a/frontend/stores/user.store.ts+++ b/frontend/stores/user.store.ts@@ -8,6 +8,20 @@ export const useUserStore = defineStore('user', {last_connection: null as ConnectionLog | null,}),actions: {+ async login(username: string, password: string) {+ try {+ const response = await makeRequest<{ auth: boolean }>('auth', 'POST', { username, password });+ if (response.status == 'success' && response.result?.auth) {+ if (import.meta.client) localStorage.setItem('isLoggedIn', 'true');+ return true;+ }+ } catch (e) {+ return e;+ }+ },+ logout() {+ if (import.meta.client) localStorage.removeItem('isLoggedIn');+ },fetch() {return new Promise(async (resolve, reject) => {const responce = await makeRequest<{ user: User; last_connection: ConnectionLog }>(`users/@me`, 'GET', {});