mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-02-12 18:30:15 +00:00
initial commit
This commit is contained in:
23
frontend/src/lib/services/api-service.ts
Normal file
23
frontend/src/lib/services/api-service.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { browser } from '$app/environment';
|
||||
import { env } from '$env/dynamic/public';
|
||||
import axios from 'axios';
|
||||
|
||||
abstract class APIService {
|
||||
baseURL: string = '/api';
|
||||
api = axios.create({
|
||||
withCredentials: true
|
||||
});
|
||||
|
||||
constructor(accessToken?: string) {
|
||||
if (accessToken) {
|
||||
this.api.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
|
||||
} else {
|
||||
this.api.defaults.baseURL = '/api';
|
||||
}
|
||||
if (!browser) {
|
||||
this.api.defaults.baseURL = (env.PUBLIC_APP_URL ?? 'http://localhost') + '/api';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default APIService;
|
||||
@@ -0,0 +1,49 @@
|
||||
import type {
|
||||
AllApplicationConfiguration,
|
||||
ApplicationConfigurationRawResponse
|
||||
} from '$lib/types/application-configuration';
|
||||
import APIService from './api-service';
|
||||
|
||||
export default class ApplicationConfigurationService extends APIService {
|
||||
async list(showAll = false) {
|
||||
const { data } = await this.api.get<ApplicationConfigurationRawResponse>(
|
||||
'/application-configuration',
|
||||
{
|
||||
params: { showAll }
|
||||
}
|
||||
);
|
||||
|
||||
const applicationConfiguration: Partial<AllApplicationConfiguration> = {};
|
||||
data.forEach(({ key, value }) => {
|
||||
(applicationConfiguration as any)[key] = value;
|
||||
});
|
||||
|
||||
return applicationConfiguration as AllApplicationConfiguration;
|
||||
}
|
||||
|
||||
async update(applicationConfiguration: AllApplicationConfiguration) {
|
||||
const res = await this.api.put('/application-configuration', applicationConfiguration);
|
||||
return res.data as AllApplicationConfiguration;
|
||||
}
|
||||
|
||||
async updateFavicon(favicon: File) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', favicon!);
|
||||
|
||||
await this.api.put(`/application-configuration/favicon`, formData);
|
||||
}
|
||||
|
||||
async updateLogo(logo: File) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', logo!);
|
||||
|
||||
await this.api.put(`/application-configuration/logo`, formData);
|
||||
}
|
||||
|
||||
async updateBackgroundImage(backgroundImage: File) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', backgroundImage!);
|
||||
|
||||
await this.api.put(`/application-configuration/background-image`, formData);
|
||||
}
|
||||
}
|
||||
80
frontend/src/lib/services/oidc-service.ts
Normal file
80
frontend/src/lib/services/oidc-service.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import type { OidcClient, OidcClientCreate } from '$lib/types/oidc.type';
|
||||
import type { Paginated, PaginationRequest } from '$lib/types/pagination.type';
|
||||
import APIService from './api-service';
|
||||
|
||||
class OidcService extends APIService {
|
||||
async authorize(clientId: string, scope: string, nonce?: string) {
|
||||
const res = await this.api.post('/oidc/authorize', {
|
||||
scope,
|
||||
nonce,
|
||||
clientId
|
||||
});
|
||||
|
||||
return res.data.code as string;
|
||||
}
|
||||
|
||||
async authorizeNewClient(clientId: string, scope: string, nonce?: string) {
|
||||
const res = await this.api.post('/oidc/authorize/new-client', {
|
||||
scope,
|
||||
nonce,
|
||||
clientId
|
||||
});
|
||||
|
||||
return res.data.code as string;
|
||||
}
|
||||
|
||||
async listClients(search?: string, pagination?: PaginationRequest) {
|
||||
const page = pagination?.page || 1;
|
||||
const limit = pagination?.limit || 10;
|
||||
|
||||
const res = await this.api.get('/oidc/clients', {
|
||||
params: {
|
||||
search,
|
||||
page,
|
||||
limit
|
||||
}
|
||||
});
|
||||
return res.data as Paginated<OidcClient>;
|
||||
}
|
||||
|
||||
async createClient(client: OidcClientCreate) {
|
||||
return (await this.api.post('/oidc/clients', client)).data as OidcClient;
|
||||
}
|
||||
|
||||
async removeClient(id: string) {
|
||||
await this.api.delete(`/oidc/clients/${id}`);
|
||||
}
|
||||
|
||||
async getClient(id: string) {
|
||||
return (await this.api.get(`/oidc/clients/${id}`)).data as OidcClient;
|
||||
}
|
||||
|
||||
async updateClient(id: string, client: OidcClientCreate) {
|
||||
return (await this.api.put(`/oidc/clients/${id}`, client)).data as OidcClient;
|
||||
}
|
||||
|
||||
async updateClientLogo(client: OidcClient, image: File | null) {
|
||||
if (client.hasLogo && !image) {
|
||||
await this.removeClientLogo(client.id);
|
||||
return;
|
||||
}
|
||||
if (!client.hasLogo && !image) {
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', image!);
|
||||
|
||||
await this.api.post(`/oidc/clients/${client.id}/logo`, formData);
|
||||
}
|
||||
|
||||
async removeClientLogo(id: string) {
|
||||
await this.api.delete(`/oidc/clients/${id}/logo`);
|
||||
}
|
||||
|
||||
async createClientSecret(id: string) {
|
||||
return (await this.api.post(`/oidc/clients/${id}/secret`)).data.secret as string;
|
||||
}
|
||||
}
|
||||
|
||||
export default OidcService;
|
||||
61
frontend/src/lib/services/user-service.ts
Normal file
61
frontend/src/lib/services/user-service.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import type { Paginated, PaginationRequest } from '$lib/types/pagination.type';
|
||||
import type { User, UserCreate } from '$lib/types/user.type';
|
||||
import APIService from './api-service';
|
||||
|
||||
export default class UserService extends APIService {
|
||||
async list(search?: string, pagination?: PaginationRequest) {
|
||||
const page = pagination?.page || 1;
|
||||
const limit = pagination?.limit || 10;
|
||||
|
||||
const res = await this.api.get('/users', {
|
||||
params: {
|
||||
search,
|
||||
page,
|
||||
limit
|
||||
}
|
||||
});
|
||||
return res.data as Paginated<User>;
|
||||
}
|
||||
|
||||
async get(id: string) {
|
||||
const res = await this.api.get(`/users/${id}`);
|
||||
return res.data as User;
|
||||
}
|
||||
|
||||
async getCurrent() {
|
||||
const res = await this.api.get('/users/me');
|
||||
return res.data as User;
|
||||
}
|
||||
|
||||
async create(user: UserCreate) {
|
||||
const res = await this.api.post('/users', user);
|
||||
return res.data as User;
|
||||
}
|
||||
|
||||
async update(id: string, user: UserCreate) {
|
||||
const res = await this.api.put(`/users/${id}`, user);
|
||||
return res.data as User;
|
||||
}
|
||||
|
||||
async updateCurrent(user: UserCreate) {
|
||||
const res = await this.api.put('/users/me', user);
|
||||
return res.data as User;
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
await this.api.delete(`/users/${id}`);
|
||||
}
|
||||
|
||||
async createOneTimeAccessToken(userId: string) {
|
||||
const res = await this.api.post(`/users/${userId}/one-time-access-token`, {
|
||||
userId,
|
||||
expiresAt: new Date(Date.now() + 1000 * 60 * 5).toISOString()
|
||||
});
|
||||
return res.data.token;
|
||||
}
|
||||
|
||||
async exchangeOneTimeAccessToken(token: string) {
|
||||
const res = await this.api.post(`/one-time-access-token/${token}`);
|
||||
return res.data as User;
|
||||
}
|
||||
}
|
||||
42
frontend/src/lib/services/webauthn-service.ts
Normal file
42
frontend/src/lib/services/webauthn-service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { Passkey } from '$lib/types/passkey.type';
|
||||
import type { User } from '$lib/types/user.type';
|
||||
import type { AuthenticationResponseJSON, RegistrationResponseJSON } from '@simplewebauthn/types';
|
||||
import APIService from './api-service';
|
||||
import userStore from '$lib/stores/user-store';
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
export default WebAuthnService;
|
||||
Reference in New Issue
Block a user