Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend: Hide Elements based on Login #167

Merged
merged 13 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion backend/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ INSERT INTO species(name) VALUES ('unknown');

/*
* Inserts test user into user table
* Email: test
* Password: test
*/
INSERT INTO users(username, email, pword, admin) VALUES ('test', 'garvina@oregonstate.edu', '$2b$12$2Z74k3vqzchWB..McZbdUOp4BXd6RGsWcS0atZJfVVheGexvH7i0O', '1');
INSERT INTO users(username, email, pword, admin) VALUES ('test', 'test', '$2b$12$PFoNf7YM0KNIPP8WGsJjveIOhiJjitsMtfwRcCjdcyTuqjdk/q//u', '1');
21 changes: 20 additions & 1 deletion frontend/src/lib/Header.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
<script lang="ts">
import logo from "../images/logo.svg";
import { links } from "svelte-routing";
import { onMount } from "svelte";
import {
UploadOutline,
UserOutline,
TableRowOutline,
BookOutline,
} from "flowbite-svelte-icons";
import {user} from "./stores/user"
import Cookies from "js-cookie"

// Checking if the user has a cookie.
// If they do, set user status for the whole site.
onMount(async () => {
if (Cookies.get("auth")) {
$user.loggedIn = true
}
});
</script>

<header class="flex justify-between" use:links>
Expand All @@ -20,17 +31,25 @@
<a href="/search" class="flex items-center gap-1"
><TableRowOutline size="lg" />Search</a
>
{#if $user.loggedIn}
<a href="/upload" class="flex items-center gap-1">
<UploadOutline size="lg" />Upload</a
>
{/if}
<a href="/tutorials" class="flex items-center gap-1">
<BookOutline size="lg" />Tutorials</a
>
</div>
</div>

<a href="/login" class="flex items-center gap-1 mr-5">
<UserOutline size="lg" />Login</a
<UserOutline size="lg" />
{#if $user.loggedIn}
Logout
{:else}
Login
{/if}
</a
>
</header>
<div style="height: 60px;" />
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/lib/stores/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {writable} from 'svelte/store';

export const user = writable({
loggedIn: false,
username: "",
admin: true
})
6 changes: 6 additions & 0 deletions frontend/src/routes/Edit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { navigate } from "svelte-routing";
import { onMount } from "svelte";
import ArticleEditor from "../lib/ArticleEditor.svelte";
import {user} from "../lib/stores/user"

// key difference, here we get the information, then populate it in the upload form that can be edited
// and reuploaded/edited
Expand All @@ -26,6 +27,11 @@

// when this component mounts, request protein wikipedia entry from backend
onMount(async () => {
if (!$user.loggedIn) {
alert("You are not logged in. You are being redirected to home. TODO: Make this better.")
navigate("/")
}

// Request the protein from backend given ID
console.log("Requesting", urlId, "info from backend");

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes/Error.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

<style>
/* put stuff here */
</style>
</style>
20 changes: 19 additions & 1 deletion frontend/src/routes/Login.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
import { Backend, type LoginResponse } from "../lib/backend";
import { Button, Label, Input } from "flowbite-svelte";
import Cookies from "js-cookie";
import { onMount } from "svelte";
import { navigate } from "svelte-routing";
import { user } from "../lib/stores/user"

/*
* Deletes the cookie upon entering the login page.
* We want to do this to avoid bugs, and to let the user log out.
*/
onMount(async () => {
Cookies.remove("auth")
$user.loggedIn = false
$user.username = ""
$user.admin = true
});

let email: string = "";
let password: string = "";
Expand Down Expand Up @@ -32,18 +46,22 @@
// User entered wrong username or password, or account doesn't exist.
// @todo Display this error message to the user.
console.log("Response received. Error: " + result["error"]);
alert(result["error"])
} else if (result["token"] != "") {
// User entered the correct username / password and got a result.
// @todo Store this in a cookie.
console.log("Response received. Token: " + result["token"]);
Cookies.set("auth", result["token"]);
$user.loggedIn = true
navigate(`/search`);
} else {
// User got a result, but both fields are null. This should never happen.
console.log(
"Unexpected edge cage regarding user authentication."
);
}
} catch (e) {
alert([e])
console.log(e);
}
}
Expand All @@ -53,7 +71,7 @@
<title>Login</title>
</svelte:head>

<form on:submit={submitForm} class="flex gap-5 flex-col p-5">
<form on:submit|preventDefault={submitForm} class="flex gap-5 flex-col p-5">
<div>
<Label for="email">Email</Label>
<Input
Expand Down
14 changes: 9 additions & 5 deletions frontend/src/routes/Protein.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import EntryCard from "../lib/EntryCard.svelte";
import SimilarProteins from "../lib/SimilarProteins.svelte";
import DelayedSpinner from "../lib/DelayedSpinner.svelte";
import { user } from "../lib/stores/user";

const fileDownloadDropdown = ["pdb", "fasta"];

Expand Down Expand Up @@ -92,11 +93,14 @@
>
{/each}
</Dropdown>
<Button
on:click={async () => {
navigate(`/edit/${entry?.name}`);
}}><PenOutline class="mr-2" size="lg" />Edit Entry</Button
>
{#if $user.loggedIn}
<Button
on:click={async () => {
navigate(`/edit/${entry?.name}`);
}}
><PenOutline class="mr-2" size="lg" />Edit Entry</Button
>
{/if}
</div>

<EntryCard title="Provided Information">
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/routes/Upload.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
import { fileToString } from "../lib/format";
import ArticleEditor from "../lib/ArticleEditor.svelte";
import { onMount } from "svelte";
import Cookies from "js-cookie";
import {user} from "../lib/stores/user"

let species: string[] | null;
let selectedSpecies: string = "unknown";

const authToken = Cookies.get("auth")
onMount(async () => {
if (!$user.loggedIn) {
alert("You are not logged in. You are being redirected to home. TODO: Make this better.")
navigate("/")
}
species = await Backend.searchSpecies();
});

Expand Down
Loading