histories are a little messed, will change the logic in the future (techdebt)
This commit is contained in:
@@ -1,58 +1,16 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import axios from 'axios'
|
||||
import { useAuthStore } from './auth'
|
||||
import { inject } from 'vue'
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
state: () => ({
|
||||
user: null,
|
||||
loading: false,
|
||||
error: null
|
||||
}),
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
|
||||
getters: {
|
||||
currentUser: (state) => state.user
|
||||
},
|
||||
const API_BASE_URL = inject('apiBaseURL')
|
||||
|
||||
actions: {
|
||||
async fetchUserProfile() {
|
||||
const authStore = useAuthStore()
|
||||
const token = authStore.getToken()
|
||||
const getCoins = async () => {
|
||||
return await axios.get(`${API_BASE_URL}/users/me/coins`)
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
this.error = 'No authentication token found'
|
||||
throw new Error('No authentication token found')
|
||||
}
|
||||
|
||||
this.loading = true
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
const response = await axios.get('/api/users/me', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Accept: 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
this.user = response.data
|
||||
return response.data
|
||||
} catch (error) {
|
||||
this.error = error.response?.data?.message || 'Failed to fetch user profile'
|
||||
|
||||
if (error.response?.status === 401) {
|
||||
authStore.logout()
|
||||
}
|
||||
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
clearUser() {
|
||||
this.user = null
|
||||
this.error = null
|
||||
this.loading = false
|
||||
}
|
||||
return {
|
||||
getCoins,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user