Daniel Bohry 9 місяців тому
батько
коміт
098aec7acd

+ 3 - 3
src/components/Header.svelte

@@ -35,10 +35,10 @@
 
 	<nav>
 		<ul>
-			<li aria-current={$page.url.pathname === '/home' ? 'page' : undefined}>
-				<a href="/home">Home</a>
-			</li>
 			{#if username !== ''}
+				<li aria-current={$page.url.pathname === '/home' ? 'page' : undefined}>
+					<a href="/home">Home</a>
+				</li>
 				<li aria-current={$page.url.pathname === '/stocks' ? 'page' : undefined}>
 					<a href="/stocks">Stocks</a>
 				</li>

+ 1 - 1
src/routes/+error.svelte

@@ -3,6 +3,6 @@
 	import { goto } from '$app/navigation';
 
 	onMount(() => {
-		goto('/home');
+		goto('/login');
 	});
 </script>

+ 1 - 1
src/routes/+page.server.js

@@ -1,5 +1,5 @@
 import { redirect } from '@sveltejs/kit';
 
 export function load() {
-	throw redirect(307, '/home');
+	throw redirect(307, '/login');
 }

+ 12 - 4
src/routes/home/+page.svelte

@@ -1,5 +1,17 @@
 <script>
 	import logo from '$lib/images/logo.png';
+	import { onMount } from 'svelte';
+	import { authentication } from '../store.js';
+	import { goto } from '$app/navigation';
+
+	onMount(() => {
+		return authentication.subscribe(async (auth) => {
+			if (!auth || !auth.token) {
+				await goto('/login');
+			}
+		});
+	});
+
 </script>
 
 <svelte:head>
@@ -22,10 +34,6 @@
 		flex: 0.6;
 	}
 
-	h1 {
-		width: 100%;
-	}
-
 	#bounce-image {
 		width: 300px;
 		animation: bounce 2s infinite;

+ 5 - 3
src/routes/insights/+page.svelte

@@ -11,9 +11,11 @@
 	let isLoading = true;
 
 	onMount(() => {
-		return authentication.subscribe(async ({ token }) => {
-			if (token) {
-				authToken = token;
+		return authentication.subscribe(async (auth) => {
+			if (!auth || !auth.token) {
+				await goto('/login');
+			} else {
+				authToken = auth.token;
 				await fetchPortfolio();
 			}
 		});

+ 7 - 6
src/routes/portfolio/+page.svelte

@@ -4,6 +4,7 @@
 	import { fade } from 'svelte/transition';
 	import AddStock from '../../components/AddStock.svelte';
 	import { getRequest } from '../../utils/api.js';
+	import { goto } from '$app/navigation';
 
 	let portfolioId = undefined;
 	let result = [];
@@ -18,14 +19,14 @@
 	let hasChanges = false;
 
 	onMount(() => {
-		const unsubscribe = authentication.subscribe((value) => {
-			if (value?.token) {
-				authToken = value.token;
-				fetchPortfolio();
+		return authentication.subscribe(async (auth) => {
+			if (!auth || !auth.token) {
+				await goto('/login');
+			} else {
+				authToken = auth.token;
+				await fetchPortfolio();
 			}
 		});
-
-		return () => unsubscribe();
 	});
 
 	async function fetchPortfolio() {

+ 13 - 6
src/routes/stocks/+page.svelte

@@ -3,17 +3,24 @@
 	import { fade } from 'svelte/transition';
 	import { goto } from '$app/navigation';
 	import { getRequest } from '../../utils/api.js';
+	import { authentication } from '../store.js';
 
 	let result = [];
 	let stockQuery = '';
 	let isLoading = true;
 
-	onMount(async () => {
-		const stockInfo = await get();
-		if (stockInfo) {
-			result = stockInfo;
-			isLoading = false;
-		}
+	onMount(() => {
+		return authentication.subscribe(async (auth) => {
+			if (!auth || !auth.token) {
+				await goto('/login');
+			} else {
+				const stockInfo = await get();
+				if (stockInfo) {
+					result = stockInfo;
+					isLoading = false;
+				}
+			}
+		});
 	});
 
 	async function get(stock) {