Files
tp/begushiybashkir/bbvue/src/stores/auth.js
T
valitovgaziz 237ee6742e modified: begushiybashkir/bbvue/src/main.js
modified:   begushiybashkir/bbvue/src/stores/auth.js
	new file:   begushiybashkir/bbvue/src/stores/helpers/api.js
	new file:   begushiybashkir/bbvue/src/stores/index.js
	new file:   begushiybashkir/bbvue/src/stores/plugins/persistence.js
	modified:   begushiybashkir/bbvue/src/stores/user.js
	modified:   serv_nginx/api_bb/internal/models/user.go
	new file:   serv_nginx/api_bb/internal/models/workout.go
fix save store, add helpers, add new models
2025-10-12 06:11:54 +05:00

131 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// stores/auth.js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { apiClient, withLoading } from './helpers/api'
export const useAuthStore = defineStore('auth', () => {
// State
const user = ref(null)
const token = ref(localStorage.getItem('auth_token') || '')
const loading = ref(false)
const error = ref('')
const initialized = ref(false)
// Getters
const isAuthenticated = computed(() => !!token.value && !!user.value)
const userFullName = computed(() =>
user.value ? `${user.value.firstName} ${user.value.lastName}` : ''
)
// Actions
const setToken = (newToken) => {
token.value = newToken
localStorage.setItem('auth_token', newToken)
}
const clearAuth = () => {
token.value = ''
user.value = null
localStorage.removeItem('auth_token')
}
const setUser = (userData) => {
user.value = userData
}
const register = async (userData) => {
// Передаем store объект с loading и error
return withLoading({ loading, error }, async () => {
await apiClient.post('/auth/register', userData)
// Auto-login after registration
const loginResponse = await apiClient.post('/auth/login', {
email: userData.email,
password: userData.password
})
const { token: authToken, user: userInfo } = loginResponse.data
setToken(authToken)
setUser(userInfo)
return { success: true }
})
}
const login = async (credentials) => {
return withLoading({ loading, error }, async () => {
const response = await apiClient.post('/auth/login', credentials)
const { token: authToken, user: userInfo } = response.data
setToken(authToken)
setUser(userInfo)
return { success: true, data: response.data }
})
}
const logout = async () => {
return withLoading({ loading, error }, async () => {
try {
await apiClient.post('/auth/logout')
} catch (err) {
console.error('Logout error:', err)
} finally {
clearAuth()
}
return { success: true }
})
}
const fetchProfile = async () => {
return withLoading({ loading, error }, async () => {
const response = await apiClient.get('/user/profile')
setUser(response.data)
return { success: true, data: response.data }
})
}
const updateProfile = async (profileData) => {
return withLoading({ loading, error }, async () => {
const response = await apiClient.post('/user/editProfile', profileData)
setUser(response.data)
return { success: true, data: response.data }
})
}
const initializeAuth = async () => {
if (initialized.value || !token.value) return
initialized.value = true
try {
await fetchProfile()
console.log('Auth restored successfully')
} catch (err) {
console.error('Auth restoration failed:', err)
clearAuth()
}
}
return {
// State
user,
token,
loading,
error,
initialized,
// Getters
isAuthenticated,
userFullName,
// Actions
register,
login,
logout,
fetchProfile,
updateProfile,
initializeAuth,
clearAuth
}
})