v1
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { api } from 'boot/axios'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
email?: string
|
||||
username: string
|
||||
first_name?: string
|
||||
last_name?: string
|
||||
avatar?: string
|
||||
}
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
// State
|
||||
const user = ref<User | null>(null)
|
||||
const isInitialized = ref(false)
|
||||
|
||||
// Getters
|
||||
const isAuthenticated = computed(() => !!user.value)
|
||||
|
||||
// Actions
|
||||
const initialize = async () => {
|
||||
try {
|
||||
const { data } = await api.get('/customer/profile')
|
||||
user.value = data
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
user.value = null
|
||||
} finally {
|
||||
isInitialized.value = true
|
||||
}
|
||||
}
|
||||
|
||||
const loginWithCredentials = async (email: string, password: string) => {
|
||||
// будет переделано на беке - нужно сменить урл
|
||||
await api.post('/api/admin/customer/login', { email, password }, { withCredentials: true })
|
||||
await initialize()
|
||||
}
|
||||
|
||||
const loginWithTelegram = async (initData: string) => {
|
||||
await api.post('/api/admin/customer/login', { initData }, { withCredentials: true })
|
||||
await initialize()
|
||||
}
|
||||
|
||||
const logout = async () => {
|
||||
try {
|
||||
await api.get('/customer/logout', {})
|
||||
} finally {
|
||||
user.value = null
|
||||
// @ts-expect-ignore
|
||||
// window.Telegram?.WebApp.close()
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
user,
|
||||
isAuthenticated,
|
||||
isInitialized,
|
||||
initialize,
|
||||
loginWithCredentials,
|
||||
loginWithTelegram,
|
||||
logout
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user