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/NoteRepository.ts b/frontend/svelte/src/models/repos/note/NoteRepository.ts similarity index 89% rename from frontend/svelte/src/models/NoteRepository.ts rename to frontend/svelte/src/models/repos/note/NoteRepository.ts index c83d5a6..84b6705 100644 --- a/frontend/svelte/src/models/NoteRepository.ts +++ b/frontend/svelte/src/models/repos/note/NoteRepository.ts @@ -1,4 +1,4 @@ -import type {Note} from "./types"; +import type {Note} from "../../types"; export interface NoteRepository { getNotes(): Promise; diff --git a/frontend/svelte/src/models/StrapiNoteRepository.ts b/frontend/svelte/src/models/repos/note/StrapiNoteRepository.ts similarity index 95% rename from frontend/svelte/src/models/StrapiNoteRepository.ts rename to frontend/svelte/src/models/repos/note/StrapiNoteRepository.ts index fc06660..a340df9 100644 --- a/frontend/svelte/src/models/StrapiNoteRepository.ts +++ b/frontend/svelte/src/models/repos/note/StrapiNoteRepository.ts @@ -1,4 +1,4 @@ -import type {Note} from "./types"; +import type {Note} from "../../types"; import {parseCookies} from "nookies"; import type {NoteRepository} from "./NoteRepository"; import {currentNoteId} from "../stores"; @@ -78,8 +78,8 @@ export class StrapiNoteRepository implements NoteRepository { return "bearer TOKEN" } - private static getAuthorizationHeader() { - const jwt = parseCookies().jwt; + static getAuthorizationHeader() { + const jwt = parseCookies('/').jwt; return `bearer ${jwt}` } } \ No newline at end of file diff --git a/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts b/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts new file mode 100644 index 0000000..58f2ea2 --- /dev/null +++ b/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts @@ -0,0 +1,93 @@ +import type {UserRepository} from "./UserRepository"; +import type {Authentication} from "../../authentication"; +import type {HttpMethod} from "@sveltejs/kit/types/private"; +import {StrapiNoteRepository} from "../note/StrapiNoteRepository"; +import {error} from "@sveltejs/kit"; +import {User} from "../../user"; + +export class StrapiUserRepo implements UserRepository { + private static instance: StrapiUserRepo; + + public static getInstance(verification: boolean = true): StrapiUserRepo { + if (this.instance === undefined || this.instance === null) { + this.instance = new StrapiUserRepo(); + this.instance.verify().then(() => { + if (verification && !this.instance.verified) { + window.location.href = "/login"; + } + }); + } + return this.instance; + } + + private verified: boolean = false; + + private constructor() { + } + + private static api: string = "http://localhost:1337/api" + + private static apiUserEndpoint: string = StrapiUserRepo.api + "/auth/local" + + /** + * Verifies the current users jwt. + * @private + */ + private async verify() { + this.verified = false; + let result = await this.getMe(); + if (!result.error) { + this.verified = true; + } + } + + async getMe(): 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 && body) { + requestInit["headers"] = { + authorization: StrapiNoteRepository.getAuthorizationHeader() ?? '', + 'Accept': 'application/json', + 'Content-Type': 'application/json' + } + } else if (authorization) { + requestInit["headers"] = { + authorization: StrapiNoteRepository.getAuthorizationHeader() ?? '', + } + } else if (body) { + requestInit["headers"] = { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + } + } + if (body) { + requestInit["body"] = JSON.stringify(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 new file mode 100644 index 0000000..3128bfb --- /dev/null +++ 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 016d89f..28d5957 100644 --- a/frontend/svelte/src/routes/+page.svelte +++ b/frontend/svelte/src/routes/+page.svelte @@ -1,12 +1,14 @@