Skip to content

Commit 67a8ac7

Browse files
authored
Improve login form (#788)
* Use proper form for login, add autocomplete and names to input fields * Do not return null if loading * Remove unused function * Cleanup and streamline
1 parent 5451f27 commit 67a8ac7

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

frontend/src/components/util.tsx

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,6 @@ import {
9393
} from "@ui/select";
9494
import { useServer } from "./resources/server";
9595

96-
export const WithLoading = ({
97-
children,
98-
isLoading,
99-
loading,
100-
isError,
101-
error,
102-
}: {
103-
children: ReactNode;
104-
isLoading: boolean;
105-
loading?: ReactNode;
106-
isError: boolean;
107-
error?: ReactNode;
108-
}) => {
109-
if (isLoading) return <>{loading ?? "loading"}</>;
110-
if (isError) return <>{error ?? null}</>;
111-
return <>{children}</>;
112-
};
113-
11496
export const ActionButton = forwardRef<
11597
HTMLButtonElement,
11698
{

frontend/src/pages/login.tsx

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
useLoginOptions,
1616
useUserInvalidate,
1717
} from "@lib/hooks";
18-
import { useState } from "react";
18+
import { type FormEvent } from "react";
1919
import { ThemeToggle } from "@ui/theme";
2020
import { KOMODO_BASE_URL } from "@main";
2121
import { KeyRound, X } from "lucide-react";
@@ -38,7 +38,6 @@ const login_with_oauth = (provider: OauthProvider) => {
3838

3939
export default function Login() {
4040
const options = useLoginOptions().data;
41-
const [creds, set] = useState({ username: "", password: "" });
4241
const userInvalidate = useUserInvalidate();
4342
const { toast } = useToast();
4443

@@ -98,7 +97,18 @@ export default function Login() {
9897
},
9998
});
10099

101-
100+
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
101+
e.preventDefault();
102+
const fd = new FormData(e.currentTarget);
103+
const username = String(fd.get("username") ?? "");
104+
const password = String(fd.get("password") ?? "");
105+
const action = String(fd.get("action") ?? "login");
106+
if (action === "signup") {
107+
signup({ username, password });
108+
} else {
109+
login({ username, password });
110+
}
111+
};
102112

103113
const no_auth_configured =
104114
options !== undefined &&
@@ -110,7 +120,6 @@ export default function Login() {
110120
return (
111121
<div className="flex flex-col min-h-screen">
112122
<div className="container flex justify-end items-center h-16">
113-
{/* <img src="/komodo-512x512.png" className="w-[32px]" /> */}
114123
<ThemeToggle />
115124
</div>
116125
<div
@@ -166,63 +175,66 @@ export default function Login() {
166175
</div>
167176
</CardHeader>
168177
{options?.local && (
169-
<>
178+
<form
179+
onSubmit={handleSubmit}
180+
autoComplete="on"
181+
>
170182
<CardContent className="flex flex-col justify-center w-full gap-4">
171183
<div className="flex flex-col gap-2">
172184
<Label htmlFor="username">Username</Label>
173185
<Input
174186
id="username"
175-
value={creds.username}
176-
onChange={({ target }) =>
177-
set((c) => ({ ...c, username: target.value }))
178-
}
187+
name="username"
188+
autoComplete="username"
189+
autoCapitalize="none"
190+
autoCorrect="off"
179191
/>
180192
</div>
181193
<div className="flex flex-col gap-2">
182194
<Label htmlFor="password">Password</Label>
183195
<Input
184196
id="password"
197+
name="password"
185198
type="password"
186-
value={creds.password}
187-
onChange={({ target }) =>
188-
set((c) => ({ ...c, password: target.value }))
189-
}
190-
onKeyDown={(e) => e.key === "Enter" && login(creds)}
199+
autoComplete="current-password"
191200
/>
192201
</div>
193202
</CardContent>
194203
<CardFooter className="flex gap-4 w-full justify-end">
195204
{show_sign_up && (
196205
<Button
197206
variant="outline"
198-
onClick={() => signup(creds)}
207+
type="submit"
208+
name="action"
209+
value="signup"
199210
disabled={signupPending}
200211
>
201212
Sign Up
202213
</Button>
203214
)}
204215
<Button
205216
variant="default"
206-
onClick={() => login(creds)}
217+
type="submit"
218+
name="action"
219+
value="login"
207220
disabled={loginPending}
208221
>
209222
Log In
210223
</Button>
211224
</CardFooter>
212-
</>
225+
</form>
213226
)}
214227
{no_auth_configured && (
215228
<CardContent className="w-full gap-2 text-muted-foreground text-sm">
216229
No login methods have been configured. See the
217-
<Button variant="link" className="text-sm py-0 px-1">
218-
<a
219-
href="https://github.com/moghtech/komodo/blob/main/config/core.config.toml"
220-
target="_blank"
221-
className="flex text-sm"
222-
>
223-
example config
224-
</a>
225-
</Button>
230+
<a
231+
href="https://github.com/moghtech/komodo/blob/main/config/core.config.toml"
232+
target="_blank"
233+
rel="noreferrer"
234+
className="text-sm py-0 px-1 underline"
235+
>
236+
example config
237+
</a>
226238
for information on configuring auth.
227239
</CardContent>
228240
)}

frontend/src/router.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const useExchangeToken = () => {
6363
};
6464

6565
export const Router = () => {
66-
const { data: user, isLoading, error } = useUser();
66+
const { data: user, error } = useUser();
6767

6868
// Handle exchange token loop to avoid showing login flash
6969
const exchangeTokenPending = useExchangeToken();
@@ -75,8 +75,6 @@ export const Router = () => {
7575
);
7676
}
7777

78-
if (isLoading && !user) return null;
79-
8078
if (!user || error) return <Login />;
8179
if (!user.enabled) return <UserDisabled />;
8280

0 commit comments

Comments
 (0)