This commit is contained in:
Matthias Hochmeister
2026-02-23 17:08:58 +01:00
commit f09748f4a1
97 changed files with 17729 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
import { api } from './api';
import { AUTHENTIK_URL, CLIENT_ID } from '../utils/config';
import { User } from '../types/auth.types';
const REDIRECT_URI = `${window.location.origin}/auth/callback`;
export interface AuthCallbackResponse {
token: string;
user: User;
}
export const authService = {
/**
* Generate Authentik authorization URL
*/
getAuthUrl(): string {
const params = new URLSearchParams({
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
response_type: 'code',
scope: 'openid profile email',
});
return `${AUTHENTIK_URL}/application/o/authorize/?${params.toString()}`;
},
/**
* Handle OAuth callback - send code to backend, receive JWT
*/
async handleCallback(code: string): Promise<AuthCallbackResponse> {
const response = await api.post<AuthCallbackResponse>('/api/auth/callback', {
code,
redirect_uri: REDIRECT_URI,
});
return response.data;
},
/**
* Logout - clear tokens
*/
async logout(): Promise<void> {
try {
// Optionally call backend logout endpoint
await api.post('/api/auth/logout');
} catch (error) {
console.error('Error during logout:', error);
// Continue with logout even if backend call fails
}
},
/**
* Get current user information
*/
async getCurrentUser(): Promise<User> {
const response = await api.get<User>('/api/user/me');
return response.data;
},
};