diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-11-17 07:48:54 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-11-17 07:48:54 +0100 |
| commit | 0a3ca92417d9d0c293469c9d1777f78328c31720 (patch) | |
| tree | 356d6fd82f482bf9b4216626a02e1709f41c2dde /code/app/src/lib/services/password-reset-service.ts | |
| parent | cfbdb4b5572c0e95fed3268fa818bc5c700fb30a (diff) | |
| download | greatoffice-0a3ca92417d9d0c293469c9d1777f78328c31720.tar.xz greatoffice-0a3ca92417d9d0c293469c9d1777f78328c31720.zip | |
feat: Move password reset logic and http calls into a service
Diffstat (limited to 'code/app/src/lib/services/password-reset-service.ts')
| -rw-r--r-- | code/app/src/lib/services/password-reset-service.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/code/app/src/lib/services/password-reset-service.ts b/code/app/src/lib/services/password-reset-service.ts new file mode 100644 index 0000000..650b5f7 --- /dev/null +++ b/code/app/src/lib/services/password-reset-service.ts @@ -0,0 +1,38 @@ +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<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 + } + } +}
\ No newline at end of file |
