aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/services/password-reset-service.ts
blob: 4a174fa32804fb3bb673018b1470b4f6fbe1d9e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { http_get_async, http_post_async } from "$utilities/_fetch";
import { api_base } from "$configuration";
import { is_known_problem } from "$models/internal/KnownProblem";
import type {
    CreateRequestResponse,
    FulfillRequestResponse,
    IPasswordResetService,
    RequestIsValidResponse,
} from "./abstractions/IPasswordResetService";

export class PasswordResetService implements IPasswordResetService {
    static resolve(): IPasswordResetService {
        return new PasswordResetService();
    }
    async create_request_async(email: string): Promise<CreateRequestResponse> {
        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<FulfillRequestResponse> {
        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<RequestIsValidResponse> {
        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,
        };
    }
}