login step update

master
Tudor Stanciu 2020-12-24 05:32:35 +02:00
parent 80f9e63a9b
commit e9c38a5fbc
6 changed files with 67 additions and 7 deletions

View File

@ -2,6 +2,8 @@ import React from "react";
import ApplicationStepper from "./stepper/ApplicationStepper"; import ApplicationStepper from "./stepper/ApplicationStepper";
import Switcher from "./Switcher"; import Switcher from "./Switcher";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import { useAuthorizationToken } from "../../hooks";
import LoginContainer from "../../features/login/components/LoginContainer";
const useStyles = makeStyles(() => ({ const useStyles = makeStyles(() => ({
app: { app: {
@ -19,18 +21,34 @@ const useStyles = makeStyles(() => ({
justifyContent: "center", justifyContent: "center",
fontSize: "calc(10px + 2vmin)", fontSize: "calc(10px + 2vmin)",
color: "white" color: "white"
},
appLoginOnly: {
textAlign: "center",
backgroundColor: "#282c34",
minHeight: "100vh",
display: "flex",
flexDirection: "column",
justifyContent: "center"
} }
})); }));
const Main = () => { const Main = () => {
const classes = useStyles(); const classes = useStyles();
const { tokenIsValid } = useAuthorizationToken();
const _tokenIsValid = tokenIsValid();
return ( return (
<div className={classes.app}> <div className={_tokenIsValid ? classes.app : classes.appLoginOnly}>
{_tokenIsValid ? (
<>
<ApplicationStepper /> <ApplicationStepper />
<div className={classes.content}> <div className={classes.content}>
<Switcher /> <Switcher />
</div> </div>
</>
) : (
<LoginContainer />
)}
</div> </div>
); );
}; };

View File

@ -23,7 +23,9 @@ const LoginContainer = () => {
try { try {
const response = await authenticate(userName, password); const response = await authenticate(userName, password);
if (response.status === "BAD_CREDENTIALS") { if (response.status === "SUCCESS") {
dispatchActions.onAuthorizationTokenChange(response.token);
} else if (response.status === "BAD_CREDENTIALS") {
error(t("Login.IncorrectCredentials")); error(t("Login.IncorrectCredentials"));
} }
} catch (err) { } catch (err) {

View File

@ -1 +1,2 @@
export { useToast } from "./useToast"; export { useToast } from "./useToast";
export { useAuthorizationToken } from "./useAuthorizationToken";

View File

@ -0,0 +1,19 @@
import { useContext } from "react";
import { ApplicationStateContext } from "../state/ApplicationContexts";
export const useAuthorizationToken = () => {
const state = useContext(ApplicationStateContext);
const getToken = () => state.security.authorization.token;
const tokenIsValid = () => {
const token = getToken();
if (!token) {
return false;
}
const valid = new Date(token.validUntil) >= new Date();
return valid;
};
return { getToken, tokenIsValid };
};

View File

@ -3,6 +3,11 @@ export const initialState = {
userName: "", userName: "",
password: "" password: ""
}, },
security: {
authorization: {
token: null
}
},
network: { network: {
test: "" test: ""
} }

View File

@ -20,6 +20,19 @@ export function reducer(state, action) {
} }
}; };
} }
case "onAuthorizationTokenChange": {
const { token } = action.payload;
return {
...state,
security: {
...state.security,
authorization: {
...state.security.authorization,
token
}
}
};
}
default: { default: {
return state; return state;
} }
@ -30,5 +43,7 @@ export const dispatchActions = dispatch => ({
onCredentialsChange: (prop, value) => onCredentialsChange: (prop, value) =>
dispatch({ type: "onCredentialsChange", payload: { prop, value } }), dispatch({ type: "onCredentialsChange", payload: { prop, value } }),
onNetworkChange: (prop, value) => onNetworkChange: (prop, value) =>
dispatch({ type: "onNetworkChange", payload: { prop, value } }) dispatch({ type: "onNetworkChange", payload: { prop, value } }),
onAuthorizationTokenChange: token =>
dispatch({ type: "onAuthorizationTokenChange", payload: { token } })
}); });