Files
pocket-id/frontend/src/lib/services/webauthn-service.ts

48 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-08-12 11:00:25 +02:00
import type { Passkey } from '$lib/types/passkey.type';
import type { User } from '$lib/types/user.type';
import APIService from './api-service';
import userStore from '$lib/stores/user-store';
2025-01-27 12:00:40 +01:00
import type { AuthenticationResponseJSON, RegistrationResponseJSON } from '@simplewebauthn/browser';
2024-08-12 11:00:25 +02:00
class WebAuthnService extends APIService {
async getRegistrationOptions() {
return (await this.api.get(`/webauthn/register/start`)).data;
}
async finishRegistration(body: RegistrationResponseJSON) {
return (await this.api.post(`/webauthn/register/finish`, body)).data as Passkey;
}
async getLoginOptions() {
return (await this.api.get(`/webauthn/login/start`)).data;
}
async finishLogin(body: AuthenticationResponseJSON) {
return (await this.api.post(`/webauthn/login/finish`, body)).data as User;
}
async logout() {
await this.api.post(`/webauthn/logout`);
userStore.clearUser();
}
async listCredentials() {
return (await this.api.get(`/webauthn/credentials`)).data as Passkey[];
}
async removeCredential(id: string) {
await this.api.delete(`/webauthn/credentials/${id}`);
}
async updateCredentialName(id: string, name: string) {
await this.api.patch(`/webauthn/credentials/${id}`, { name });
}
async reauthenticate(body?: AuthenticationResponseJSON) {
const res = await this.api.post('/webauthn/reauthenticate', body);
return res.data.reauthenticationToken as string;
}
2024-08-12 11:00:25 +02:00
}
export default WebAuthnService;