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

@ -21,8 +21,9 @@
}, },
"owners": { "owners": {
"type": "relation", "type": "relation",
"relation": "oneToMany", "relation": "manyToMany",
"target": "plugin::users-permissions.user" "target": "plugin::users-permissions.user",
"inversedBy": "notes"
}, },
"lastViewed": { "lastViewed": {
"type": "datetime", "type": "datetime",

View file

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

View file

@ -1,40 +1,23 @@
<script lang="ts"> <script lang="ts">
import type {Note} from "../types"; import type {Note} from "../types";
import {bearerFetch, jwt} from "../models/PomeloUtils";
import {onMount} from "svelte"; 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 () => { onMount(() => {
const jwt = parseCookies("/").jwt; console.log("snasidbsa dghsasa");
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";
}
}); });
console.log(notes);
//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);
sortNotesByDate(); sortNotesByDate();
window.localStorage.setItem("notes", JSON.stringify(notes)); 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";
}
}