import { defineStore } from "pinia"; const AUTH_URL = 'http://localhost:8000/api/sanctum/'; interface AuthState { user: null, errors: [] }; export const useAuthStore = defineStore("auth", { state: (): AuthState => ({ user: null, errors: [] }), actions: { async FETCH_USER() { const res = await fetch("http://localhost:8000/api/user"); const user = await res.json(); this.user = user; }, async SIGN_UP(email: string, password: string, name: string) { const res = await fetch(AUTH_URL + 'register', { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email, password, name, password_confirmation: password }), }); const user = await res.json(); if (user.errors && Array.isArray(user.errors) && user.errors.length > 0) { console.error(user.errors.join(', ')); return; } this.user = user; console.log('store : sign UP' + user); debugger; }, async SIGN_IN(email: string, password: string) { const res = await fetch(AUTH_URL + 'token', { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email, password, device_name: 'front_auth_vue' }), }); const user = await res.json(); if (user.errors && Array.isArray(user.errors) && user.errors.length > 0) { console.error(user.errors.join(', ')); return; } this.user = user; console.log('store : sign in' + user); }, async SIGN_OUT() { return await true; } }, });