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