This commit is contained in:
Vasilisk9812 2025-04-06 01:14:40 +09:00
parent 19a8cdc1d6
commit 79848ef36f
4 changed files with 29 additions and 114 deletions

View file

@ -1,6 +1,5 @@
<script>
import { goto } from '$app/navigation';
import Navbar from '../Navbar.svelte';
let username = '';
let password = '';
@ -16,14 +15,38 @@
isLoading = true;
error = '';
// Simulate login request
// login request
try {
await new Promise(resolve => setTimeout(resolve, 1000));
// Replace with actual login logic
// await yourAuthFunction(username, password);
const response = await fetch('http://127.0.0.1:8000/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: username,
password: password
}),
credentials: 'include' // For session/cookie based auth
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'Login failed');
}
const data = await response.json();
// Store token if using JWT
if (data.access) {
localStorage.setItem('accessToken', data.access);
if (data.refresh) {
localStorage.setItem('refreshToken', data.refresh);
}
}
goto('/'); // Redirect after successful login
} catch (err) {
error = 'Invalid credentials';
error = err.message || 'Invalid credentials';
} finally {
isLoading = false;
}