From cff580648acd1b204e30d6a20a5ae0271dde9f1d Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Thu, 24 Dec 2020 15:56:30 +0200 Subject: [PATCH] login step update --- public/locales/en/translations.json | 2 + public/locales/ro/translations.json | 2 + src/components/layout/Main.js | 10 +-- .../login/components/LoggedInComponent.js | 80 +++++++++++++++++++ src/features/login/components/LoginCard.js | 25 ++++++ .../login/components/LoginComponent.js | 15 +++- .../login/components/LoginContainer.js | 31 +++++-- src/hooks/useAuthorizationToken.js | 5 +- src/utils/identity.js | 11 ++- 9 files changed, 159 insertions(+), 22 deletions(-) create mode 100644 src/features/login/components/LoggedInComponent.js create mode 100644 src/features/login/components/LoginCard.js diff --git a/public/locales/en/translations.json b/public/locales/en/translations.json index e84a894..2a998b4 100644 --- a/public/locales/en/translations.json +++ b/public/locales/en/translations.json @@ -21,6 +21,8 @@ "Username": "Username", "Password": "Password", "Label": "Login", + "ChangeUser": "Change user", + "Logout": "Logout", "IncorrectCredentials": "Incorrect credentials." } } diff --git a/public/locales/ro/translations.json b/public/locales/ro/translations.json index 856f529..e2809c3 100644 --- a/public/locales/ro/translations.json +++ b/public/locales/ro/translations.json @@ -12,6 +12,8 @@ "Username": "Utilizator", "Password": "Parolă", "Label": "Autentificare", + "ChangeUser": "Schimbă utilizatorul", + "Logout": "Deconectare", "IncorrectCredentials": "Credențiale incorecte." } } diff --git a/src/components/layout/Main.js b/src/components/layout/Main.js index 35f83bf..ccc8094 100644 --- a/src/components/layout/Main.js +++ b/src/components/layout/Main.js @@ -7,7 +7,6 @@ import LoginContainer from "../../features/login/components/LoginContainer"; const useStyles = makeStyles(() => ({ app: { - textAlign: "center", backgroundColor: "#282c34", minHeight: "100vh", display: "flex", @@ -23,7 +22,6 @@ const useStyles = makeStyles(() => ({ color: "white" }, appLoginOnly: { - textAlign: "center", backgroundColor: "#282c34", minHeight: "100vh", display: "flex", @@ -34,12 +32,12 @@ const useStyles = makeStyles(() => ({ const Main = () => { const classes = useStyles(); - const { tokenIsValid } = useAuthorizationToken(); - const _tokenIsValid = tokenIsValid(); + const { validateToken } = useAuthorizationToken(); + const tokenIsValid = validateToken(); return ( -
- {_tokenIsValid ? ( +
+ {tokenIsValid ? ( <>
diff --git a/src/features/login/components/LoggedInComponent.js b/src/features/login/components/LoggedInComponent.js new file mode 100644 index 0000000..bfb51f4 --- /dev/null +++ b/src/features/login/components/LoggedInComponent.js @@ -0,0 +1,80 @@ +import React from "react"; +import { makeStyles } from "@material-ui/core/styles"; +import Card from "@material-ui/core/Card"; +import CardHeader from "@material-ui/core/CardHeader"; +import CardContent from "@material-ui/core/CardContent"; +import CardActions from "@material-ui/core/CardActions"; +import { Avatar, Collapse, Tooltip, Divider } from "@material-ui/core"; +import IconButton from "@material-ui/core/IconButton"; +import { AccountBox, RotateLeft, ExitToApp } from "@material-ui/icons"; +import LoginComponent from "./LoginComponent"; +import { useTranslation } from "react-i18next"; + +const useStyles = makeStyles(theme => ({ + root: { + minWidth: 350 + }, + onRight: { + marginLeft: "auto" + }, + avatar: { + backgroundColor: theme.palette.primary.main + } +})); + +const LoggedInComponent = ({ credentials, onChange, onLogin, onLogout }) => { + const classes = useStyles(); + const { t } = useTranslation(); + const [expanded, setExpanded] = React.useState(false); + + const handleExpandLogin = () => { + setExpanded(!expanded); + }; + + return ( + + + + + } + title={{t("Server.ActiveSession")}} + subheader="September 14, 2016" + /> + + + + + + + + + + + + + + + + + + + + + ); +}; + +LoggedInComponent.propTypes = {}; + +export default LoggedInComponent; diff --git a/src/features/login/components/LoginCard.js b/src/features/login/components/LoginCard.js new file mode 100644 index 0000000..50b817f --- /dev/null +++ b/src/features/login/components/LoginCard.js @@ -0,0 +1,25 @@ +import React from "react"; +import PropTypes from "prop-types"; +import { Card } from "@material-ui/core"; + +import LoginComponent from "./LoginComponent"; + +const LoginCard = ({ credentials, onChange, onLogin }) => { + return ( + + + + ); +}; + +LoginCard.propTypes = { + credentials: PropTypes.object.isRequired, + onChange: PropTypes.func.isRequired, + onLogin: PropTypes.func.isRequired +}; + +export default LoginCard; diff --git a/src/features/login/components/LoginComponent.js b/src/features/login/components/LoginComponent.js index 3e451d2..398d3b2 100644 --- a/src/features/login/components/LoginComponent.js +++ b/src/features/login/components/LoginComponent.js @@ -5,7 +5,6 @@ import { TextField, InputAdornment, Button, - Card, CardActions, CardContent } from "@material-ui/core"; @@ -17,6 +16,9 @@ const useStyles = makeStyles(theme => ({ field: { margin: theme.spacing(1), width: "300px" + }, + onRight: { + marginLeft: "auto" } })); @@ -25,7 +27,7 @@ const LoginComponent = ({ credentials, onChange, onLogin }) => { const { t } = useTranslation(); return ( - + <> { /> - - + ); }; diff --git a/src/features/login/components/LoginContainer.js b/src/features/login/components/LoginContainer.js index c2a33d7..b589cb2 100644 --- a/src/features/login/components/LoginContainer.js +++ b/src/features/login/components/LoginContainer.js @@ -1,18 +1,20 @@ import React, { useContext } from "react"; -import LoginComponent from "./LoginComponent"; -import { authenticate } from "../../../utils/identity"; +import LoginCard from "./LoginCard"; +import { authenticate, invalidate } from "../../../utils/identity"; import { ApplicationStateContext, ApplicationDispatchContext } from "../../../state/ApplicationContexts"; -import { useToast } from "../../../hooks"; +import { useToast, useAuthorizationToken } from "../../../hooks"; import { useTranslation } from "react-i18next"; +import LoggedInComponent from "./LoggedInComponent"; const LoginContainer = () => { const state = useContext(ApplicationStateContext); const dispatchActions = useContext(ApplicationDispatchContext); const { error } = useToast(); const { t } = useTranslation(); + const { tokenIsValid } = useAuthorizationToken(); const handleChange = prop => event => { dispatchActions.onCredentialsChange(prop, event.target.value); @@ -33,13 +35,26 @@ const LoginContainer = () => { } }; + const handleLogout = () => { + invalidate(); + }; + return ( <> - + {tokenIsValid ? ( + + ) : ( + + )} ); }; diff --git a/src/hooks/useAuthorizationToken.js b/src/hooks/useAuthorizationToken.js index ee97a0b..25da53a 100644 --- a/src/hooks/useAuthorizationToken.js +++ b/src/hooks/useAuthorizationToken.js @@ -5,7 +5,7 @@ export const useAuthorizationToken = () => { const state = useContext(ApplicationStateContext); const getToken = () => state.security.authorization.token; - const tokenIsValid = () => { + const validateToken = () => { const token = getToken(); if (!token) { return false; @@ -15,5 +15,6 @@ export const useAuthorizationToken = () => { return valid; }; - return { getToken, tokenIsValid }; + const tokenIsValid = validateToken(); + return { getToken, validateToken, tokenIsValid }; }; diff --git a/src/utils/identity.js b/src/utils/identity.js index f7e105f..37c5446 100644 --- a/src/utils/identity.js +++ b/src/utils/identity.js @@ -1,5 +1,5 @@ import { request } from "./axios"; -import { setItem } from "./localStorage"; +import { setItem, getItem, removeItem } from "./localStorage"; const storageKeys = { TOKEN: "AUTHORIZATION_TOKEN" @@ -22,4 +22,11 @@ const authenticate = async (username, password) => { return response; }; -export { storageKeys, authenticate }; +const invalidate = () => { + const token = getItem(storageKeys.TOKEN); + if (token) { + removeItem(storageKeys.TOKEN); + } +}; + +export { storageKeys, authenticate, invalidate };