From 07e014cd8f046331bd53585b420b449f19feff42 Mon Sep 17 00:00:00 2001 From: sprechtl Date: Mon, 10 Oct 2022 00:10:11 +0200 Subject: [PATCH 1/5] new folder structure --- frontend/svelte/src/models/{ => repos/note}/NoteRepository.ts | 2 +- .../svelte/src/models/{ => repos/note}/StrapiNoteRepository.ts | 2 +- frontend/svelte/src/models/repos/user/StrapiUserRepo.ts | 0 frontend/svelte/src/models/repos/user/UserRepository.ts | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename frontend/svelte/src/models/{ => repos/note}/NoteRepository.ts (87%) rename frontend/svelte/src/models/{ => repos/note}/StrapiNoteRepository.ts (98%) create mode 100644 frontend/svelte/src/models/repos/user/StrapiUserRepo.ts create mode 100644 frontend/svelte/src/models/repos/user/UserRepository.ts diff --git a/frontend/svelte/src/models/NoteRepository.ts b/frontend/svelte/src/models/repos/note/NoteRepository.ts similarity index 87% rename from frontend/svelte/src/models/NoteRepository.ts rename to frontend/svelte/src/models/repos/note/NoteRepository.ts index 1cae27d..4fcda1e 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 98% rename from frontend/svelte/src/models/StrapiNoteRepository.ts rename to frontend/svelte/src/models/repos/note/StrapiNoteRepository.ts index 8b3f668..fff1952 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"; 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..e69de29 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..e69de29 From d66b9272b775efec7ef04da8f1d32a59cbe95c1f Mon Sep 17 00:00:00 2001 From: sprechtl Date: Mon, 10 Oct 2022 01:21:53 +0200 Subject: [PATCH 2/5] Repo for USER started --- frontend/svelte/src/models/PomeloUtils.ts | 24 +++--- .../login => }/models/authentication.ts | 2 +- .../models/repos/note/StrapiNoteRepository.ts | 2 +- .../src/models/repos/user/StrapiUserRepo.ts | 77 +++++++++++++++++++ .../src/models/repos/user/UserRepository.ts | 19 +++++ frontend/svelte/src/routes/+page.svelte | 11 +-- frontend/svelte/src/routes/login/+page.svelte | 51 ++++-------- .../svelte/src/routes/register/+page.svelte | 46 ++++------- 8 files changed, 146 insertions(+), 86 deletions(-) rename frontend/svelte/src/{routes/login => }/models/authentication.ts (70%) 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 @@