refactor: improve login page UI #338 #202

This commit is contained in:
Prayag Prajapati committed 2024-11-20 21:51:42 +05:30
1 parent 19343b4ea3
commit 45f67aac40
9 files changed
+314 -316

No files matched your search

+129 -121
View File
@@ -1,5 +1,10 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import React, { type FormEvent, useLayoutEffect, useState } from "react";
import { skipToken, useMutation, useQuery } from "@tanstack/react-query";
import React, {
type ChangeEvent,
type FormEvent,
useEffect,
useState,
} from "react";
import { saveUser } from "@/utils/storage/user.js";
@@ -15,19 +20,19 @@ import InputLabel from "@mui/material/InputLabel";
import { useSnackbar } from "notistack";
import { AppBarBackOnly } from "@/components/appBar/backOnly.jsx";
import { axiosClient, useApiInContext } from "@/utils/store/api.js";
import { useApiInContext } from "@/utils/store/api.js";
import { getBrandingApi } from "@jellyfin/sdk/lib/utils/api/branding-api";
import "./login.scss";
import QuickConnectButton from "@/components/buttons/quickConnectButton";
import { setBackdrop } from "@/utils/store/backdrop.js";
import { useBackdropStore } from "@/utils/store/backdrop.js";
import { blue } from "@mui/material/colors";
import {
createFileRoute,
useNavigate,
useRouter,
} from "@tanstack/react-router";
import axios from "axios";
export const Route = createFileRoute("/_api/login/manual")({
component: UserLoginManual,
@@ -43,15 +48,15 @@ function UserLoginManual() {
const server = useQuery({
queryKey: ["login", "manual", "serverInfo"],
queryFn: async () => {
return (await getBrandingApi(api).getBrandingOptions()).data;
},
queryFn: async () =>
api ? (await getBrandingApi(api).getBrandingOptions()).data : skipToken,
});
const ssoProviders = useQuery({
queryKey: ["login", "manual", "ssoProviders"],
queryFn: async () => {
return axiosClient.get(
if (!api?.accessToken) return;
return axios.get(
`${api.basePath}/sso/SAML/Get?api_key=${api.accessToken}`,
);
},
@@ -69,7 +74,7 @@ function UserLoginManual() {
password: "",
});
const handlePassword = (prop) => (event) => {
const handlePassword = (prop: "password" | "showpass") => (event) => {
setPassword({
...password,
[prop]: event.target.value,
@@ -86,14 +91,19 @@ function UserLoginManual() {
const handleLoginFn = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
try {
if (!api) {
throw new Error("Api is not available");
}
const authenticate = await api.authenticateUserByName(
username,
password.password,
);
if (!authenticate.data?.AccessToken) {
throw new Error("Access token not available");
}
createApi(api.basePath, authenticate.data?.AccessToken);
if (rememberMe) await saveUser(username, authenticate.data.AccessToken);
router.invalidate().finally(() => {
navigate({ to: searchParams.redirect || "/home", replace: true });
});
@@ -112,126 +122,124 @@ function UserLoginManual() {
mutationFn: handleLoginFn,
});
const handleCheckRememberMe = (event) => {
setRememberMe(event.target.checked);
const handleCheckRememberMe = (
_: ChangeEvent<HTMLInputElement>,
checked: boolean,
) => {
setRememberMe(checked);
};
useLayoutEffect(() => {
const setBackdrop = useBackdropStore((state) => state.setBackdrop);
useEffect(() => {
setBackdrop("", "");
}, []);
return (
<>
<AppBarBackOnly />
<div className="login scrollY">
<div
className={`login-form-container ${
server.data?.LoginDisclaimer && "padded-top"
}`}
>
<Typography variant="h3" color="textPrimary">
Login
</Typography>
<form className="login-form" onSubmit={(e) => handleLogin.mutate(e)}>
<TextField
variant="outlined"
label="Username"
onChange={(e) => setUsername(e.currentTarget.value)}
/>
<FormControl sx={{ width: "100%" }} variant="outlined">
<InputLabel htmlFor="user-password">Password:</InputLabel>
<OutlinedInput
fullWidth
id="user-password"
type={password.showpass ? "text" : "password"}
onChange={handlePassword("password")}
label="Password:"
endAdornment={
<InputAdornment position="end">
<IconButton
onClick={handleShowPassword}
aria-label="toggle password visibility"
>
{password.showpass ? (
<span className="material-symbols-rounded">
visibility
</span>
) : (
<span className="material-symbols-rounded">
visibility_off
</span>
)}
</IconButton>
</InputAdornment>
}
/>
</FormControl>
<FormControlLabel
control={
<Checkbox
checked={rememberMe}
onChange={handleCheckRememberMe}
/>
<div className="login scrollY">
<div
className={`login-form-container ${
server.data?.LoginDisclaimer && "padded-top"
}`}
>
<Typography variant="h4" color="textPrimary">
Login
</Typography>
<form className="login-form" onSubmit={(e) => handleLogin.mutate(e)}>
<TextField
variant="outlined"
label="Username"
onChange={(e) => setUsername(e.currentTarget.value)}
/>
<FormControl sx={{ width: "100%" }} variant="outlined">
<InputLabel htmlFor="user-password">Password:</InputLabel>
<OutlinedInput
fullWidth
id="user-password"
type={password.showpass ? "text" : "password"}
onChange={handlePassword("password")}
label="Password:"
endAdornment={
<InputAdornment position="end">
<IconButton
onClick={handleShowPassword}
aria-label="toggle password visibility"
>
{password.showpass ? (
<span className="material-symbols-rounded">
visibility
</span>
) : (
<span className="material-symbols-rounded">
visibility_off
</span>
)}
</IconButton>
</InputAdornment>
}
label="Remember me"
/>
<div
className="flex flex-row"
style={{ width: "100%", gap: "0.8em", flexWrap: "wrap" }}
>
<LoadingButton
variant="contained"
type="submit"
loading={handleLogin.isPending}
sx={{ width: "100%" }}
>
Login
</LoadingButton>
<QuickConnectButton />
<Button
color="secondary"
variant="contained"
className="userEventButton"
// size="large"
style={{ flex: 1 }}
onClick={() => navigate({ to: "/setup/server/list" })}
>
Change Server
</Button>
</div>
</form>
</div>
{server.isSuccess && server.data.LoginDisclaimer && (
<Paper
elevation={5}
style={{
marginTop: "1em",
padding: "1em",
borderRadius: "15px",
width: "32em",
}}
</FormControl>
<FormControlLabel
control={
<Checkbox checked={rememberMe} onChange={handleCheckRememberMe} />
}
label="Remember me"
/>
<div
className="flex flex-row"
style={{ width: "100%", gap: "0.8em", flexWrap: "wrap" }}
>
<div
className="flex flex-row flex-align-center"
style={{ gap: "0.5em", marginBottom: "0.5em" }}
<LoadingButton
variant="contained"
type="submit"
loading={handleLogin.isPending}
sx={{ width: "100%" }}
>
<span
className="material-symbols-rounded"
style={{
color: blue[700],
"--fill": 1,
}}
>
info
</span>
Notice
</div>
<Typography variant="subtitle2" style={{ opacity: 0.8 }}>
{server.data.LoginDisclaimer}
</Typography>
</Paper>
)}
Login
</LoadingButton>
<Button
color="secondary"
variant="contained"
className="userEventButton"
// size="large"
style={{ flex: 1 }}
onClick={() => navigate({ to: "/setup/server/list" })}
>
Change Server
</Button>
</div>
</form>
</div>
</>
{server.isSuccess && server.data.LoginDisclaimer && (
<Paper
elevation={5}
style={{
marginTop: "1em",
padding: "1em",
borderRadius: "15px",
width: "32em",
}}
>
<div
className="flex flex-row flex-align-center"
style={{ gap: "0.5em", marginBottom: "0.5em" }}
>
<span
className="material-symbols-rounded"
style={{
color: blue[700],
//@ts-ignore
"--fill": 1,
}}
>
info
</span>
Notice
</div>
<Typography variant="subtitle2" style={{ opacity: 0.8 }}>
{server.data.LoginDisclaimer}
</Typography>
</Paper>
)}
</div>
);
}