Merge branch 'login' into listing
This commit is contained in:
commit
15ed4bd993
2 changed files with 47 additions and 0 deletions
|
|
@ -1,3 +1,21 @@
|
|||
/**
|
||||
* Capitalises first letter of string.
|
||||
* @param str
|
||||
*/
|
||||
export function capitalizeFirstLetter(str: string) {
|
||||
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches with applied bearer token.
|
||||
* @param endpoint ex.: /users/me
|
||||
* @param jwt Java Web Token used to authorize
|
||||
* @param baseUrl Base Url of request
|
||||
*/
|
||||
export async function bearerFetch(endpoint: string, jwt: string, baseUrl: string = "http://localhost:1337/api") {
|
||||
return await fetch(baseUrl + endpoint, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${jwt}`
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -1,5 +1,34 @@
|
|||
<script lang="ts">
|
||||
import type {Note} from "../types";
|
||||
import {onMount} from "svelte";
|
||||
import {parseCookies} from "nookies";
|
||||
import {bearerFetch} from "../models/PomeloUtils";
|
||||
import {User} from "../models/user";
|
||||
|
||||
let user: User;
|
||||
|
||||
|
||||
onMount(async () => {
|
||||
const jwt = parseCookies("/").jwt;
|
||||
let invalid = !jwt;
|
||||
|
||||
if (!invalid) {
|
||||
const request = await bearerFetch("/users/me", jwt);
|
||||
const response = await request.json();
|
||||
|
||||
if ('error' in response){
|
||||
invalid = true;
|
||||
} else {
|
||||
user = new User(response);
|
||||
}
|
||||
}
|
||||
|
||||
if (invalid) {
|
||||
window.location = "/login";
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
//TODO: TEMP!!!
|
||||
const tempJson = "[{\"id\":0,\"title\":\"samc\",\"content\":\"SAAAAAAAAAAMC\",\"lastOpened\":\"2022-09-25T10:45:49.903Z\"},{\"id\":1,\"title\":\"Push\",\"content\":\"Kollege Pusch\",\"lastOpened\":\"2022-09-25T10:50:49.903Z\"},{\"id\":2,\"title\":\"Mike\",\"content\":\"C Meister\",\"lastOpened\":\"2022-09-25T10:09:13.903Z\"},{\"id\":3,\"title\":\"kekw\",\"content\":\"OMEGALUL\",\"lastOpened\":\"2022-09-25T12:03:49.903Z\"}]";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue