Files
oil-formula-calculator/frontend/src/stores/auth.js
Hera Zhao 6563a6f7d2
Some checks failed
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 4s
Test / build-check (push) Successful in 3s
PR Preview / test (pull_request) Successful in 4s
PR Preview / deploy-preview (pull_request) Successful in 20s
Test / e2e-test (push) Failing after 1m2s
Grant senior_editor oil editing, PDF export, and public recipe management
Add canManage computed (senior_editor + admin) to auth store and use it
for oil edit/delete buttons, PDF export, and public recipe section
visibility. Backend already allowed these operations for senior_editor.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 11:59:49 +00:00

108 lines
2.6 KiB
JavaScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { api } from '../composables/useApi'
const DEFAULT_USER = {
id: null,
role: 'viewer',
username: 'anonymous',
display_name: '匿名',
has_password: false,
business_verified: false,
}
export const useAuthStore = defineStore('auth', () => {
const token = ref(localStorage.getItem('oil_auth_token') || '')
const user = ref({ ...DEFAULT_USER })
// Getters
const isLoggedIn = computed(() => user.value.id !== null)
const isAdmin = computed(() => user.value.role === 'admin')
const canManage = computed(() =>
['senior_editor', 'admin'].includes(user.value.role)
)
const canEdit = computed(() =>
['editor', 'senior_editor', 'admin'].includes(user.value.role)
)
const isBusiness = computed(() => user.value.business_verified)
// Actions
async function initToken() {
const params = new URLSearchParams(window.location.search)
const urlToken = params.get('token')
if (urlToken) {
token.value = urlToken
localStorage.setItem('oil_auth_token', urlToken)
// Clean URL
const url = new URL(window.location)
url.searchParams.delete('token')
window.history.replaceState({}, '', url)
}
if (token.value) {
await loadMe()
}
}
async function loadMe() {
try {
const data = await api.get('/api/me')
user.value = {
id: data.id,
role: data.role,
username: data.username,
display_name: data.display_name,
has_password: data.has_password ?? false,
business_verified: data.business_verified ?? false,
}
} catch {
logout()
}
}
async function login(username, password) {
const data = await api.post('/api/login', { username, password })
token.value = data.token
localStorage.setItem('oil_auth_token', data.token)
await loadMe()
}
async function register(username, password, displayName) {
const data = await api.post('/api/register', {
username,
password,
display_name: displayName,
})
token.value = data.token
localStorage.setItem('oil_auth_token', data.token)
await loadMe()
}
function logout() {
token.value = ''
localStorage.removeItem('oil_auth_token')
user.value = { ...DEFAULT_USER }
}
function canEditRecipe(recipe) {
if (isAdmin.value || user.value.role === 'senior_editor') return true
if (recipe._owner_id === user.value.id) return true
return false
}
return {
token,
user,
isLoggedIn,
isAdmin,
canManage,
canEdit,
isBusiness,
initToken,
loadMe,
login,
register,
logout,
canEditRecipe,
}
})