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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

@@ -114,7 +114,7 @@ const QuickConnectButton = (props: LoadingButtonProps) => {
style={{ flex: 1 }}
onClick={initQuickConnect.mutate}
>
Quick Connect
Use Quick Connect
</LoadingButton>
<Dialog
open={Boolean(quickConnectCode)}
+2 -2
View File
@@ -262,8 +262,8 @@ export const Route = createRootRouteWithContext<ApiContext>()({
<AppBar />
<AudioPlayer />
<Outlet />
{/* <ReactQueryDevtools /> */}
{/* <TanStackRouterDevtools /> */}
<ReactQueryDevtools />
<TanStackRouterDevtools />
</SnackbarProvider>
</ThemeProvider>
</Suspense>
+10 -14
View File
@@ -1,6 +1,6 @@
import { AppBarBackOnly } from "@/components/appBar/backOnly";
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
import { CircularProgress, Typography } from "@mui/material";
import { createFileRoute, redirect } from "@tanstack/react-router";
import { Outlet, createFileRoute, redirect } from "@tanstack/react-router";
import React from "react";
export const Route = createFileRoute("/_api/login")({
@@ -19,16 +19,12 @@ export const Route = createFileRoute("/_api/login")({
}
}
},
// component: () => {
// const data = Route.useRouteContext();
// console.log(data);
// return (
// <div style={{ height: "100vh" }} className="flex flex-column flex-center">
// <CircularProgress size={86} thickness={2} />
// <Typography mt={4} align="center">
// Fetching users...
// </Typography>
// </div>
// );
// },
component: () => {
return (
<>
<AppBarBackOnly />
<Outlet />
</>
);
},
});
+149 -152
View File
@@ -1,51 +1,68 @@
import LoadingButton from "@mui/lab/LoadingButton";
import { OutlinedInput, Paper, Typography } from "@mui/material";
import { OutlinedInput, Typography } from "@mui/material";
import Checkbox from "@mui/material/Checkbox";
import FormControl from "@mui/material/FormControl";
import FormControlLabel from "@mui/material/FormControlLabel";
import FormGroup from "@mui/material/FormGroup";
import IconButton from "@mui/material/IconButton";
import InputAdornment from "@mui/material/InputAdornment";
import InputLabel from "@mui/material/InputLabel";
import { useSnackbar } from "notistack";
import { AppBarBackOnly } from "@/components/appBar/backOnly.jsx";
import { AvatarImage } from "@/components/avatar/avatar.jsx";
import "./login.scss";
import { saveUser } from "@/utils/storage/user";
import { useApiInContext } from "@/utils/store/api";
import { setBackdrop } from "@/utils/store/backdrop.js";
import { createFileRoute, useNavigate } from "@tanstack/react-router";
import React, { useLayoutEffect, useState } from "react";
import { useBackdropStore } from "@/utils/store/backdrop.js";
import { useMutation } from "@tanstack/react-query";
import {
createFileRoute,
useNavigate,
useRouter,
} from "@tanstack/react-router";
import React, {
type ChangeEvent,
type FormEvent,
useEffect,
useState,
} from "react";
export const Route = createFileRoute("/_api/login/$userId/$userName")({
component: LoginUser,
});
type PasswordState = {
showpass: boolean;
password: string | undefined;
};
function LoginUser() {
const { userName, userId } = Route.useParams();
const router = useRouter();
const api = useApiInContext((s) => s.api);
const createApi = useApiInContext((s) => s.createApi);
const [password, setPassword] = useState({
const [password, setPassword] = useState<PasswordState>({
showpass: false,
password: undefined,
});
const [loading, setLoading] = useState(false);
const [rememberMe, setRememberMe] = useState(true);
const navigate = useNavigate({ from: "/login/$userId/$userName" });
const { enqueueSnackbar } = useSnackbar();
const handlePassword = (prop) => (event) => {
setPassword({
...password,
[prop]: event.target.value,
});
};
const handlePassword =
(prop: "password" | "showPassord") =>
(event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
setPassword({
...password,
[prop]: event.target.value,
});
};
const handleShowPassword = () => {
setPassword({
@@ -54,159 +71,139 @@ function LoginUser() {
});
};
const authUser = async () => {
try {
const auth = await api.authenticateUserByName(
userName,
password.password,
);
return auth;
} catch (error) {
if (error.response?.status === 500) {
enqueueSnackbar(
"Unable to connect to server. Please try again after some time.",
{ variant: "error" },
);
} else {
enqueueSnackbar("Incorrect Password.", { variant: "error" });
}
setLoading(false);
console.error(error);
}
};
const setBackdrop = useBackdropStore((state) => state.setBackdrop);
const handleLogin = async () => {
setLoading(true);
const user = await authUser();
sessionStorage.setItem("accessToken", user.data.AccessToken);
createApi(api.basePath, user.data.AccessToken);
if (rememberMe === true) {
await saveUser(userName, user.data.AccessToken);
useEffect(() => {
if (!api) {
return;
}
setLoading(false);
navigate({ to: "/home" });
};
useLayoutEffect(() => {
setBackdrop("", "");
}, []);
const handleCheckRememberMe = (event) => {
setRememberMe(event.target.checked);
const handleCheckRememberMe = (
_: ChangeEvent<HTMLInputElement>,
checked: boolean,
) => {
setRememberMe(checked);
};
const handleLoginFn = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
try {
if (!api) {
throw new Error("API not initialized");
}
const authenticate = await api.authenticateUserByName(
userName,
password.password,
);
if (!authenticate.data.AccessToken) {
throw new Error("No access token received");
}
createApi(api.basePath, authenticate.data?.AccessToken);
if (rememberMe) await saveUser(userName, authenticate.data.AccessToken);
router.invalidate().finally(() => {
navigate({ to: "/home", replace: true });
});
} catch (err) {
console.error(err);
enqueueSnackbar("Unable to authenticate! Please check your password", {
variant: "error",
});
}
return;
};
const handleLogin = useMutation({
mutationKey: ["login", "authenticate"],
mutationFn: handleLoginFn,
});
return (
<>
<AppBarBackOnly />
<div
style={{
height: "100vh",
display: "flex",
justifyContent: "center",
alignItems: "center",
flexDirection: "column",
width: "30vw",
margin: "auto",
}}
<div
style={{
height: "100vh",
display: "flex",
justifyContent: "center",
alignItems: "center",
flexDirection: "column",
width: "30vw",
margin: "auto",
}}
>
<AvatarImage userId={userId} />
<Typography
textAlign="center"
variant="h5"
sx={{ opacity: 0.8 }}
mb={2}
mt={2}
>
<div
Login as{" "}
<Typography
variant="h5"
style={{
marginBottom: "-4em",
boxShadow: "1em 0 hsl(256, 100%, 6%)",
display: "inline",
}}
fontWeight={700}
className="gradient-text"
>
<AvatarImage userId={userId} />
</div>
<Paper
sx={{
{userName}
</Typography>
</Typography>
<form onSubmit={(e) => handleLogin.mutate(e)}>
<FormControl
variant="outlined"
style={{
width: "100%",
padding: "1em",
paddingTop: "5em",
borderRadius: "10px",
}}
elevation={2}
>
<Typography
textAlign="center"
variant="h5"
sx={{ opacity: 0.8 }}
mb={2}
>
Login as{" "}
<Typography
variant="h5"
style={{
display: "inline",
}}
fontWeight={700}
className="gradient-text"
>
{userName}
</Typography>
</Typography>
<FormGroup
sx={{
width: "100%",
}}
>
<FormControl
variant="outlined"
style={{
width: "100%",
}}
>
<InputLabel htmlFor="user-password">Password:</InputLabel>
<OutlinedInput
id="user-password"
type={password.showpass ? "text" : "password"}
variant="outlined"
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}
/>
}
label="Remember me"
/>
</FormGroup>
<LoadingButton
variant="contained"
endIcon={
<span className="material-symbols-rounded">chevron_right</span>
<InputLabel htmlFor="user-password">Password:</InputLabel>
<OutlinedInput
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>
}
onClick={handleLogin}
loading={loading}
loadingPosition="end"
size="large"
fullWidth
>
Login
</LoadingButton>
</Paper>
</div>
</>
/>
</FormControl>
<FormControlLabel
control={
<Checkbox checked={rememberMe} onChange={handleCheckRememberMe} />
}
label="Remember me"
/>
<LoadingButton
variant="contained"
endIcon={
<span className="material-symbols-rounded">chevron_right</span>
}
type="submit"
loading={handleLogin.isPending}
loadingPosition="end"
size="large"
fullWidth
>
Login
</LoadingButton>
</form>
</div>
);
}
+21 -24
View File
@@ -74,20 +74,18 @@ function LoginPublicUsersList() {
}, []);
if (users.isSuccess) {
return (
<>
<AppBarBackOnly />
<div className="login-container">
<Typography variant="h4" align="center">
Users
</Typography>
<div className="login-container">
<Typography variant="h4" align="center">
Users
</Typography>
<div className="user-list-container roundedScrollbar">
{users.data.map((item) => {
return <UserCard user={item} key={item.Id} />;
})}
</div>
<div className="user-list-container roundedScrollbar">
{users.data.map((item) => {
return <UserCard user={item} key={item.Id} />;
})}
</div>
{/* <div className="buttons">
{/* <div className="buttons">
<Button
color="secondary"
variant="contained"
@@ -98,18 +96,17 @@ function LoginPublicUsersList() {
</Button>
<QuickConnectButton />
</div> */}
<Chip
style={{ marginLeft: "50%", transform: "translateX(-50%)" }}
label={
<Typography variant="body2" align="center">
Don't see your user? Try using{" "}
<Link to="/login/manual">Manual Login</Link> or{" "}
<Link to="/setup/server/list">Changing Server</Link>
</Typography>
}
/>
</div>
</>
<Chip
style={{ marginLeft: "50%", transform: "translateX(-50%)" }}
label={
<Typography variant="body2" align="center">
Don't see your user? Try using{" "}
<Link to="/login/manual">Manual Login</Link> or{" "}
<Link to="/setup/server/list">Changing Server</Link>
</Typography>
}
/>
</div>
);
}
if (users.isError) {
+2 -2
View File
@@ -66,7 +66,7 @@
}
&-form {
width: 28em;
width: 24em;
display: flex;
flex-direction: column;
justify-content: center;
@@ -81,7 +81,7 @@
background: rgb(255 255 255 / 0.05);
padding: 2em;
border-radius: 20px;
border: 1px solid rgb(255 255 255 / 0.1);
// border: 1px solid rgb(255 255 255 / 0.1);
&.padded-top {
margin-top: 25vh;
}
+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>
);
}