Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions frontend/src/components/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,6 @@ import {
} from "@ui/select";
import { useServer } from "./resources/server";

export const WithLoading = ({
children,
isLoading,
loading,
isError,
error,
}: {
children: ReactNode;
isLoading: boolean;
loading?: ReactNode;
isError: boolean;
error?: ReactNode;
}) => {
if (isLoading) return <>{loading ?? "loading"}</>;
if (isError) return <>{error ?? null}</>;
return <>{children}</>;
};

export const ActionButton = forwardRef<
HTMLButtonElement,
{
Expand Down
64 changes: 38 additions & 26 deletions frontend/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
useLoginOptions,
useUserInvalidate,
} from "@lib/hooks";
import { useState } from "react";
import { type FormEvent } from "react";
import { ThemeToggle } from "@ui/theme";
import { KOMODO_BASE_URL } from "@main";
import { KeyRound, X } from "lucide-react";
Expand All @@ -38,7 +38,6 @@ const login_with_oauth = (provider: OauthProvider) => {

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

Expand Down Expand Up @@ -98,7 +97,18 @@ export default function Login() {
},
});


const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const fd = new FormData(e.currentTarget);
const username = String(fd.get("username") ?? "");
const password = String(fd.get("password") ?? "");
const action = String(fd.get("action") ?? "login");
if (action === "signup") {
signup({ username, password });
} else {
login({ username, password });
}
};

const no_auth_configured =
options !== undefined &&
Expand All @@ -110,7 +120,6 @@ export default function Login() {
return (
<div className="flex flex-col min-h-screen">
<div className="container flex justify-end items-center h-16">
{/* <img src="/komodo-512x512.png" className="w-[32px]" /> */}
<ThemeToggle />
</div>
<div
Expand Down Expand Up @@ -166,63 +175,66 @@ export default function Login() {
</div>
</CardHeader>
{options?.local && (
<>
<form
onSubmit={handleSubmit}
autoComplete="on"
>
<CardContent className="flex flex-col justify-center w-full gap-4">
<div className="flex flex-col gap-2">
<Label htmlFor="username">Username</Label>
<Input
id="username"
value={creds.username}
onChange={({ target }) =>
set((c) => ({ ...c, username: target.value }))
}
name="username"
autoComplete="username"
autoCapitalize="none"
autoCorrect="off"
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="password">Password</Label>
<Input
id="password"
name="password"
type="password"
value={creds.password}
onChange={({ target }) =>
set((c) => ({ ...c, password: target.value }))
}
onKeyDown={(e) => e.key === "Enter" && login(creds)}
autoComplete="current-password"
/>
</div>
</CardContent>
<CardFooter className="flex gap-4 w-full justify-end">
{show_sign_up && (
<Button
variant="outline"
onClick={() => signup(creds)}
type="submit"
name="action"
value="signup"
disabled={signupPending}
>
Sign Up
</Button>
)}
<Button
variant="default"
onClick={() => login(creds)}
type="submit"
name="action"
value="login"
disabled={loginPending}
>
Log In
</Button>
</CardFooter>
</>
</form>
)}
{no_auth_configured && (
<CardContent className="w-full gap-2 text-muted-foreground text-sm">
No login methods have been configured. See the
<Button variant="link" className="text-sm py-0 px-1">
<a
href="https://github.com/moghtech/komodo/blob/main/config/core.config.toml"
target="_blank"
className="flex text-sm"
>
example config
</a>
</Button>
<a
href="https://github.com/moghtech/komodo/blob/main/config/core.config.toml"
target="_blank"
rel="noreferrer"
className="text-sm py-0 px-1 underline"
>
example config
</a>
for information on configuring auth.
</CardContent>
)}
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const useExchangeToken = () => {
};

export const Router = () => {
const { data: user, isLoading, error } = useUser();
const { data: user, error } = useUser();

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

if (isLoading && !user) return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this caused the login page to re-render on switching tabs which resulted in reset input fields

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think removing this might cause the login screen to "flash" every refresh, since there will be a render where user has not loaded yet but the user has localstorage token

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I just verified this behavior from this change


if (!user || error) return <Login />;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion should fix

Suggested change
if (!user || error) return <Login />;
if (error) return <Login />;
if (!user) return null;

if (!user.enabled) return <UserDisabled />;

Expand Down