✨ Improve code formatting and update API request payload structure in authentication composable
This commit is contained in:
@@ -22,7 +22,7 @@ const isAdmin = computed(() => user.value?.isAdmin || false);
|
||||
*/
|
||||
async function initializeAuth() {
|
||||
const storedToken = localStorage.getItem('auth_token');
|
||||
|
||||
|
||||
if (!storedToken) {
|
||||
token.value = null;
|
||||
user.value = null;
|
||||
@@ -30,7 +30,9 @@ async function initializeAuth() {
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await apiClient.post('/api/auth/verify', { token: storedToken });
|
||||
const response = await apiClient.post('/api/auth/verify', {
|
||||
token: storedToken
|
||||
});
|
||||
token.value = storedToken;
|
||||
user.value = response.user;
|
||||
} catch (err) {
|
||||
@@ -104,7 +106,9 @@ async function refreshToken() {
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await apiClient.post('/api/auth/refresh', { token: token.value });
|
||||
const response = await apiClient.post('/api/auth/refresh', {
|
||||
token: token.value
|
||||
});
|
||||
token.value = response.token;
|
||||
localStorage.setItem('auth_token', response.token);
|
||||
return response;
|
||||
@@ -142,7 +146,7 @@ async function getUserInfo() {
|
||||
*/
|
||||
function hasPermission(permissions) {
|
||||
if (!user.value) return false;
|
||||
|
||||
|
||||
const perms = Array.isArray(permissions) ? permissions : [permissions];
|
||||
return perms.some(perm => user.value.permissions?.includes(perm));
|
||||
}
|
||||
@@ -156,7 +160,7 @@ export function setupAuthInterceptor() {
|
||||
}
|
||||
|
||||
// Watch for token changes and update header
|
||||
watch(token, (newToken) => {
|
||||
watch(token, newToken => {
|
||||
if (newToken) {
|
||||
apiClient.setDefaultHeader('Authorization', `Bearer ${newToken}`);
|
||||
} else {
|
||||
@@ -172,11 +176,11 @@ export function useAuth() {
|
||||
user: computed(() => user.value),
|
||||
isLoading: computed(() => isLoading.value),
|
||||
error: computed(() => error.value),
|
||||
|
||||
|
||||
// Computed
|
||||
isAuthenticated,
|
||||
isAdmin,
|
||||
|
||||
|
||||
// Methods
|
||||
initializeAuth,
|
||||
login,
|
||||
|
||||
Reference in New Issue
Block a user