Merge branch 'user-repo' into develop

This commit is contained in:
sprechtl 2022-10-16 23:44:18 +02:00
commit fa91d5f670
10 changed files with 166 additions and 109 deletions

View file

@ -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();
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);
}
}
}

View file

@ -0,0 +1,10 @@
import type {User} from "./user";
/**
* User Login Auth.
*/
export interface Authentication {
jwt: string;
user: User;
error: any;
}

View file

@ -1,4 +1,4 @@
import type {Note} from "./types";
import type {Note} from "../../types";
export interface NoteRepository {
getNotes(): Promise<Note[]>;

View file

@ -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}`
}
}

View file

@ -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<Authentication> {
const response = await StrapiUserRepo.fetchStrapi("/me", "GET", null, true, "/users")
return await response.json();
}
async registerUser(email: string, username: string, password: string): Promise<Authentication> {
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<Authentication> {
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<Response> {
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);
}
}

View file

@ -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<Authentication>;
/**
* Gets the current user.
* @param jwt
*/
getMe(jwt: string): Promise<Authentication>;
loginUser(identifier: string, password: string): Promise<Authentication>;
}