Files
homeschool/frontend/src/stores/auth.js
derekc c560055b10 Add Super Admin panel with user impersonation
- New /super-admin/login and /super-admin routes with separate auth
- Super admin can view all registered accounts and impersonate any user
- Impersonation banner shows at top of screen with exit button
- ADMIN_USERNAME and ADMIN_PASSWORD config added to .env and docker-compose.yml
- Fixed auth store: export setToken, clearToken, and setUser so they are
  accessible from superAdmin store
- Updated README with super admin feature, new env vars, and setup notes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-04 22:30:44 -08:00

86 lines
1.8 KiB
JavaScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import api from '@/composables/useApi'
export const useAuthStore = defineStore('auth', () => {
const accessToken = ref(localStorage.getItem('access_token') || null)
const user = ref(null)
const isAuthenticated = computed(() => !!accessToken.value)
const timezone = computed(() => user.value?.timezone || 'UTC')
function setToken(token) {
accessToken.value = token
localStorage.setItem('access_token', token)
}
function clearToken() {
accessToken.value = null
user.value = null
localStorage.removeItem('access_token')
}
async function login(email, password) {
const res = await api.post('/api/auth/login', { email, password })
setToken(res.data.access_token)
await fetchMe()
}
async function register(email, password, fullName) {
const res = await api.post('/api/auth/register', {
email,
password,
full_name: fullName,
})
setToken(res.data.access_token)
await fetchMe()
}
async function logout() {
try {
await api.post('/api/auth/logout')
} catch (_) {
// ignore errors on logout
}
clearToken()
}
async function tryRefresh() {
try {
const res = await api.post('/api/auth/refresh')
setToken(res.data.access_token)
await fetchMe()
} catch (_) {
clearToken()
}
}
async function fetchMe() {
try {
const res = await api.get('/api/users/me')
user.value = res.data
} catch (_) {
clearToken()
}
}
function setUser(userData) {
user.value = userData
}
return {
accessToken,
user,
isAuthenticated,
timezone,
setToken,
clearToken,
setUser,
login,
register,
logout,
tryRefresh,
fetchMe,
}
})