From fe812a371cae39a1744c5de0f43a38ac2562db9b Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 6 Jul 2021 19:26:39 +0300 Subject: [PATCH] login --- src/components/App.js | 48 +++++++++++++++++------------------ src/context/UserContext.js | 51 +++++++++++++++++++++++++------------- src/hooks/index.js | 1 + src/index.js | 6 +++-- src/utils/identity.js | 16 +++++++++++- 5 files changed, 78 insertions(+), 44 deletions(-) create mode 100644 src/hooks/index.js 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 };