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
+154 -79

No files matched your search

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