oidc authentication
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
export async function logout() {
|
||||
useAuthSession().value = null;
|
||||
useAdminStore().$reset();
|
||||
useWarrenStore().$reset();
|
||||
await navigateTo({
|
||||
path: '/login',
|
||||
});
|
||||
|
||||
74
frontend/lib/api/auth/oidc.ts
Normal file
74
frontend/lib/api/auth/oidc.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
import { getApiHeaders } from '..';
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { AuthUser } from '~/shared/types/auth';
|
||||
|
||||
export async function getRedirectUrl(): Promise<
|
||||
{ success: true; url: string } | { success: false }
|
||||
> {
|
||||
const { data, error } = await useFetch<ApiResponse<string>>(
|
||||
getApiUrl('auth/oidc'),
|
||||
{
|
||||
method: 'GET',
|
||||
headers: getApiHeaders(false),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
toast.error('OpenID Connect', {
|
||||
description: error.value?.data ?? 'Failed to get OIDC redirect',
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
url: data.value.data,
|
||||
};
|
||||
}
|
||||
|
||||
export async function oidcLoginUser(
|
||||
code: string,
|
||||
state: string
|
||||
): Promise<{ success: boolean }> {
|
||||
const { data, error } = await useFetch<
|
||||
ApiResponse<{ token: string; user: AuthUser; expiresAt: number }>
|
||||
>(getApiUrl(`auth/oidc/login?code=${code}&state=${state}`), {
|
||||
method: 'GET',
|
||||
headers: getApiHeaders(false),
|
||||
responseType: 'json',
|
||||
retry: false,
|
||||
});
|
||||
|
||||
if (data.value == null) {
|
||||
toast.error('OpenID Connect', {
|
||||
description: error.value?.data ?? 'Failed to login with OIDC',
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const token = data.value.data.token;
|
||||
const { user, expiresAt } = data.value.data;
|
||||
|
||||
useAuthSession().value = {
|
||||
type: 'WarrenAuth',
|
||||
id: token,
|
||||
user,
|
||||
expiresAt,
|
||||
};
|
||||
|
||||
toast.success('OpenID Connect', {
|
||||
description: `Successfully logged in`,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user