v4
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { api } from 'boot/axios'
|
||||
import { api, type ServerError } from 'boot/axios'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
@@ -11,48 +11,77 @@ interface User {
|
||||
avatar?: string
|
||||
}
|
||||
|
||||
const ENDPOINT_MAP = {
|
||||
register: '/auth/register',
|
||||
forgot: '/auth/forgot',
|
||||
change: '/auth/change'
|
||||
} as const
|
||||
|
||||
export type AuthFlowType = keyof typeof ENDPOINT_MAP
|
||||
|
||||
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
|
||||
user.value = data.data
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
user.value = null
|
||||
handleAuthError(error as ServerError)
|
||||
} finally {
|
||||
isInitialized.value = true
|
||||
}
|
||||
}
|
||||
|
||||
const handleAuthError = (error: ServerError) => {
|
||||
if (error.code === '401') {
|
||||
user.value = null
|
||||
} else {
|
||||
console.error('Authentication error:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const loginWithCredentials = async (email: string, password: string) => {
|
||||
// будет переделано на беке - нужно сменить урл
|
||||
await api.post('/api/admin/customer/login', { email, password }, { withCredentials: true })
|
||||
await api.post('/auth/email', { email, password }, { withCredentials: true })
|
||||
await initialize()
|
||||
}
|
||||
|
||||
const loginWithTelegram = async (initData: string) => {
|
||||
await api.post('/api/admin/customer/login', { initData }, { withCredentials: true })
|
||||
await api.post('/auth/telegram', { initData }, { withCredentials: true })
|
||||
await initialize()
|
||||
}
|
||||
|
||||
const logout = async () => {
|
||||
try {
|
||||
await api.get('/customer/logout', {})
|
||||
} finally {
|
||||
await api.get('/auth/logout', {})
|
||||
user.value = null
|
||||
isInitialized.value = false
|
||||
} finally {
|
||||
// @ts-expect-ignore
|
||||
// window.Telegram?.WebApp.close()
|
||||
}
|
||||
}
|
||||
|
||||
const initRegistration = async (flowType: AuthFlowType, email: string) => {
|
||||
await api.post(ENDPOINT_MAP[flowType], { email })
|
||||
}
|
||||
|
||||
const confirmCode = async (flowType: AuthFlowType, email: string, code: string) => {
|
||||
await api.post(ENDPOINT_MAP[flowType], { email, code })
|
||||
}
|
||||
|
||||
const setPassword = async (
|
||||
flowType: AuthFlowType,
|
||||
email: string,
|
||||
code: string,
|
||||
password: string
|
||||
) => {
|
||||
await api.post(ENDPOINT_MAP[flowType], { email, code, password })
|
||||
}
|
||||
|
||||
return {
|
||||
user,
|
||||
isAuthenticated,
|
||||
@@ -60,6 +89,9 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
initialize,
|
||||
loginWithCredentials,
|
||||
loginWithTelegram,
|
||||
logout
|
||||
logout,
|
||||
initRegistration,
|
||||
confirmCode,
|
||||
setPassword
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user