From ad23d05000f4b7c745dba1809c8c3cbe1b370147 Mon Sep 17 00:00:00 2001 From: sprechtl Date: Sun, 16 Oct 2022 23:42:40 +0200 Subject: [PATCH] No beauty but worky --- .../src/models/repos/user/StrapiUserRepo.ts | 27 +++++++++++-------- frontend/svelte/src/routes/+page.svelte | 4 ++- frontend/svelte/src/routes/login/+page.svelte | 10 ++----- .../svelte/src/routes/register/+page.svelte | 2 +- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts b/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts index 959bc51..58f2ea2 100644 --- a/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts +++ b/frontend/svelte/src/models/repos/user/StrapiUserRepo.ts @@ -1,25 +1,26 @@ 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"; +import {error} from "@sveltejs/kit"; +import {User} from "../../user"; export class StrapiUserRepo implements UserRepository { private static instance: StrapiUserRepo; - public static getInstance(): StrapiUserRepo { + public static getInstance(verification: boolean = true): 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"; + this.instance.verify().then(() => { + if (verification && !this.instance.verified) { + window.location.href = "/login"; } }); } return this.instance; } - public currentUser?: Authentication; + private verified: boolean = false; private constructor() { } @@ -29,14 +30,18 @@ export class StrapiUserRepo implements UserRepository { private static apiUserEndpoint: string = StrapiUserRepo.api + "/auth/local" /** - * Sets the current user. + * Verifies the current users jwt. * @private */ - private async setup() { - this.currentUser = await this.getMe(parseCookies().jwt); + private async verify() { + this.verified = false; + let result = await this.getMe(); + if (!result.error) { + this.verified = true; + } } - async getMe(jwt: string): Promise { + async getMe(): Promise { const response = await StrapiUserRepo.fetchStrapi("/me", "GET", null, true, "/users") return await response.json(); } @@ -70,7 +75,7 @@ export class StrapiUserRepo implements UserRepository { 'Accept': 'application/json', 'Content-Type': 'application/json' } - } else if (authorization){ + } else if (authorization) { requestInit["headers"] = { authorization: StrapiNoteRepository.getAuthorizationHeader() ?? '', } diff --git a/frontend/svelte/src/routes/+page.svelte b/frontend/svelte/src/routes/+page.svelte index ef1e0fc..28d5957 100644 --- a/frontend/svelte/src/routes/+page.svelte +++ b/frontend/svelte/src/routes/+page.svelte @@ -1,12 +1,14 @@