diff --git a/frontend/svelte/src/models/PomeloUtils.ts b/frontend/svelte/src/models/PomeloUtils.ts index 5d290ae..cfec11b 100644 --- a/frontend/svelte/src/models/PomeloUtils.ts +++ b/frontend/svelte/src/models/PomeloUtils.ts @@ -1,4 +1,5 @@ -import {parseCookies} from "nookies"; +import type {Authentication} from "./authentication"; +import {createErrorToast} from "./customToasts"; /** * Capitalises first letter of string. @@ -22,13 +23,14 @@ export async function bearerFetch(endpoint: string, jwt: string, baseUrl: string }); } - -const getJwtCookie = () => { - // @ts-ignore - return parseCookies("/").jwt; -}; - -/** - * JWT Cookie - */ -export const jwt: string = getJwtCookie(); \ No newline at end of file +export function handleErrorsFromResponseWithToast(response: Authentication) { + if (response.error != null) { + if (response.error.details.errors) { + for (const error of response.error.details.errors) { + createErrorToast(error.message); + } + } else { + createErrorToast(response.error.message); + } + } +} \ No newline at end of file diff --git a/frontend/svelte/src/routes/login/models/authentication.ts b/frontend/svelte/src/models/authentication.ts similarity index 70% rename from frontend/svelte/src/routes/login/models/authentication.ts rename to frontend/svelte/src/models/authentication.ts index dccb434..c5adc10 100644 --- a/frontend/svelte/src/routes/login/models/authentication.ts +++ b/frontend/svelte/src/models/authentication.ts @@ -1,4 +1,4 @@ -import type {User} from "../../../models/user"; +import type {User} from "./user"; /** * User Login Auth. diff --git a/frontend/svelte/src/models/repos/note/StrapiNoteRepository.ts b/frontend/svelte/src/models/repos/note/StrapiNoteRepository.ts index fff1952..2a4ef83 100644 --- a/frontend/svelte/src/models/repos/note/StrapiNoteRepository.ts +++ b/frontend/svelte/src/models/repos/note/StrapiNoteRepository.ts @@ -66,7 +66,7 @@ export class StrapiNoteRepository implements NoteRepository { return "bearer TOKEN" } - private static getAuthorizationHeader() { + static getAuthorizationHeader() { const jwt = parseCookies().jwt; return `bearer ${jwt}` } diff --git a/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts b/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts index e69de29..7c08cd5 100644 --- a/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts +++ b/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts @@ -0,0 +1,77 @@ +import type {UserRepository} from "./UserRepository"; +import type {Authentication} from "../../authentication"; +import {parseCookies} from "nookies"; +import type {HttpMethod} from "@sveltejs/kit/types/private"; +import {StrapiNoteRepository} from "../note/StrapiNoteRepository"; + +export class StrapiUserRepo implements UserRepository { + private static instance: StrapiUserRepo; + + public static getInstance(): StrapiUserRepo { + if (this.instance === undefined || this.instance === null) { + this.instance = new StrapiUserRepo(); + this.instance.setup().then(() => { + if (!this.instance.currentUser?.jwt) { + window.location.href = "/login"; + } + }); + } + return this.instance; + } + + public currentUser?: Authentication; + + private constructor() { + } + + private static api: string = "http://localhost:1337/api" + + private static apiUserEndpoint: string = StrapiUserRepo.api + "/local/auth" + + /** + * Sets the current user. + * @private + */ + private async setup() { + this.currentUser = await this.getMe(parseCookies().jwt); + } + + async getMe(jwt: string): Promise { + const response = await StrapiUserRepo.fetchStrapi("/me", "GET", null, true, "/users") + return await response.json(); + } + + async registerUser(email: string, username: string, password: string): Promise { + const payload = { + email: email, + password: password, + username: username + }; + const response = await StrapiUserRepo.fetchStrapi("/register", "POST", payload, false); + return await response.json(); + } + + async loginUser(identifier: string, password: string): Promise { + const payload = { + identifier: identifier, + password: password + }; + const response = await StrapiUserRepo.fetchStrapi("/", "POST", payload, false); + return response.json(); + } + + private static async fetchStrapi(path: string, method: HttpMethod, body: any | null = null, authorization: boolean = true, customPath: any = null): Promise { + let requestInit: RequestInit = { + method: method, + }; + if (authorization){ + requestInit["headers"] = { + authorization: StrapiNoteRepository.getAuthorizationHeader(), + } + } + if (body) { + requestInit["body"] = JSON.stringify({data: body}); + } + return await fetch((customPath) ? (this.api + customPath + path) : StrapiUserRepo.apiUserEndpoint + path, requestInit); + } +} \ No newline at end of file diff --git a/frontend/svelte/src/models/repos/user/UserRepository.ts b/frontend/svelte/src/models/repos/user/UserRepository.ts index e69de29..3128bfb 100644 --- a/frontend/svelte/src/models/repos/user/UserRepository.ts +++ b/frontend/svelte/src/models/repos/user/UserRepository.ts @@ -0,0 +1,19 @@ +import type {Authentication} from "../../authentication"; + +export interface UserRepository { + /** + * Registers a new user. + * @param email + * @param username + * @param password + */ + registerUser(email: string, username: string, password: string): Promise; + + /** + * Gets the current user. + * @param jwt + */ + getMe(jwt: string): Promise; + + loginUser(identifier: string, password: string): Promise; +} \ No newline at end of file diff --git a/frontend/svelte/src/routes/+page.svelte b/frontend/svelte/src/routes/+page.svelte index f52d418..d276084 100644 --- a/frontend/svelte/src/routes/+page.svelte +++ b/frontend/svelte/src/routes/+page.svelte @@ -1,7 +1,8 @@