58 lines · 1.8 KB
| 1 | --- |
| 2 | import Base from '../layouts/Base.astro'; |
| 3 | import { apiPost } from '../lib/api'; |
| 4 | |
| 5 | let error = ''; |
| 6 | |
| 7 | if (Astro.request.method === 'POST') { |
| 8 | try { |
| 9 | const formData = await Astro.request.formData(); |
| 10 | const username = formData.get('username') as string; |
| 11 | const password = formData.get('password') as string; |
| 12 | |
| 13 | const { data, headers } = await apiPost('/api/login', { username, password }); |
| 14 | |
| 15 | // Forward the Set-Cookie from the API |
| 16 | const setCookie = headers.get('set-cookie'); |
| 17 | if (setCookie) { |
| 18 | Astro.cookies.set('session', setCookie.match(/session=([^;]+)/)?.[1] || '', { |
| 19 | path: '/', |
| 20 | httpOnly: true, |
| 21 | secure: true, |
| 22 | sameSite: 'strict', |
| 23 | maxAge: 7 * 24 * 60 * 60, |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | return Astro.redirect('/'); |
| 28 | } catch (e: any) { |
| 29 | error = e.message; |
| 30 | } |
| 31 | } |
| 32 | --- |
| 33 | |
| 34 | <Base title="Sign in"> |
| 35 | <div class="container" style="max-width: 400px; padding-top: 48px;"> |
| 36 | <div class="card"> |
| 37 | <h1 style="font-size: 1.25rem; margin-bottom: 20px; text-align: center;">Sign in to gitfastr</h1> |
| 38 | |
| 39 | {error && <div class="flash-error">{error}</div>} |
| 40 | |
| 41 | <form method="POST"> |
| 42 | <div class="form-group"> |
| 43 | <label for="username">Username</label> |
| 44 | <input type="text" id="username" name="username" required autocomplete="username" /> |
| 45 | </div> |
| 46 | <div class="form-group"> |
| 47 | <label for="password">Password</label> |
| 48 | <input type="password" id="password" name="password" required autocomplete="current-password" /> |
| 49 | </div> |
| 50 | <button type="submit" class="btn btn-primary" style="width: 100%;">Sign in</button> |
| 51 | </form> |
| 52 | </div> |
| 53 | <p style="text-align: center; margin-top: 16px; font-size: 0.875rem; color: var(--text-muted);"> |
| 54 | Don't have an account? <a href="/register">Sign up</a> |
| 55 | </p> |
| 56 | </div> |
| 57 | </Base> |
| 58 |