diff options
Diffstat (limited to 'code/app/src/lib')
3 files changed, 59 insertions, 14 deletions
diff --git a/code/app/src/lib/api/password-reset-request/index.ts b/code/app/src/lib/api/password-reset-request/index.ts deleted file mode 100644 index 53cb1d2..0000000 --- a/code/app/src/lib/api/password-reset-request/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -import {api_base} from "$lib/configuration"; -import {http_get_async, http_post_async} from "../_fetch"; - -export const http_password_reset_request = { - create_request_async(username: string): Promise<Response> { - return http_get_async(api_base("_/password-reset-request/create?for_user=" + username)); - }, - check_request_async(publicId: string): Promise<Response> { - return http_get_async(api_base("_/password-reset-request/is-valid?id=" + publicId)); - }, - fulfill_request_async(publicId: string, newPassword: string): Promise<Response> { - return http_post_async(api_base("_/password-reset-request/fulfill"), {id: publicId, newPassword}); - }, -};
\ No newline at end of file diff --git a/code/app/src/lib/services/abstractions/IPasswordResetService.ts b/code/app/src/lib/services/abstractions/IPasswordResetService.ts new file mode 100644 index 0000000..b6f6671 --- /dev/null +++ b/code/app/src/lib/services/abstractions/IPasswordResetService.ts @@ -0,0 +1,21 @@ +import type { KnownProblem } from "$lib/models/internal/KnownProblem" + +export interface IPasswordResetService { + create_request_async(email: string): Promise<CreateRequestResponse>, + fulfill_request_async(id: string, newPassword: string): Promise<FulfillRequestResponse>, + request_is_valid_async(id: string): Promise<RequestIsValidResponse> +} + +export type RequestIsValidResponse = { + isValid: boolean +} + +export type FulfillRequestResponse = { + isFulfilled: boolean, + knownProblem?: KnownProblem +} + +export type CreateRequestResponse = { + isCreated: boolean, + knownProblem?: KnownProblem +}
\ No newline at end of file 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 |
