Add bcrypt user file auth, JWT middleware on all write routes, Pinia authStore with localStorage persistence, login view with redirect support, and v-if guards on all CRUD controls and admin nav link. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
24 lines
669 B
TypeScript
24 lines
669 B
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
import { login as apiLogin } from '../api/auth'
|
|
|
|
const TOKEN_KEY = 'auth_token'
|
|
|
|
export const useAuthStore = defineStore('auth', () => {
|
|
const token = ref<string | null>(localStorage.getItem(TOKEN_KEY))
|
|
const isLoggedIn = computed(() => token.value !== null)
|
|
|
|
async function login(username: string, password: string) {
|
|
const resp = await apiLogin(username, password)
|
|
token.value = resp.token
|
|
localStorage.setItem(TOKEN_KEY, resp.token)
|
|
}
|
|
|
|
function logout() {
|
|
token.value = null
|
|
localStorage.removeItem(TOKEN_KEY)
|
|
}
|
|
|
|
return { token, isLoggedIn, login, logout }
|
|
})
|