Allow user password and username to be saved

This commit is contained in:
Prayag Prajapati committed 2023-02-23 10:44:53 +05:30
1 parent c73774a3b2
commit 059f391e52
3 files changed
+93 -18

No files matched your search

+69 -12
View File
@@ -1,10 +1,11 @@
/** @format */ /** @format */
import { useState, useEffect } from "react"; import { useState, useEffect, useMemo } from "react";
import { Cookies, useCookies } from "react-cookie"; import { Cookies, useCookies } from "react-cookie";
import { useNavigate, useParams, Link } from "react-router-dom"; import { useNavigate, useParams, Link } from "react-router-dom";
import { EventEmitter as event } from "../../eventEmitter.js"; import { EventEmitter as event } from "../../eventEmitter.js";
import { motion } from "framer-motion";
import { saveUser } from "../../utils/store/user.js";
// import Icon from "mdi-material-ui"; // import Icon from "mdi-material-ui";
import { MdiEyeOffOutline } from "../../components/icons/mdiEyeOffOutline"; import { MdiEyeOffOutline } from "../../components/icons/mdiEyeOffOutline";
@@ -22,6 +23,9 @@ import IconButton from "@mui/material/IconButton";
import LoadingButton from "@mui/lab/LoadingButton"; import LoadingButton from "@mui/lab/LoadingButton";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import { useSnackbar } from "notistack"; import { useSnackbar } from "notistack";
import FormGroup from "@mui/material/FormGroup";
import FormControlLabel from "@mui/material/FormControlLabel";
import Checkbox from "@mui/material/Checkbox";
import { AvatarCard, AvatarImage } from "../../components/avatar/avatar.jsx"; import { AvatarCard, AvatarImage } from "../../components/avatar/avatar.jsx";
import { AppBarBackOnly } from "../../components/appBar/backOnly.jsx"; import { AppBarBackOnly } from "../../components/appBar/backOnly.jsx";
@@ -34,11 +38,15 @@ import "./login.module.scss";
export const LoginWithImage = () => { export const LoginWithImage = () => {
const { userName, userId } = useParams(); const { userName, userId } = useParams();
const [password, setPassword] = useState({ const [password, setPassword] = useState({
showpass: false, showpass: false,
}); });
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [rememberMe, setRememberMe] = useState(true);
const navigate = useNavigate(); const navigate = useNavigate();
const { enqueueSnackbar } = useSnackbar(); const { enqueueSnackbar } = useSnackbar();
const handlePassword = (prop) => (event) => { const handlePassword = (prop) => (event) => {
@@ -73,12 +81,19 @@ export const LoginWithImage = () => {
setLoading(true); setLoading(true);
const user = await authUser(); const user = await authUser();
sessionStorage.setItem("accessToken", user.data.AccessToken); sessionStorage.setItem("accessToken", user.data.AccessToken);
if (rememberMe == true) {
saveUser(userName, password.password);
}
event.emit("set-api-accessToken", window.api.basePath); event.emit("set-api-accessToken", window.api.basePath);
// setAccessToken(user.data.AccessToken) // setAccessToken(user.data.AccessToken)
setLoading(false); setLoading(false);
navigate(`/home`); navigate(`/home`);
}; };
const handleCheckRememberMe = (event) => {
setRememberMe(event.target.checked);
};
return ( return (
<> <>
<AppBarBackOnly /> <AppBarBackOnly />
@@ -90,7 +105,13 @@ export const LoginWithImage = () => {
alignItems="center" alignItems="center"
spacing={2} spacing={2}
> >
<Grid item marginBottom={2}> <Grid
container
direction="column"
justifyContent="center"
alignItems="center"
marginBottom={2}
>
<Typography <Typography
textAlign="center" textAlign="center"
variant="h3" variant="h3"
@@ -116,7 +137,7 @@ export const LoginWithImage = () => {
</Typography> </Typography>
</Grid> </Grid>
<Grid item minWidth="100%"> <Grid item minWidth="100%">
<br /> <FormGroup>
<FormControl variant="outlined" fullWidth> <FormControl variant="outlined" fullWidth>
<InputLabel htmlFor="user-password"> <InputLabel htmlFor="user-password">
Password: Password:
@@ -129,7 +150,9 @@ export const LoginWithImage = () => {
: "password" : "password"
} }
variant="outlined" variant="outlined"
onChange={handlePassword("password")} onChange={handlePassword(
"password",
)}
label="Password:" label="Password:"
endAdornment={ endAdornment={
<InputAdornment position="end"> <InputAdornment position="end">
@@ -149,6 +172,18 @@ export const LoginWithImage = () => {
} }
/> />
</FormControl> </FormControl>
<FormControlLabel
control={
<Checkbox
checked={rememberMe}
onChange={
handleCheckRememberMe
}
/>
}
label="Remember me"
/>
</FormGroup>
</Grid> </Grid>
<Grid item minWidth="100%"> <Grid item minWidth="100%">
<LoadingButton <LoadingButton
@@ -172,10 +207,6 @@ export const LoginWithImage = () => {
export const UserLogin = () => { export const UserLogin = () => {
const [userList, setUsers] = useState([]); const [userList, setUsers] = useState([]);
const navigate = useNavigate(); const navigate = useNavigate();
const cookies = new Cookies();
const currentServer = cookies.get("currentServer");
const serverList = cookies.get("servers");
const handleChangeServer = () => { const handleChangeServer = () => {
navigate("/servers/list"); navigate("/servers/list");
@@ -189,7 +220,7 @@ export const UserLogin = () => {
return users; return users;
// return {}; // return {};
}; };
useEffect(() => { useMemo(() => {
getUsers().then((users) => { getUsers().then((users) => {
setUsers(users.data); setUsers(users.data);
console.log(userList); console.log(userList);
@@ -200,7 +231,9 @@ export const UserLogin = () => {
<> <>
<AppBarBackOnly /> <AppBarBackOnly />
<Container maxWidth="md" className="centered"> <Container maxWidth="md" className="centered">
<h1 color="white">WIP-user login</h1> <Typography variant="h3" sx={{ marginBottom: "1em" }}>
Users
</Typography>
<div className="userList"> <div className="userList">
{userList.map((item, index) => { {userList.map((item, index) => {
return ( return (
@@ -251,6 +284,8 @@ export const UserLogin = () => {
export const UserLoginManual = () => { export const UserLoginManual = () => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [rememberMe, setRememberMe] = useState(true);
const navigate = useNavigate(); const navigate = useNavigate();
const { enqueueSnackbar } = useSnackbar(); const { enqueueSnackbar } = useSnackbar();
@@ -297,12 +332,19 @@ export const UserLoginManual = () => {
setLoading(true); setLoading(true);
const user = await authUser(); const user = await authUser();
sessionStorage.setItem("accessToken", user.data.AccessToken); sessionStorage.setItem("accessToken", user.data.AccessToken);
if (rememberMe == true) {
saveUser(userName, password.password);
}
event.emit("set-api-accessToken", window.api.basePath); event.emit("set-api-accessToken", window.api.basePath);
// setAccessToken(user.data.AccessToken) // setAccessToken(user.data.AccessToken)
setLoading(false); setLoading(false);
navigate(`/home`); navigate(`/home`);
}; };
const handleCheckRememberMe = (event) => {
setRememberMe(event.target.checked);
};
return ( return (
<> <>
<AppBarBackOnly /> <AppBarBackOnly />
@@ -338,6 +380,7 @@ export const UserLoginManual = () => {
</FormControl> </FormControl>
</Grid> </Grid>
<Grid sx={{ width: "100%" }} item xl={5} md={6}> <Grid sx={{ width: "100%" }} item xl={5} md={6}>
<FormGroup>
<FormControl <FormControl
sx={{ width: "100%" }} sx={{ width: "100%" }}
variant="outlined" variant="outlined"
@@ -354,7 +397,9 @@ export const UserLoginManual = () => {
: "password" : "password"
} }
variant="outlined" variant="outlined"
onChange={handlePassword("password")} onChange={handlePassword(
"password",
)}
label="Password:" label="Password:"
endAdornment={ endAdornment={
<InputAdornment position="end"> <InputAdornment position="end">
@@ -374,6 +419,18 @@ export const UserLoginManual = () => {
} }
/> />
</FormControl> </FormControl>
<FormControlLabel
control={
<Checkbox
checked={rememberMe}
onChange={
handleCheckRememberMe
}
/>
}
label="Remember me"
/>
</FormGroup>
</Grid> </Grid>
<Grid item xl={5} md={6} sx={{ width: "100%" }}> <Grid item xl={5} md={6} sx={{ width: "100%" }}>
<LoadingButton <LoadingButton
+5 -6
View File
@@ -8,6 +8,7 @@ import { v4 as uuidv4 } from "uuid";
import { EventEmitter as event } from "../../../eventEmitter.js"; import { EventEmitter as event } from "../../../eventEmitter.js";
import { setServer } from "../../../utils/store/servers.js"; import { setServer } from "../../../utils/store/servers.js";
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api"; import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api";
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
// MUI // MUI
import TextField from "@mui/material/TextField"; import TextField from "@mui/material/TextField";
@@ -15,8 +16,6 @@ import Typography from "@mui/material/Typography";
import Grid from "@mui/material/Grid"; import Grid from "@mui/material/Grid";
import LoadingButton from "@mui/lab/LoadingButton"; import LoadingButton from "@mui/lab/LoadingButton";
import SvgIcon from "@mui/material/SvgIcon"; import SvgIcon from "@mui/material/SvgIcon";
import Button from "@mui/material/Button";
import LinearProgress from "@mui/material/LinearProgress";
import Container from "@mui/material/Container"; import Container from "@mui/material/Container";
import { mdiChevronRight } from "@mdi/js"; import { mdiChevronRight } from "@mdi/js";
@@ -136,8 +135,8 @@ export const ServerSetup = (props) => {
const usersAvailable = async () => { const usersAvailable = async () => {
const users = await getUserApi(window.api).getPublicUsers(); const users = await getUserApi(window.api).getPublicUsers();
console.log("Users => ", users);
if (users.data.length >= 1) { if (users.data.length >= 1) {
console.log("Users => ", users.data);
return true; return true;
} else { } else {
return false; return false;
@@ -152,7 +151,7 @@ export const ServerSetup = (props) => {
sysInfo.Ip = serverIp; sysInfo.Ip = serverIp;
setServer(sysInfo); setServer(sysInfo);
const usersAvailablity = usersAvailable(); const usersAvailablity = await usersAvailable();
if (usersAvailablity == true) { if (usersAvailablity == true) {
console.log(usersAvailablity); console.log(usersAvailablity);
navigate("/login/users"); navigate("/login/users");
@@ -195,7 +194,7 @@ export const ServerSetup = (props) => {
return ( return (
<> <>
<Container <Container
maxWidth="md" maxWidth="sm"
className={ className={
checkingServer checkingServer
? "centered serverContainer loading" ? "centered serverContainer loading"
@@ -210,7 +209,7 @@ export const ServerSetup = (props) => {
alignItems="center" alignItems="center"
> >
<Grid item xl={5} md={6} sx={{ marginBottom: "1em" }}> <Grid item xl={5} md={6} sx={{ marginBottom: "1em" }}>
<JellyplayerLogo className="logo" /> <Typography variant="h3">Add Server</Typography>
</Grid> </Grid>
<Grid item xl={5} md={6} sx={{ width: "100%" }}> <Grid item xl={5} md={6} sx={{ width: "100%" }}>
<TextField <TextField
+19
View File
@@ -0,0 +1,19 @@
/** @format */
import { Store } from "tauri-plugin-store-api";
const user = new Store(".user.dat");
/**
* Set User details to .user.dat
* @param {string} userName - User's name
* @param {string} userPassword - User's Password
*/
const saveUser = (userName, userPassword) => {
let details = user.set("details", {
Name: userName,
Password: userPassword,
});
};
export { saveUser };