No beauty but worky
This commit is contained in:
parent
6fd7726ae0
commit
ad23d05000
4 changed files with 22 additions and 21 deletions
|
|
@ -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<Authentication> {
|
||||
async getMe(): Promise<Authentication> {
|
||||
const response = await StrapiUserRepo.fetchStrapi("/me", "GET", null, true, "/users")
|
||||
return await response.json();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
<script lang="ts">
|
||||
import type {Note} from "../models/types";
|
||||
import {onMount} from "svelte";
|
||||
import {StrapiNoteRepository} from "../models/StrapiNoteRepository";
|
||||
import {StrapiNoteRepository} from "../models/repos/note/StrapiNoteRepository";
|
||||
import {StrapiUserRepo} from "../models/repos/user/StrapiUserRepo";
|
||||
|
||||
const noteRepo: StrapiNoteRepository = StrapiNoteRepository.getInstance();
|
||||
let notes: Note[];
|
||||
|
||||
onMount(async () => {
|
||||
StrapiUserRepo.getInstance();
|
||||
notes = await noteRepo.getNotes();
|
||||
notes.forEach(note => {
|
||||
note.lastViewed = new Date(note.lastViewed);
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
* Handles the button click.
|
||||
*/
|
||||
async function handleSubmit() {
|
||||
const userRepo: StrapiUserRepo = StrapiUserRepo.getInstance();
|
||||
const userRepo: StrapiUserRepo = StrapiUserRepo.getInstance(false);
|
||||
|
||||
const response = await userRepo.loginUser(user, password);
|
||||
|
||||
|
|
@ -50,7 +50,7 @@
|
|||
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>
|
||||
|
||||
<div class="form-floating">
|
||||
<input bind:value={user} class="form-control" id="floatingInput" placeholder="name@example.com" type="email">
|
||||
<input bind:value={user} class="form-control" id="floatingInput" placeholder="name@example.com" type="text">
|
||||
<label for="floatingInput">Email address or username</label>
|
||||
</div>
|
||||
<div class="form-floating">
|
||||
|
|
@ -75,10 +75,4 @@
|
|||
<style>
|
||||
@import "../../userInput.css";
|
||||
@import "../../customBootstrap.css";
|
||||
|
||||
.form-signin input[type="email"] {
|
||||
margin-bottom: -1px !important;
|
||||
border-bottom-right-radius: 0 !important;
|
||||
border-bottom-left-radius: 0 !important;
|
||||
}
|
||||
</style>
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
* Handles the button click.
|
||||
*/
|
||||
async function handleSubmit() {
|
||||
const userRepo: StrapiUserRepo = StrapiUserRepo.getInstance();
|
||||
const userRepo: StrapiUserRepo = StrapiUserRepo.getInstance(false);
|
||||
|
||||
const response = await userRepo.registerUser(email, user, password);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue