This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { getCurrentPrincipal, login, logout } from '@/services/auth-service.js';
|
||||
|
||||
const principal = ref(null);
|
||||
const isLoading = ref(false);
|
||||
const error = ref(null);
|
||||
|
||||
function csrfToken() {
|
||||
return document.cookie.split('; ').find((value) => value.startsWith('pokemon_inventory_csrf='))?.split('=')[1] ?? '';
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
const isAuthenticated = computed(() => Boolean(principal.value));
|
||||
|
||||
async function load() {
|
||||
isLoading.value = true;
|
||||
try { principal.value = await getCurrentPrincipal(); }
|
||||
catch { principal.value = null; }
|
||||
finally { isLoading.value = false; }
|
||||
}
|
||||
|
||||
async function signIn(email, password) {
|
||||
error.value = null;
|
||||
isLoading.value = true;
|
||||
try { await login(email, password); await load(); }
|
||||
catch (requestError) { error.value = requestError; throw requestError; }
|
||||
finally { isLoading.value = false; }
|
||||
}
|
||||
|
||||
async function signOut() {
|
||||
await logout(csrfToken());
|
||||
principal.value = null;
|
||||
}
|
||||
|
||||
return { principal, isAuthenticated, isLoading, error, load, signIn, signOut };
|
||||
}
|
||||
Reference in New Issue
Block a user