login step update
parent
80f9e63a9b
commit
e9c38a5fbc
|
@ -2,6 +2,8 @@ import React from "react";
|
|||
import ApplicationStepper from "./stepper/ApplicationStepper";
|
||||
import Switcher from "./Switcher";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
import { useAuthorizationToken } from "../../hooks";
|
||||
import LoginContainer from "../../features/login/components/LoginContainer";
|
||||
|
||||
const useStyles = makeStyles(() => ({
|
||||
app: {
|
||||
|
@ -19,18 +21,34 @@ const useStyles = makeStyles(() => ({
|
|||
justifyContent: "center",
|
||||
fontSize: "calc(10px + 2vmin)",
|
||||
color: "white"
|
||||
},
|
||||
appLoginOnly: {
|
||||
textAlign: "center",
|
||||
backgroundColor: "#282c34",
|
||||
minHeight: "100vh",
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
justifyContent: "center"
|
||||
}
|
||||
}));
|
||||
|
||||
const Main = () => {
|
||||
const classes = useStyles();
|
||||
const { tokenIsValid } = useAuthorizationToken();
|
||||
const _tokenIsValid = tokenIsValid();
|
||||
|
||||
return (
|
||||
<div className={classes.app}>
|
||||
<ApplicationStepper />
|
||||
<div className={classes.content}>
|
||||
<Switcher />
|
||||
</div>
|
||||
<div className={_tokenIsValid ? classes.app : classes.appLoginOnly}>
|
||||
{_tokenIsValid ? (
|
||||
<>
|
||||
<ApplicationStepper />
|
||||
<div className={classes.content}>
|
||||
<Switcher />
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<LoginContainer />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -23,7 +23,9 @@ const LoginContainer = () => {
|
|||
|
||||
try {
|
||||
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"));
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
export { useToast } from "./useToast";
|
||||
export { useAuthorizationToken } from "./useAuthorizationToken";
|
||||
|
|
|
@ -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 };
|
||||
};
|
|
@ -3,6 +3,11 @@ export const initialState = {
|
|||
userName: "",
|
||||
password: ""
|
||||
},
|
||||
security: {
|
||||
authorization: {
|
||||
token: null
|
||||
}
|
||||
},
|
||||
network: {
|
||||
test: ""
|
||||
}
|
||||
|
|
|
@ -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: {
|
||||
return state;
|
||||
}
|
||||
|
@ -30,5 +43,7 @@ export const dispatchActions = dispatch => ({
|
|||
onCredentialsChange: (prop, value) =>
|
||||
dispatch({ type: "onCredentialsChange", payload: { prop, value } }),
|
||||
onNetworkChange: (prop, value) =>
|
||||
dispatch({ type: "onNetworkChange", payload: { prop, value } })
|
||||
dispatch({ type: "onNetworkChange", payload: { prop, value } }),
|
||||
onAuthorizationTokenChange: token =>
|
||||
dispatch({ type: "onAuthorizationTokenChange", payload: { token } })
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue