+page.svelte 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <script>
  2. import { onMount } from 'svelte';
  3. import { authentication } from '../store.js';
  4. import { goto } from '$app/navigation';
  5. import { fade } from 'svelte/transition';
  6. let isAuthenticated = false;
  7. let isLoading = true;
  8. let showPassword = false;
  9. let isDisabled = false;
  10. $: authentication.subscribe((value) => {
  11. isAuthenticated = !!value;
  12. });
  13. onMount(() => {
  14. try {
  15. const storedAuth = localStorage.getItem('authentication');
  16. if (storedAuth) {
  17. authentication.set(JSON.parse(storedAuth));
  18. goto("/portfolio");
  19. }
  20. } catch (error) {
  21. console.error('Error parsing stored auth:', error);
  22. } finally {
  23. isLoading = false;
  24. }
  25. });
  26. async function submit(event) {
  27. event.preventDefault();
  28. isDisabled = true;
  29. const formData = new FormData(event.target);
  30. const username = formData.get('username');
  31. const password = formData.get('password');
  32. if (!username || !password) {
  33. alert('Please enter both username and password.');
  34. return;
  35. }
  36. const data = {
  37. username: username,
  38. password: password
  39. };
  40. try {
  41. const response = await fetch(import.meta.env.VITE_AUTH_HOST + '/api/authenticate', {
  42. method: 'POST',
  43. headers: {
  44. 'Content-Type': 'application/json'
  45. },
  46. body: JSON.stringify(data)
  47. });
  48. if (response.ok) {
  49. const result = await response.json();
  50. authentication.set(result);
  51. localStorage.setItem('authentication', JSON.stringify(result));
  52. await goto('/portfolio');
  53. } else {
  54. const error = await response.json();
  55. console.error('Login failed:', error);
  56. alert('Login failed: ' + error.message);
  57. }
  58. } catch (err) {
  59. console.error('Error during login:', err);
  60. alert('An error occurred. Please try again.');
  61. } finally {
  62. isDisabled = false;
  63. }
  64. }
  65. function navigateToRegister() {
  66. window.location.href = '/register';
  67. }
  68. function togglePasswordVisibility() {
  69. showPassword = !showPassword;
  70. }
  71. </script>
  72. <svelte:head>
  73. <title>Login</title>
  74. <meta name="description" content="Login page" />
  75. </svelte:head>
  76. <div in:fade class="flex justify-center items-center min-h-[70vh] px-4">
  77. {#if isLoading}
  78. <p class="text-gray-500 dark:text-gray-400">Loading...</p>
  79. {:else if !isAuthenticated}
  80. <div class="w-full max-w-sm bg-white dark:bg-gray-900 shadow-md rounded-xl p-6 space-y-6">
  81. <form on:submit={submit} class="space-y-4">
  82. <!-- Username -->
  83. <input
  84. type="text"
  85. name="username"
  86. placeholder="Username"
  87. class="w-full px-4 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500"
  88. />
  89. <!-- Password -->
  90. <div class="relative">
  91. <input
  92. type={showPassword ? 'text' : 'password'}
  93. name="password"
  94. placeholder="Password"
  95. class="w-full px-4 py-2 pr-10 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500"
  96. />
  97. <span
  98. class="absolute inset-y-0 right-3 flex items-center cursor-pointer text-gray-500 dark:text-gray-400 text-lg"
  99. on:click={togglePasswordVisibility}
  100. >
  101. {#if showPassword}
  102. 👁️‍🗨️
  103. {:else}
  104. 👁️‍🗨️
  105. {/if}
  106. </span>
  107. </div>
  108. <!-- Buttons -->
  109. <button
  110. type="submit"
  111. class="w-full bg-blue-500 hover:bg-blue-600 text-white py-2 rounded-lg transition flex justify-center items-center gap-2"
  112. disabled={isDisabled}
  113. >
  114. {#if isDisabled}
  115. <svg class="animate-spin h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
  116. <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
  117. <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v8H4z"></path>
  118. </svg>
  119. <span>Logging in...</span>
  120. {:else}
  121. <span>Login</span>
  122. {/if}
  123. </button>
  124. <button
  125. type="button"
  126. class="w-full bg-gray-300 hover:bg-gray-400 text-gray-800 py-2 rounded-lg transition"
  127. on:click={navigateToRegister}
  128. disabled={isDisabled}
  129. >
  130. Register
  131. </button>
  132. </form>
  133. </div>
  134. {/if}
  135. </div>