|
| 1 | +<script lang="ts"> |
| 2 | + import RedirectIfLoggedIn from '$lib/auth/RedirectIfLoggedIn.svelte'; |
| 3 | + import { AUTH_SERVICE } from '$lib/auth/authService.svelte'; |
| 4 | + import GitHubButton from '$lib/components/login/GitHubButton.svelte'; |
| 5 | + import GoogleButton from '$lib/components/login/GoogleButton.svelte'; |
| 6 | + import { inject } from '@gitbutler/shared/context'; |
| 7 | + import { LOGIN_SERVICE } from '@gitbutler/shared/login/loginService'; |
| 8 | + import { WEB_ROUTES_SERVICE } from '@gitbutler/shared/routing/webRoutes.svelte'; |
| 9 | + import { Button, SectionCard } from '@gitbutler/ui'; |
| 10 | + import { env } from '$env/dynamic/public'; |
| 11 | +
|
| 12 | + let email = $state<string>(); |
| 13 | + let password = $state<string>(); |
| 14 | +
|
| 15 | + let error = $state<string>(); |
| 16 | + let errorCode = $state<string>(); |
| 17 | +
|
| 18 | + const loginService = inject(LOGIN_SERVICE); |
| 19 | + const routesService = inject(WEB_ROUTES_SERVICE); |
| 20 | + const authService = inject(AUTH_SERVICE); |
| 21 | +
|
| 22 | + async function handleSubmit(event: Event) { |
| 23 | + event.preventDefault(); |
| 24 | + if (!email || !password) { |
| 25 | + console.error('Email and password are required'); |
| 26 | + return; |
| 27 | + } |
| 28 | +
|
| 29 | + const response = await loginService.loginWithEmail(email, password); |
| 30 | +
|
| 31 | + if (response.type === 'error') { |
| 32 | + error = response.errorMessage; |
| 33 | + errorCode = response.errorCode; |
| 34 | + console.error('Login failed:', response.raw ?? response.errorMessage); |
| 35 | + } else { |
| 36 | + const token = response.data; |
| 37 | + authService.setToken(token); |
| 38 | + window.location.href = `${env.PUBLIC_APP_HOST}successful_login?access_token=${token}`; |
| 39 | + } |
| 40 | + } |
| 41 | +
|
| 42 | + async function resendConfirmationEmail() { |
| 43 | + if (!email) { |
| 44 | + error = 'Please enter your email to resend the confirmation email.'; |
| 45 | + return; |
| 46 | + } |
| 47 | + const response = await loginService.resendConfirmationEmail(email); |
| 48 | + if (response.type === 'error') { |
| 49 | + error = response.errorMessage; |
| 50 | + errorCode = response.errorCode; |
| 51 | + console.error('Failed to resend confirmation email:', response.raw ?? response.errorMessage); |
| 52 | + } else { |
| 53 | + error = 'Confirmation email resent. Please check your inbox.'; |
| 54 | + } |
| 55 | + } |
| 56 | +</script> |
| 57 | + |
| 58 | +<svelte:head> |
| 59 | + <title>GitButler | Login</title> |
| 60 | +</svelte:head> |
| 61 | + |
| 62 | +<RedirectIfLoggedIn /> |
| 63 | + |
| 64 | +<div class="main-links"> |
| 65 | + <a href={routesService.homePath()} class="logo" aria-label="main nav" title="Home"> |
| 66 | + <svg width="23" height="22" viewBox="0 0 23 22" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 67 | + <path d="M0 22V0L11.4819 9.63333L23 0V22L11.4819 12.4L0 22Z" fill="var(--clr-text-1)" /> |
| 68 | + </svg> |
| 69 | + </a> |
| 70 | +</div> |
| 71 | + |
| 72 | +<form onsubmit={handleSubmit} class="login-form"> |
| 73 | + <div class="login-form__content"> |
| 74 | + <SectionCard> |
| 75 | + <div class="field"> |
| 76 | + <label for="email">Email</label> |
| 77 | + <input id="email" type="email" bind:value={email} required /> |
| 78 | + </div> |
| 79 | + <div class="field"> |
| 80 | + <label for="password">Password</label> |
| 81 | + <input id="password" type="password" bind:value={password} required /> |
| 82 | + </div> |
| 83 | + </SectionCard> |
| 84 | + |
| 85 | + <div class="reset-password-link"> |
| 86 | + Or did you forget your password? |
| 87 | + <br /> |
| 88 | + Wow. That sounds irresponsible. |
| 89 | + <a href={routesService.resetPasswordPath()}>I mean... it's fine. Reset it</a> |
| 90 | + </div> |
| 91 | + |
| 92 | + <Button type="submit">Log in</Button> |
| 93 | + |
| 94 | + <div class="signup-link"> |
| 95 | + Don't have an account? |
| 96 | + <a href={routesService.signupPath()}>Sign up</a> |
| 97 | + </div> |
| 98 | + |
| 99 | + {#if error} |
| 100 | + <div class="error-message"> |
| 101 | + <span> |
| 102 | + {error} |
| 103 | + </span> |
| 104 | + </div> |
| 105 | + {#if errorCode === 'email_not_verified'} |
| 106 | + <span> |
| 107 | + Please verify your email address before logging in. Check your inbox for a verification |
| 108 | + email or |
| 109 | + </span> |
| 110 | + <Button |
| 111 | + type="button" |
| 112 | + onclick={resendConfirmationEmail} |
| 113 | + disabled={!email} |
| 114 | + tooltip={!email |
| 115 | + ? 'Please enter your email in the field above to resend the confirmation email.' |
| 116 | + : 'Resend confirmation email'} |
| 117 | + > |
| 118 | + resend the confirmation email</Button |
| 119 | + > |
| 120 | + {/if} |
| 121 | + {/if} |
| 122 | + |
| 123 | + <SectionCard> |
| 124 | + <GitHubButton /> |
| 125 | + <GoogleButton /> |
| 126 | + </SectionCard> |
| 127 | + </div> |
| 128 | +</form> |
| 129 | + |
| 130 | +<style lang="postcss"> |
| 131 | + .main-links { |
| 132 | + display: flex; |
| 133 | + align-items: center; |
| 134 | + margin-bottom: 16px; |
| 135 | + overflow: hidden; |
| 136 | + gap: 16px; |
| 137 | + } |
| 138 | +
|
| 139 | + .logo { |
| 140 | + display: flex; |
| 141 | + } |
| 142 | +
|
| 143 | + .login-form__content { |
| 144 | + display: flex; |
| 145 | + flex-direction: column; |
| 146 | + max-width: 400px; |
| 147 | + margin: auto; |
| 148 | + gap: 16px; |
| 149 | + } |
| 150 | +
|
| 151 | + .field { |
| 152 | + display: flex; |
| 153 | + flex-direction: column; |
| 154 | + gap: 4px; |
| 155 | +
|
| 156 | + label { |
| 157 | + color: var(--clr-scale-ntrl-30); |
| 158 | + font-size: 14px; |
| 159 | + } |
| 160 | +
|
| 161 | + input { |
| 162 | + padding: 8px 12px; |
| 163 | + border: 1px solid var(--clr-border-2); |
| 164 | + border-radius: var(--radius-m); |
| 165 | + background-color: var(--clr-bg-1); |
| 166 | + color: var(--clr-scale-ntrl-0); |
| 167 | + font-size: 14px; |
| 168 | +
|
| 169 | + &:read-only { |
| 170 | + cursor: not-allowed; |
| 171 | + opacity: 0.7; |
| 172 | + } |
| 173 | +
|
| 174 | + &:not(:read-only) { |
| 175 | + &:focus { |
| 176 | + border-color: var(--clr-scale-pop-70); |
| 177 | + outline: none; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +
|
| 183 | + .error-message { |
| 184 | + display: flex; |
| 185 | + flex-direction: column; |
| 186 | + padding: 8px; |
| 187 | + gap: 8px; |
| 188 | + border: 1px solid var(--clr-scale-err-60); |
| 189 | + border-radius: var(--radius-m); |
| 190 | +
|
| 191 | + background-color: var(--clr-theme-err-bg-muted); |
| 192 | + color: var(--clr-scale-err-10); |
| 193 | + font-size: 14px; |
| 194 | + } |
| 195 | +
|
| 196 | + .signup-link, |
| 197 | + .reset-password-link { |
| 198 | + display: flex; |
| 199 | + flex-direction: column; |
| 200 | + align-items: center; |
| 201 | + justify-content: center; |
| 202 | +
|
| 203 | + gap: 4px; |
| 204 | + font-size: 14px; |
| 205 | +
|
| 206 | + a { |
| 207 | + text-decoration: underline; |
| 208 | + } |
| 209 | + } |
| 210 | +</style> |
0 commit comments