jwt oba nix geht

This commit is contained in:
dhain 2022-09-26 23:07:39 +02:00
parent 15ed4bd993
commit 1c1d758ab1
4 changed files with 45 additions and 32 deletions

View file

@ -1,3 +1,5 @@
import {parseCookies} from "nookies";
/**
* Capitalises first letter of string.
* @param str
@ -18,4 +20,14 @@ export async function bearerFetch(endpoint: string, jwt: string, baseUrl: string
Authorization: `Bearer ${jwt}`
}
});
}
}
const getJwtCookie = () => {
return parseCookies("/").jwt;
};
/**
* JWT Cookie
*/
export const jwt: string = getJwtCookie();

View file

@ -1,40 +1,23 @@
<script lang="ts">
import type {Note} from "../types";
import {bearerFetch, jwt} from "../models/PomeloUtils";
import {onMount} from "svelte";
import {parseCookies} from "nookies";
import {bearerFetch} from "../models/PomeloUtils";
import {User} from "../models/user";
let user: User;
const endpoint = "/notes";
let notes: Note[];
// onMount(async () => {
// const response = await bearerFetch(endpoint, jwt);
// debugger;
// var data = await response.json();
// console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
// });
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";
}
onMount(() => {
console.log("snasidbsa dghsasa");
});
//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\"}]";
//TODO: TEMP!!!
let notes: Note[] = JSON.parse(tempJson);
console.log(notes);
sortNotesByDate();
window.localStorage.setItem("notes", JSON.stringify(notes));

View file

@ -0,0 +1,17 @@
import {bearerFetch, jwt} from "../models/PomeloUtils";
/** @type {import('./$types').PageLoad} */
export async function load({params}) {
let invalid = !jwt;
if (!invalid) {
const request = await bearerFetch("/users/me", jwt);
const response = await request.json();
invalid = "error" in response;
}
if (invalid) {
window.location = "/login";
}
}