login step update

master
Tudor Stanciu 2020-12-24 18:22:07 +02:00
parent 84d9fda15d
commit 2d53a734d4
6 changed files with 69 additions and 20 deletions

View File

@ -23,6 +23,8 @@
"Label": "Login", "Label": "Login",
"ChangeUser": "Change user", "ChangeUser": "Change user",
"Logout": "Logout", "Logout": "Logout",
"IncorrectCredentials": "Incorrect credentials." "IncorrectCredentials": "Incorrect credentials.",
"Hello": "Hi, {{username}}",
"AuthenticationDate": "Authentication date"
} }
} }

View File

@ -14,6 +14,8 @@
"Label": "Autentificare", "Label": "Autentificare",
"ChangeUser": "Schimbă utilizatorul", "ChangeUser": "Schimbă utilizatorul",
"Logout": "Deconectare", "Logout": "Deconectare",
"IncorrectCredentials": "Credențiale incorecte." "IncorrectCredentials": "Credențiale incorecte.",
"Hello": "Salut, {{username}}",
"AuthenticationDate": "Momentul autentificării"
} }
} }

View File

@ -1,11 +1,18 @@
import React from "react"; import React, { useState, useCallback } from "react";
import PropTypes from "prop-types";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import Card from "@material-ui/core/Card"; import {
import CardHeader from "@material-ui/core/CardHeader"; Avatar,
import CardContent from "@material-ui/core/CardContent"; Collapse,
import CardActions from "@material-ui/core/CardActions"; Tooltip,
import { Avatar, Collapse, Tooltip, Divider } from "@material-ui/core"; Divider,
import IconButton from "@material-ui/core/IconButton"; Typography,
Card,
CardContent,
CardActions,
CardHeader,
IconButton
} from "@material-ui/core";
import { AccountBox, RotateLeft, ExitToApp } from "@material-ui/icons"; import { AccountBox, RotateLeft, ExitToApp } from "@material-ui/icons";
import LoginComponent from "./LoginComponent"; import LoginComponent from "./LoginComponent";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
@ -25,25 +32,52 @@ const useStyles = makeStyles(theme => ({
} }
})); }));
const LoggedInComponent = ({ credentials, onChange, onLogin, onLogout }) => { const LoggedInComponent = ({
credentials,
token,
onChange,
onLogin,
onLogout
}) => {
const classes = useStyles(); const classes = useStyles();
const { t } = useTranslation(); const { t } = useTranslation();
const [expanded, setExpanded] = React.useState(false); const [expanded, setExpanded] = useState(false);
const handleExpandLogin = () => { const handleExpandLogin = () => {
setExpanded(!expanded); setExpanded(!expanded);
}; };
const getTokenValidFrom = useCallback(() => {
const tokenValidFrom = token?.validFrom;
if (tokenValidFrom) {
const valueForDisplay = t("LONG_DATE", { date: tokenValidFrom });
return valueForDisplay;
}
return "N/A";
}, [token, t]);
return ( return (
<Card className={classes.card}> <Card className={classes.card}>
<CardHeader <CardHeader
avatar={ avatar={
<Avatar aria-label="recipe" className={classes.avatar}> <Avatar aria-label="user" className={classes.avatar}>
<AccountBox /> <AccountBox />
</Avatar> </Avatar>
} }
title={<strong>{t("Server.ActiveSession")}</strong>} title={
subheader="September 14, 2016" <strong>
{t("Login.Hello", { username: credentials.userName })}
</strong>
}
subheader={
<Tooltip title={t("Login.AuthenticationDate")}>
<Typography variant="caption" display="block">
{getTokenValidFrom()}
</Typography>
</Tooltip>
}
/> />
<CardActions disableSpacing> <CardActions disableSpacing>
@ -81,6 +115,12 @@ const LoggedInComponent = ({ credentials, onChange, onLogin, onLogout }) => {
); );
}; };
LoggedInComponent.propTypes = {}; LoggedInComponent.propTypes = {
credentials: PropTypes.object.isRequired,
token: PropTypes.object,
onChange: PropTypes.func.isRequired,
onLogin: PropTypes.func.isRequired,
onLogout: PropTypes.func.isRequired
};
export default LoggedInComponent; export default LoggedInComponent;

View File

@ -14,7 +14,7 @@ const LoginContainer = () => {
const dispatchActions = useContext(ApplicationDispatchContext); const dispatchActions = useContext(ApplicationDispatchContext);
const { error } = useToast(); const { error } = useToast();
const { t } = useTranslation(); const { t } = useTranslation();
const { tokenIsValid, invalidateToken } = useAuthorizationToken(); const { tokenIsValid, invalidateToken, getToken } = useAuthorizationToken();
const handleChange = prop => event => { const handleChange = prop => event => {
dispatchActions.onCredentialsChange(prop, event.target.value); dispatchActions.onCredentialsChange(prop, event.target.value);
@ -45,6 +45,7 @@ const LoginContainer = () => {
{tokenIsValid ? ( {tokenIsValid ? (
<LoggedInComponent <LoggedInComponent
credentials={state.credentials} credentials={state.credentials}
token={getToken()}
onChange={handleChange} onChange={handleChange}
onLogin={handleLogin} onLogin={handleLogin}
onLogout={handleLogout} onLogout={handleLogout}

View File

@ -2,10 +2,11 @@ import { getItem } from "../utils/localStorage";
import { storageKeys } from "../utils/identity"; import { storageKeys } from "../utils/identity";
const token = getItem(storageKeys.TOKEN); const token = getItem(storageKeys.TOKEN);
const userName = getItem(storageKeys.USER);
export const initialState = { export const initialState = {
credentials: { credentials: {
userName: "", userName: userName || "",
password: "" password: ""
}, },
security: { security: {

View File

@ -2,13 +2,14 @@ import { request } from "./axios";
import { setItem, getItem, removeItem } from "./localStorage"; import { setItem, getItem, removeItem } from "./localStorage";
const storageKeys = { const storageKeys = {
TOKEN: "AUTHORIZATION_TOKEN" TOKEN: "AUTHORIZATION_TOKEN",
USER: "USER_NAME"
}; };
const authenticate = async (username, password) => { const authenticate = async (userName, password) => {
const urlTemplate = process.env.REACT_APP_IDENTITY_AUTHENTICATION_URL; const urlTemplate = process.env.REACT_APP_IDENTITY_AUTHENTICATION_URL;
const url = urlTemplate const url = urlTemplate
.replace("{username}", username) .replace("{username}", userName)
.replace("{password}", password); .replace("{password}", password);
const options = { const options = {
method: "post" method: "post"
@ -17,6 +18,7 @@ const authenticate = async (username, password) => {
const response = await request(url, options); const response = await request(url, options);
if (response.status === "SUCCESS") { if (response.status === "SUCCESS") {
setItem(storageKeys.TOKEN, response.token); setItem(storageKeys.TOKEN, response.token);
setItem(storageKeys.USER, userName);
} }
return response; return response;
@ -26,6 +28,7 @@ const invalidate = () => {
const token = getItem(storageKeys.TOKEN); const token = getItem(storageKeys.TOKEN);
if (token) { if (token) {
removeItem(storageKeys.TOKEN); removeItem(storageKeys.TOKEN);
removeItem(storageKeys.USER);
} }
}; };