init - basic toke-based auth
This commit is contained in:
parent
da418169c6
commit
9259cdd84c
0
frontend/src/app.css
Normal file
0
frontend/src/app.css
Normal file
8
frontend/src/routes/(auth)/+layout.ts
Normal file
8
frontend/src/routes/(auth)/+layout.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { loggedIn } from '$lib/api';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export async function load() {
|
||||
if (!loggedIn()) {
|
||||
redirect(307, '/login');
|
||||
}
|
||||
}
|
7
frontend/src/routes/+layout.svelte
Normal file
7
frontend/src/routes/+layout.svelte
Normal file
@ -0,0 +1,7 @@
|
||||
<script lang="ts">
|
||||
import '../app.css';
|
||||
|
||||
let { children } = $props();
|
||||
</script>
|
||||
|
||||
{@render children()}
|
1
frontend/src/routes/+layout.ts
Normal file
1
frontend/src/routes/+layout.ts
Normal file
@ -0,0 +1 @@
|
||||
export const ssr = false;
|
7
frontend/src/routes/+page.ts
Normal file
7
frontend/src/routes/+page.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { loggedIn } from '$lib/api';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export async function load() {
|
||||
const page = loggedIn() ? '/main' : '/login';
|
||||
redirect(307, page);
|
||||
}
|
51
frontend/src/routes/login/+page.svelte
Normal file
51
frontend/src/routes/login/+page.svelte
Normal file
@ -0,0 +1,51 @@
|
||||
<script lang="ts">
|
||||
import { login } from '$lib/api';
|
||||
|
||||
let username = '';
|
||||
let password = '';
|
||||
let errorMessage = '';
|
||||
let isLoading = false;
|
||||
|
||||
const handleLogin = async () => {
|
||||
isLoading = true;
|
||||
errorMessage = ''; // Reset any previous error message
|
||||
|
||||
try {
|
||||
await login(username, password);
|
||||
// If successful, you can redirect the user to another page or show a success message
|
||||
window.location.href = '/main'; // Example redirection after login
|
||||
} catch (error: any) {
|
||||
errorMessage = error.message || 'Login failed. Please try again.';
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<div class="login-container">
|
||||
<h2>Login</h2>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username" bind:value={username} placeholder="Enter your username" />
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" bind:value={password} placeholder="Enter your password" />
|
||||
</div>
|
||||
|
||||
{#if errorMessage}
|
||||
<div class="error-message">{errorMessage}</div>
|
||||
{/if}
|
||||
|
||||
<button class="submit-button" on:click={handleLogin} disabled={isLoading}>
|
||||
{#if isLoading}
|
||||
<div class="loading">
|
||||
<span>Loading...</span>
|
||||
</div>
|
||||
{:else}
|
||||
Login
|
||||
{/if}
|
||||
</button>
|
||||
</div>
|
8
frontend/src/routes/login/+page.ts
Normal file
8
frontend/src/routes/login/+page.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { loggedIn } from '$lib/api';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export async function load() {
|
||||
if (loggedIn()) {
|
||||
redirect(307, '/');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user