Merge branch 'login' into develop
This commit is contained in:
commit
7f2e30c587
6 changed files with 60 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,6 +1,10 @@
|
|||
import {toast} from "@zerodevx/svelte-toast";
|
||||
import {capitalizeFirstLetter} from "./PomeloUtils";
|
||||
|
||||
/**
|
||||
* Creates an error message toast with red background.
|
||||
* @param m
|
||||
*/
|
||||
export const createErrorToast = (m: string) => {
|
||||
toast.push("Error: " + capitalizeFirstLetter(m), {
|
||||
theme: {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
/**
|
||||
* User.
|
||||
*/
|
||||
export class User {
|
||||
id: number;
|
||||
email: string;
|
||||
|
|
|
|||
|
|
@ -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\"}]";
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@
|
|||
let rememberMe: boolean = true;
|
||||
|
||||
|
||||
/**
|
||||
* Handles the button click.
|
||||
*/
|
||||
async function handleSubmit() {
|
||||
const endpoint = "http://localhost:1337/api/auth/local";
|
||||
const payload = {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
import type {User} from "../../../models/user";
|
||||
|
||||
/**
|
||||
* User Login Auth.
|
||||
*/
|
||||
export interface Authentication {
|
||||
jwt: string;
|
||||
user: User;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue