jwt oba nix geht
This commit is contained in:
parent
15ed4bd993
commit
1c1d758ab1
4 changed files with 45 additions and 32 deletions
|
|
@ -21,8 +21,9 @@
|
|||
},
|
||||
"owners": {
|
||||
"type": "relation",
|
||||
"relation": "oneToMany",
|
||||
"target": "plugin::users-permissions.user"
|
||||
"relation": "manyToMany",
|
||||
"target": "plugin::users-permissions.user",
|
||||
"inversedBy": "notes"
|
||||
},
|
||||
"lastViewed": {
|
||||
"type": "datetime",
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import {parseCookies} from "nookies";
|
||||
|
||||
/**
|
||||
* Capitalises first letter of string.
|
||||
* @param str
|
||||
|
|
@ -19,3 +21,13 @@ export async function bearerFetch(endpoint: string, jwt: string, baseUrl: string
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const getJwtCookie = () => {
|
||||
return parseCookies("/").jwt;
|
||||
};
|
||||
|
||||
/**
|
||||
* JWT Cookie
|
||||
*/
|
||||
export const jwt: string = getJwtCookie();
|
||||
|
|
@ -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));
|
||||
|
||||
|
|
|
|||
17
frontend/svelte/src/routes/+page.ts
Normal file
17
frontend/svelte/src/routes/+page.ts
Normal 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";
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue