Skip to content

Commit

Permalink
Added ability to set and clear token headers from front end
Browse files Browse the repository at this point in the history
  • Loading branch information
ansengarvin committed Feb 26, 2024
1 parent 96e679e commit fb0b3ff
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
13 changes: 13 additions & 0 deletions frontend/src/lib/backend.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
export * from "./openapi";
export { DefaultService as Backend } from "./openapi";
import { OpenAPI } from "./openapi";
import Cookies from "js-cookie";

export const BACKEND_URL = "http://localhost:8000";
OpenAPI.BASE = BACKEND_URL;

// Sets the token header to the stored authentication token from the cookie
export function setToken() {
console.log("Setting Token")
OpenAPI.TOKEN = Cookies.get("auth")
}

// Sets the token header to nothing.
export function clearToken() {
console.log("Clearing token.")
OpenAPI.TOKEN = undefined
}
4 changes: 3 additions & 1 deletion frontend/src/routes/Edit.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { Backend, UploadError, type ProteinEntry } from "../lib/backend";
import { Backend, UploadError, setToken, type ProteinEntry } from "../lib/backend";
import { Button, Input, Label, Helper, Select } from "flowbite-svelte";
import { navigate } from "svelte-routing";
import { onMount } from "svelte";
Expand Down Expand Up @@ -140,6 +140,7 @@
on:click={async () => {
if (entry) {
try {
setToken()
const err = await Backend.editProteinEntry({
newName: name,
oldName: entry.name,
Expand Down Expand Up @@ -179,6 +180,7 @@
<Button
color="red"
on:click={async () => {
setToken()
await Backend.deleteProteinEntry(urlId);
navigate("/");
}}>Delete Protein Entry</Button
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/routes/Login.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { Backend, type LoginResponse } from "../lib/backend";
import { Backend, clearToken, type LoginResponse } from "../lib/backend";
import { Button, Label, Input } from "flowbite-svelte";
import Cookies from "js-cookie";
import { onMount } from "svelte";
Expand All @@ -11,6 +11,7 @@
* We want to do this to avoid bugs, and to let the user log out.
*/
onMount(async () => {
clearToken()
Cookies.remove("auth")
$user.loggedIn = false
$user.username = ""
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/routes/Upload.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { Backend, UploadError } from "../lib/backend";
import { Backend, UploadError, setToken } from "../lib/backend";
import {
Fileupload,
Button,
Expand Down Expand Up @@ -108,6 +108,7 @@

const pdbFileStr = await fileToString(file);
try {
setToken()
const err = await Backend.uploadProteinEntry({
name,
description,
Expand Down

0 comments on commit fb0b3ff

Please sign in to comment.