This should allow us to re-use the code better.
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
import { RouterLink, RouterView } from "vue-router"
// 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();
- }
+ import { useAuth } from "@/composables/auth";
+ const auth = useAuth();
</script>
<template>
</div>
<div class="navbar-end">
- <RouterLink to="/login" class="navbar-item" v-if="!auth.isLoggedIn">
+ <RouterLink to="/login" class="navbar-item" v-if="!auth.isLoggedIn.value">
{{ $t("Login") }}
</RouterLink>
<div v-else class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link" href="#">
- {{ auth.user?.name }}
+ {{ auth.user.value?.name }}
</a>
<div class="navbar-dropdown is-boxed">
- <a class="navbar-item" href="#" @click.prevent="logout">
+ <a class="navbar-item" href="#" @click.prevent="auth.logout">
{{ $t("Log Out") }}
</a>
</div>
Takes username and password and logs in the user
*/
-export async function login(username: string, password: string): Promise<User> {
+export async function authenticateUser(username: string, password: string): Promise<User> {
const response = await fetch("/api/v1/auth/user", {
method : "POST",
--- /dev/null
+import { computed } from "vue";
+
+// Import types
+import type { User } from "@/types/User";
+
+// Authentication Store
+import { useAuthStore } from "@/stores/auth";
+
+// Import API
+import { authenticateUser, deleteAccessToken } from "@/api/auth";
+
+export function useAuth() {
+ const store = useAuthStore();
+
+ // Logs in the user
+ async function login(username: string, password: string) {
+ const user: User = await authenticateUser(username, password);
+ if (user)
+ store.setUser(user);
+ }
+
+ // Logs out the user
+ async function logout() {
+ // Drop all stored authentication data
+ store.logout();
+
+ // Drop the current access token
+ deleteAccessToken();
+ }
+
+ return {
+ // Methods
+ login : login,
+ logout : logout,
+
+ // Store properties
+ user : computed(() => store.user),
+ isLoggedIn : computed(() => store.isLoggedIn),
+ }
+}
import { defineStore } from 'pinia'
import type { User } from "@/types/User"
-import { fetchCurrentUser } from "@/utils/auth"
+import { fetchCurrentUser } from "@/api/auth"
interface AuthState {
user: User | null;
import { useRouter } from "vue-router"
const router = useRouter()
- // Import types
- import type { User } from "@/types/User"
-
- // Import utils
- import { login } from "@/utils/auth"
+ // Import composables
+ import { useAuth } from "@/composables/auth";
+ const auth = useAuth();
// Import components
import Notification from "../components/Notification.vue"
- // Authentication Store
- import { useAuthStore } from '@/stores/auth';
- const auth = useAuthStore();
-
// Error string shown to the user in case something went wrong
const error = ref<string | null>(null)
const username = ref<string>("")
const password = ref<string>("")
- async function submit() {
+ async function login() {
// Reset the error
error.value = null
try {
// Perform login
- const user: User = await login(username.value, password.value);
- if (user)
- auth.setUser(user);
+ await auth.login(username.value, password.value);
// Redirect back to the index page
router.push("/")
{{ error }}
</Notification>
- <form @submit.prevent="submit">
+ <form @submit.prevent="login">
<div class="field">
<p class="control has-icons-left">
<input class="input" type="text" v-model="username"