// Authentication
import { useAuthStore } from '@/stores/auth'
const auth = useAuthStore()
+
+ // Import utils
+ import { deleteAccessToken } from "@/utils/auth";
+
+ function logout() {
+ // Drop all stored authentication data
+ auth.logout();
+
+ // Drop the current access token
+ deleteAccessToken();
+ }
</script>
<template>
</a>
<div class="navbar-dropdown is-boxed">
- <a class="navbar-item" href="#" @click.prevent="auth.logout">
+ <a class="navbar-item" href="#" @click.prevent="logout">
{{ $t("Log Out") }}
</a>
</div>
})
)
-app.mount('#app')
+// Authentication
+import { useAuthStore } from '@/stores/auth';
+const auth = useAuthStore();
+
+// Fetch authentication credentials and the mount the app
+auth.restore().finally(() => {
+ app.mount("#app")
+});
import { defineStore } from 'pinia'
import type { User } from "@/types/User"
+import { fetchCurrentUser } from "@/utils/auth"
interface AuthState {
user: User | null;
this.user = null;
this.isLoggedIn = false;
},
+
+ async restore() {
+ const user: User = await fetchCurrentUser();
+ if (user)
+ this.setUser(user);
+ },
},
});
localStorage.setItem("token", token)
}
+export function deleteAccessToken(): void {
+ localStorage.removeItem("token")
+}
+
/*
Takes username and password and logs in the user
*/