diff --git a/src/components/App.js b/src/components/App.js index 23f3683..7f6ca2f 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,4 +1,4 @@ -import React, { Suspense } from "react"; +import React, { useEffect } from "react"; import { HashRouter, Route, Switch, Redirect } from "react-router-dom"; // components @@ -10,31 +10,31 @@ import Login from "../pages/login"; // context import { useUserState } from "../context/UserContext"; +import { useTranslation } from "react-i18next"; export default function App() { - // global - var { isAuthenticated } = useUserState(); + var { authenticated, messageCode } = useUserState(); + const { t } = useTranslation(); + + useEffect(() => { + if (!messageCode) return; + alert(t(messageCode)); + }, [messageCode, t]); return ( - Loading...}> - - - } - /> - } - /> - - - - - - + + + } /> + } + /> + + + + + ); function PrivateRoute({ component, ...rest }) { @@ -42,7 +42,7 @@ export default function App() { - isAuthenticated ? ( + authenticated ? ( React.createElement(component, props) ) : ( - isAuthenticated ? ( + authenticated ? ( { - localStorage.setItem('id_token', 1) - setError(null) - setIsLoading(false) - dispatch({ type: 'LOGIN_SUCCESS' }) + authenticate(login, password).then((response) => { + if (response.status === "SUCCESS") { + setError(null); + setIsLoading(false); + dispatch({ type: "LOGIN_SUCCESS", payload: { token: response.token } }); + } else if (response.status === "BAD_CREDENTIALS") { + setLoginFailed( + dispatch, + setError, + setIsLoading, + "Login.IncorrectCredentials" + ); + } + }); - history.push('/app/dashboard') - }, 2000); + history.push("/app/dashboard"); } else { - dispatch({ type: "LOGIN_FAILURE" }); - setError(true); - setIsLoading(false); + setLoginFailed(dispatch, setError, setIsLoading, "Login.EmptyCredentials"); } } function signOut(dispatch, history) { - localStorage.removeItem("id_token"); + invalidate(); dispatch({ type: "SIGN_OUT_SUCCESS" }); history.push("/login"); } + +export { UserProvider, useUserState, useUserDispatch, loginUser, signOut }; diff --git a/src/hooks/index.js b/src/hooks/index.js new file mode 100644 index 0000000..356f966 --- /dev/null +++ b/src/hooks/index.js @@ -0,0 +1 @@ +//export { useAuthorizationToken } from "./useAuthorizationToken"; diff --git a/src/index.js b/src/index.js index 20c057e..139a7fe 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { Suspense } from "react"; import ReactDOM from "react-dom"; import { ThemeProvider } from "@material-ui/styles"; import { CssBaseline } from "@material-ui/core"; @@ -14,7 +14,9 @@ ReactDOM.render( - + Loading...}> + + , diff --git a/src/utils/identity.js b/src/utils/identity.js index ba79528..14912c5 100644 --- a/src/utils/identity.js +++ b/src/utils/identity.js @@ -32,4 +32,18 @@ const invalidate = () => { } }; -export { storageKeys, authenticate, invalidate }; +const validateToken = () => { + const token = getItem(storageKeys.TOKEN); + if (!token) { + return false; + } + + const valid = new Date(token.validUntil) >= new Date(); + if (!valid) { + invalidate(); + } + + return valid; +}; + +export { storageKeys, authenticate, invalidate, validateToken };