✨ add login page
Browse files at this commita35272ff8e36c94a2f0784c8455f583296149809Parent: d631d8e- feat
- ✨ add login page
1 changed file +86 -0
- src/pages/Login.tsx +86 -0
src/pages/Login.tsxadded+86 -0
| @@ -0,0 +1,86 @@ | |||
| 1 | +// Login / register, one form with a mode switch. On success the auth resource | ||
| 2 | +// is already mutated, so navigation lands on a fully hydrated app. | ||
| 3 | + | ||
| 4 | +import { Navigate, useNavigate } from "@solidjs/router"; | ||
| 5 | +import { Show, createSignal } from "solid-js"; | ||
| 6 | +import { ErrorLine } from "../components/Feedback"; | ||
| 7 | +import { IconPlane } from "../components/Icons"; | ||
| 8 | +import { createAction } from "../lib/action"; | ||
| 9 | +import { useAuth } from "../lib/auth"; | ||
| 10 | +import { LIMITS } from "../lib/types"; | ||
| 11 | + | ||
| 12 | +export default function Login() { | ||
| 13 | + const { user, login, register } = useAuth(); | ||
| 14 | + const navigate = useNavigate(); | ||
| 15 | + const [mode, setMode] = createSignal<"login" | "register">("login"); | ||
| 16 | + | ||
| 17 | + const submit = createAction(async (form: HTMLFormElement) => { | ||
| 18 | + const data = new FormData(form); | ||
| 19 | + const credentials = { | ||
| 20 | + username: String(data.get("username")), | ||
| 21 | + password: String(data.get("password")), | ||
| 22 | + }; | ||
| 23 | + await (mode() === "login" ? login(credentials) : register(credentials)); | ||
| 24 | + navigate("/", { replace: true }); | ||
| 25 | + }); | ||
| 26 | + | ||
| 27 | + const switchMode = () => { | ||
| 28 | + setMode((m) => (m === "login" ? "register" : "login")); | ||
| 29 | + submit.clearError(); | ||
| 30 | + }; | ||
| 31 | + | ||
| 32 | + return ( | ||
| 33 | + <Show when={!user()} fallback={<Navigate href="/" />}> | ||
| 34 | + <main class="auth-page"> | ||
| 35 | + <form | ||
| 36 | + class="card auth-card" | ||
| 37 | + onSubmit={(e) => { | ||
| 38 | + e.preventDefault(); | ||
| 39 | + void submit.run(e.currentTarget); | ||
| 40 | + }} | ||
| 41 | + > | ||
| 42 | + <header class="auth-head"> | ||
| 43 | + <span class="brand-mark"> | ||
| 44 | + <IconPlane /> | ||
| 45 | + </span> | ||
| 46 | + <h1>Hezarfen</h1> | ||
| 47 | + <p> | ||
| 48 | + {mode() === "login" | ||
| 49 | + ? "Welcome back — log in to continue." | ||
| 50 | + : "Create an account to get started."} | ||
| 51 | + </p> | ||
| 52 | + </header> | ||
| 53 | + <label> | ||
| 54 | + Username | ||
| 55 | + <input | ||
| 56 | + name="username" | ||
| 57 | + required | ||
| 58 | + minLength={LIMITS.username.min} | ||
| 59 | + maxLength={LIMITS.username.max} | ||
| 60 | + autocomplete="username" | ||
| 61 | + autofocus | ||
| 62 | + /> | ||
| 63 | + </label> | ||
| 64 | + <label> | ||
| 65 | + Password | ||
| 66 | + <input | ||
| 67 | + name="password" | ||
| 68 | + type="password" | ||
| 69 | + required | ||
| 70 | + minLength={LIMITS.password.min} | ||
| 71 | + maxLength={LIMITS.password.max} | ||
| 72 | + autocomplete={mode() === "login" ? "current-password" : "new-password"} | ||
| 73 | + /> | ||
| 74 | + </label> | ||
| 75 | + <ErrorLine error={submit.error()} /> | ||
| 76 | + <button type="submit" disabled={submit.pending()}> | ||
| 77 | + {mode() === "login" ? "Log in" : "Create account"} | ||
| 78 | + </button> | ||
| 79 | + <button type="button" class="ghost" onClick={switchMode}> | ||
| 80 | + {mode() === "login" ? "New here? Register" : "Have an account? Log in"} | ||
| 81 | + </button> | ||
| 82 | + </form> | ||
| 83 | + </main> | ||
| 84 | + </Show> | ||
| 85 | + ); | ||
| 86 | +} | ||