Repo for USER started
This commit is contained in:
parent
07e014cd8f
commit
d66b9272b7
8 changed files with 146 additions and 86 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
frontend/svelte/src/models/authentication.ts
Normal file
10
frontend/svelte/src/models/authentication.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import type {User} from "./user";
|
||||
|
||||
/**
|
||||
* User Login Auth.
|
||||
*/
|
||||
export interface Authentication {
|
||||
jwt: string;
|
||||
user: User;
|
||||
error: any;
|
||||
}
|
||||
|
|
@ -66,7 +66,7 @@ export class StrapiNoteRepository implements NoteRepository {
|
|||
return "bearer TOKEN"
|
||||
}
|
||||
|
||||
private static getAuthorizationHeader() {
|
||||
static getAuthorizationHeader() {
|
||||
const jwt = parseCookies().jwt;
|
||||
return `bearer ${jwt}`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<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){
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue