Merge branch 'user-repo' into develop
This commit is contained in:
commit
fa91d5f670
10 changed files with 166 additions and 109 deletions
10
frontend/svelte/src/models/repos/note/NoteRepository.ts
Normal file
10
frontend/svelte/src/models/repos/note/NoteRepository.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import type {Note} from "../../types";
|
||||
|
||||
export interface NoteRepository {
|
||||
getNotes(): Promise<Note[]>;
|
||||
getNote(id: number): Promise<Note>;
|
||||
getCurrentNote(): Promise<Note | void>;
|
||||
updateNote(id: number, note: Partial<Note>): Promise<Note>;
|
||||
deleteNote(id: number): void;
|
||||
createNote(note: Partial<Note> & Pick<Note, 'title'>): Promise<Note>;
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
import type {Note} from "../../types";
|
||||
import {parseCookies} from "nookies";
|
||||
import type {NoteRepository} from "./NoteRepository";
|
||||
import {currentNoteId} from "../stores";
|
||||
|
||||
|
||||
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
|
||||
|
||||
export class StrapiNoteRepository implements NoteRepository {
|
||||
private static instance: StrapiNoteRepository;
|
||||
public static getInstance(): StrapiNoteRepository {
|
||||
if (this.instance === undefined || this.instance === null) {
|
||||
this.instance = new StrapiNoteRepository();
|
||||
}
|
||||
return this.instance;
|
||||
}
|
||||
|
||||
private constructor() {
|
||||
currentNoteId.subscribe((value) => (this._currentNoteId = value));
|
||||
}
|
||||
|
||||
private _currentNoteId: unknown;
|
||||
private static apiNoteEndpoint: string = "http://localhost:1337/api/notes"
|
||||
|
||||
public set currentNoteId(value: number | undefined) {
|
||||
currentNoteId.set(value || -1);
|
||||
}
|
||||
|
||||
public get currentNoteId(): number {
|
||||
return <number>this._currentNoteId;
|
||||
}
|
||||
|
||||
public async getNotes(): Promise<Note[]>{
|
||||
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/", 'GET');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getNote(id: number): Promise<Note>{
|
||||
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/" + id, 'GET');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getCurrentNote(): Promise<Note | void> {
|
||||
if (this._currentNoteId === null || this._currentNoteId === undefined) {
|
||||
return;
|
||||
}
|
||||
return await this.getNote(this.currentNoteId);
|
||||
}
|
||||
|
||||
public async updateNote(id: number, note: Partial<Note>): Promise<Note> {
|
||||
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/" + id, 'PUT', note);
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async createNote(note: Partial<Note> & Pick<Note, 'title'>): Promise<Note> {
|
||||
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/", 'POST', note);
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async deleteNote(id: number): Promise<void> {
|
||||
await StrapiNoteRepository.fetchStrapiNoteEndpoint("/" + id, 'DELETE');
|
||||
}
|
||||
|
||||
private static async fetchStrapiNoteEndpoint(path: string, method: HttpMethod, body: Partial<Note> | null = null): Promise<Response> {
|
||||
let requestInit: RequestInit = {
|
||||
method: method,
|
||||
headers: {
|
||||
authorization: StrapiNoteRepository.getAuthorizationHeader()
|
||||
}
|
||||
};
|
||||
if (body) {
|
||||
requestInit["body"] = JSON.stringify({data: body});
|
||||
}
|
||||
return await fetch(StrapiNoteRepository.apiNoteEndpoint + path, requestInit);
|
||||
}
|
||||
|
||||
private static mockedGetAuthorizationHeader() {
|
||||
return "bearer TOKEN"
|
||||
}
|
||||
|
||||
static getAuthorizationHeader() {
|
||||
const jwt = parseCookies('/').jwt;
|
||||
return `bearer ${jwt}`
|
||||
}
|
||||
}
|
||||
93
frontend/svelte/src/models/repos/user/StrapiUserRepo.ts
Normal file
93
frontend/svelte/src/models/repos/user/StrapiUserRepo.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
19
frontend/svelte/src/models/repos/user/UserRepository.ts
Normal file
19
frontend/svelte/src/models/repos/user/UserRepository.ts
Normal 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>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue