75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
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,
|
|
};
|
|
}
|