import { http_get_async, http_post_async } from "$lib/api/_fetch"; import { api_base } from "$lib/configuration"; import { is_known_problem } from "$lib/models/internal/KnownProblem"; import type { CreateRequestResponse, FulfillRequestResponse, IPasswordResetService, RequestIsValidResponse } from "./abstractions/IPasswordResetService"; export class PasswordResetService implements IPasswordResetService { async create_request_async(email: string): Promise { const response = await http_post_async(api_base("_/password-reset-request/create"), { email }); if (response.ok) return { isCreated: true }; if (is_known_problem(response)) return { isCreated: false, knownProblem: await response.json() } return { isCreated: false } } async fulfill_request_async(id: string, newPassword: string): Promise { const response = await http_post_async(api_base("_/password-reset-request/fulfill"), { id: id, newPassword }); if (response.ok) return { isFulfilled: true }; if (is_known_problem(response)) return { isFulfilled: false, knownProblem: await response.json() } return { isFulfilled: false, } } async request_is_valid_async(id: string): Promise { const response = await http_get_async(api_base("_/password-reset-request/is-valid?id=" + id)); const responseBody = await response.json() as { isValid: boolean }; return { isValid: responseBody.isValid } } }