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 {UserRepository} from "./UserRepository";
|
||||||
import type {Authentication} from "../../authentication";
|
import type {Authentication} from "../../authentication";
|
||||||
import {parseCookies} from "nookies";
|
|
||||||
import type {HttpMethod} from "@sveltejs/kit/types/private";
|
import type {HttpMethod} from "@sveltejs/kit/types/private";
|
||||||
import {StrapiNoteRepository} from "../note/StrapiNoteRepository";
|
import {StrapiNoteRepository} from "../note/StrapiNoteRepository";
|
||||||
|
import {error} from "@sveltejs/kit";
|
||||||
|
import {User} from "../../user";
|
||||||
|
|
||||||
export class StrapiUserRepo implements UserRepository {
|
export class StrapiUserRepo implements UserRepository {
|
||||||
private static instance: StrapiUserRepo;
|
private static instance: StrapiUserRepo;
|
||||||
|
|
||||||
public static getInstance(): StrapiUserRepo {
|
public static getInstance(verification: boolean = true): StrapiUserRepo {
|
||||||
if (this.instance === undefined || this.instance === null) {
|
if (this.instance === undefined || this.instance === null) {
|
||||||
this.instance = new StrapiUserRepo();
|
this.instance = new StrapiUserRepo();
|
||||||
this.instance.setup().then(() => {
|
this.instance.verify().then(() => {
|
||||||
if (!this.instance.currentUser?.jwt) {
|
if (verification && !this.instance.verified) {
|
||||||
//window.location.href = "/login";
|
window.location.href = "/login";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return this.instance;
|
return this.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public currentUser?: Authentication;
|
private verified: boolean = false;
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
}
|
}
|
||||||
|
|
@ -29,14 +30,18 @@ export class StrapiUserRepo implements UserRepository {
|
||||||
private static apiUserEndpoint: string = StrapiUserRepo.api + "/auth/local"
|
private static apiUserEndpoint: string = StrapiUserRepo.api + "/auth/local"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the current user.
|
* Verifies the current users jwt.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private async setup() {
|
private async verify() {
|
||||||
this.currentUser = await this.getMe(parseCookies().jwt);
|
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")
|
const response = await StrapiUserRepo.fetchStrapi("/me", "GET", null, true, "/users")
|
||||||
return await response.json();
|
return await response.json();
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +75,7 @@ export class StrapiUserRepo implements UserRepository {
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
} else if (authorization){
|
} else if (authorization) {
|
||||||
requestInit["headers"] = {
|
requestInit["headers"] = {
|
||||||
authorization: StrapiNoteRepository.getAuthorizationHeader() ?? '',
|
authorization: StrapiNoteRepository.getAuthorizationHeader() ?? '',
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type {Note} from "../models/types";
|
import type {Note} from "../models/types";
|
||||||
import {onMount} from "svelte";
|
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();
|
const noteRepo: StrapiNoteRepository = StrapiNoteRepository.getInstance();
|
||||||
let notes: Note[];
|
let notes: Note[];
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
|
StrapiUserRepo.getInstance();
|
||||||
notes = await noteRepo.getNotes();
|
notes = await noteRepo.getNotes();
|
||||||
notes.forEach(note => {
|
notes.forEach(note => {
|
||||||
note.lastViewed = new Date(note.lastViewed);
|
note.lastViewed = new Date(note.lastViewed);
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
* Handles the button click.
|
* Handles the button click.
|
||||||
*/
|
*/
|
||||||
async function handleSubmit() {
|
async function handleSubmit() {
|
||||||
const userRepo: StrapiUserRepo = StrapiUserRepo.getInstance();
|
const userRepo: StrapiUserRepo = StrapiUserRepo.getInstance(false);
|
||||||
|
|
||||||
const response = await userRepo.loginUser(user, password);
|
const response = await userRepo.loginUser(user, password);
|
||||||
|
|
||||||
|
|
@ -50,7 +50,7 @@
|
||||||
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>
|
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>
|
||||||
|
|
||||||
<div class="form-floating">
|
<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>
|
<label for="floatingInput">Email address or username</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-floating">
|
<div class="form-floating">
|
||||||
|
|
@ -75,10 +75,4 @@
|
||||||
<style>
|
<style>
|
||||||
@import "../../userInput.css";
|
@import "../../userInput.css";
|
||||||
@import "../../customBootstrap.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>
|
</style>
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
* Handles the button click.
|
* Handles the button click.
|
||||||
*/
|
*/
|
||||||
async function handleSubmit() {
|
async function handleSubmit() {
|
||||||
const userRepo: StrapiUserRepo = StrapiUserRepo.getInstance();
|
const userRepo: StrapiUserRepo = StrapiUserRepo.getInstance(false);
|
||||||
|
|
||||||
const response = await userRepo.registerUser(email, user, password);
|
const response = await userRepo.registerUser(email, user, password);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue