histories are a little messed, will change the logic in the future (techdebt)
This commit is contained in:
@@ -53,12 +53,10 @@ export const useAPIStore = defineStore('api', () => {
|
||||
delete axios.defaults.headers.common['Authorization']
|
||||
}
|
||||
|
||||
// Users
|
||||
const getAuthUser = () => {
|
||||
return axios.get(`${API_BASE_URL}/users/me`)
|
||||
}
|
||||
|
||||
//Games
|
||||
const getGames = (resetPagination = false) => {
|
||||
if (resetPagination) {
|
||||
gameQueryParameters.value.page = 1
|
||||
@@ -78,11 +76,26 @@ export const useAPIStore = defineStore('api', () => {
|
||||
return axios.get(`${API_BASE_URL}/games?${queryParams}`)
|
||||
}
|
||||
|
||||
const getCurrentUserMatches = (params = {}) => {
|
||||
const queryParams = new URLSearchParams({
|
||||
page: params.page || 1,
|
||||
...(params.type && { type: params.type }),
|
||||
...(params.status && { status: params.status }),
|
||||
sort_by: params.sort || 'began_at',
|
||||
sort_direction: params.direction || 'desc',
|
||||
}).toString()
|
||||
|
||||
console.log(params, queryParams)
|
||||
|
||||
return axios.get(`${API_BASE_URL}/users/${authStore.currentUserID}/matches?${queryParams}`)
|
||||
}
|
||||
|
||||
return {
|
||||
postLogin,
|
||||
postLogout,
|
||||
getAuthUser,
|
||||
getGames,
|
||||
getCurrentUserMatches,
|
||||
gameQueryParameters,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -7,8 +7,6 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
const DEFAULT_TIMEOUT = 5 * 60 * 1000
|
||||
const REMEMBER_ME_TIMEOUT = 365 * 24 * 60 * 60 * 1000
|
||||
|
||||
|
||||
|
||||
const currentUser = ref(undefined)
|
||||
const token = ref(localStorage.getItem('token') || null)
|
||||
const tokenExpiry = ref(localStorage.getItem('tokenExpiry') ? parseInt(localStorage.getItem('tokenExpiry')) : null)
|
||||
@@ -54,10 +52,14 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
const login = async (credentials) => {
|
||||
const apiStore = useAPIStore()
|
||||
const loginResp = await apiStore.postLogin(credentials)
|
||||
const userResp = await apiStore.getAuthUser()
|
||||
return await setLocalStorageUser(apiStore, loginResp, true)
|
||||
}
|
||||
|
||||
const jwtToken = loginResp.data.token
|
||||
setToken(jwtToken, credentials.rememberMe)
|
||||
const setLocalStorageUser = async (api, response, rememberMe) => {
|
||||
const userResp = await api.getAuthUser()
|
||||
|
||||
const jwtToken = response.data.token
|
||||
setToken(jwtToken, rememberMe)
|
||||
currentUser.value = userResp.data
|
||||
|
||||
localStorage.setItem('currentUser', JSON.stringify(userResp.data))
|
||||
@@ -72,15 +74,8 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
localStorage.removeItem('currentUser')
|
||||
}
|
||||
|
||||
const checkTokenExpiry = () => {
|
||||
if (isTokenAlmostExpired()) {
|
||||
logout()
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const restoreSession = async () => {
|
||||
const apiStore = useAPIStore()
|
||||
const storedToken = localStorage.getItem('token')
|
||||
const storedUser = localStorage.getItem('currentUser')
|
||||
const storedTokenExpiry = localStorage.getItem('tokenExpiry')
|
||||
@@ -102,7 +97,6 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
try {
|
||||
currentUser.value = JSON.parse(storedUser)
|
||||
} catch (e) {
|
||||
// If stored user data is corrupted, fetch fresh data from API
|
||||
try {
|
||||
const userResp = await apiStore.getAuthUser()
|
||||
currentUser.value = userResp.data
|
||||
@@ -124,13 +118,11 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
currentUserID,
|
||||
token,
|
||||
rememberMe,
|
||||
setToken,
|
||||
getToken,
|
||||
login,
|
||||
logout,
|
||||
resetTokenExpiry,
|
||||
isTokenAlmostExpired,
|
||||
checkTokenExpiry,
|
||||
restoreSession,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import axios from 'axios'
|
||||
import { defineStore } from 'pinia'
|
||||
import { inject } from 'vue'
|
||||
|
||||
export const useRegisterStore = defineStore('register', () => {
|
||||
|
||||
const API_BASE_URL = inject('apiBaseURL')
|
||||
|
||||
const register = async (data) => {
|
||||
return await axios.post(`${API_BASE_URL}/register`, data)
|
||||
}
|
||||
|
||||
return {
|
||||
register,
|
||||
}
|
||||
})
|
||||
@@ -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