fix: add openid scope and improve OAuth token handling for Authentik
This commit is contained in:
parent
46cecdc007
commit
b5fe07d07e
2 changed files with 104 additions and 14 deletions
|
@ -5,10 +5,13 @@ export const useAuthStore = defineStore('auth', {
|
|||
state: () => ({
|
||||
token: localStorage.getItem('auth_token') || null,
|
||||
user: null as any | null,
|
||||
loading: false,
|
||||
error: null as string | null,
|
||||
}),
|
||||
|
||||
getters: {
|
||||
isAuthenticated: (state) => !!state.token,
|
||||
isLoading: (state) => state.loading,
|
||||
},
|
||||
|
||||
actions: {
|
||||
|
@ -21,32 +24,60 @@ export const useAuthStore = defineStore('auth', {
|
|||
this.user = user;
|
||||
},
|
||||
|
||||
setError(error: string | null) {
|
||||
this.error = error;
|
||||
},
|
||||
|
||||
logout() {
|
||||
this.token = null;
|
||||
this.user = null;
|
||||
this.error = null;
|
||||
localStorage.removeItem('auth_token');
|
||||
},
|
||||
|
||||
async fetchUserInfo() {
|
||||
if (!this.token) return;
|
||||
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/v1/health', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${this.token}`
|
||||
'Authorization': `Bearer ${this.token}`,
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
this.setUser(data.user);
|
||||
console.log('User info fetched successfully:', data.user);
|
||||
} else {
|
||||
// If token is invalid, logout
|
||||
this.logout();
|
||||
// Get error details
|
||||
let errorText = 'Authentication failed';
|
||||
try {
|
||||
const errorData = await response.json();
|
||||
errorText = errorData.error || errorText;
|
||||
} catch (e) {
|
||||
// If we can't parse the error as JSON, use the status text
|
||||
errorText = response.statusText || errorText;
|
||||
}
|
||||
|
||||
console.error('Failed to fetch user info:', errorText);
|
||||
this.setError(errorText);
|
||||
|
||||
// If token is invalid (401), logout
|
||||
if (response.status === 401) {
|
||||
this.logout();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch user info:', error);
|
||||
this.logout();
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
console.error('Failed to fetch user info:', errorMessage);
|
||||
this.setError(errorMessage);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue