Skip to content

Commit

Permalink
fix: fixes login screen
Browse files Browse the repository at this point in the history
Co-authored-by: Victor Rodrigues <victorrsilva17@gmail.com>
  • Loading branch information
lucasvmx and VictorRodriguesS0 committed Nov 10, 2021
1 parent a080008 commit d09b708
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 73 deletions.
15 changes: 9 additions & 6 deletions src/Auth/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export const STORAGE_KEY = "user-token";

const isAuthenticated = () => !!localStorage.getItem(STORAGE_KEY);

const hasToken = () => isAuthenticated();

const login = (token) => localStorage.setItem(STORAGE_KEY, token);

const logout = () => localStorage.removeItem(STORAGE_KEY);

// Check if User token on local storage is valid
const tokenCheck = () => {
if (isAuthenticated()) {
const tokenExpired = () => {
if (hasToken()) {
const token = getToken();

// Decode token
Expand All @@ -22,13 +24,14 @@ const tokenCheck = () => {
const now = new Date();

// Checks if token is expired
if (expirationTime < now) {
return false;
} else {
if (expirationTime > now) {
// if token expired we need to clear it from local storage
logout();
return true;
}
}

return false;
};

export { isAuthenticated, login, logout, tokenCheck };
export { isAuthenticated, login, logout, tokenExpired, hasToken };
6 changes: 6 additions & 0 deletions src/Pages/HomePage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const HomePage = () => {
const fetchProcess = async () => {
// Fetch user info
const user = await getInfoUser(toast);

if (user.levels == undefined) {
this.props.history.push("/");
return;
}

//Check if user is admin
setAdmin(userType.admin === user.levels[0].id);
//Set the name of user's department
Expand Down
79 changes: 12 additions & 67 deletions src/Routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { BrowserRouter, Redirect, Route, Switch } from "react-router-dom";
import toast, { Toaster } from "react-hot-toast";
import { isAuthenticated, tokenCheck } from "./Auth/Auth";
import { hasToken, isAuthenticated, tokenExpired } from "./Auth/Auth";
import CreateRecord from "./Pages/CreateRecord";
import LoginScreen from "./Pages/LoginScreen";
import ViewProfile from "./Pages/ViewProfile";
Expand All @@ -19,46 +19,17 @@ import EditRecord from "./Pages/EditRecord";
import EditDepartment from "./Pages/EditDepartment";
import GenericBlueButton from "./Components/GenericBlueButton";


const PrivateRoutes = ({ component: Component, ...prop }) => (
<Route
{...prop}
render={(props) =>
// Check if the user has a valid token on his browser
tokenCheck() ? (
hasToken() && !tokenExpired() ? (
<Component {...props} />
) : (
<>
{
//Show a pop-up to inform user that his session is expired
toast(
(t) => (
<span style={{ textAlign: "center" }}>
<p style={{ fontSize: "18px" }}>
{
// Check if user has a token in storage
isAuthenticated() ? "Sessão Expirada!" : "Acesso negado!"
}
</p>
<p>Para continuar realize login!</p>
<GenericBlueButton
title="Ok"
onClick={() => {
toast.dismiss(t.id);
}}
></GenericBlueButton>
</span>
),
{
//Set the duration of the pop-up
duration: 10000000,
}
)
}
{/* Redirect the user to login-screen if it's not logged */}
<Redirect
to={{ pathname: "/login", state: { from: props.location } }}
/>
/* Redirect the user to login-screen if it's not logged */
<Redirect to={{ pathname: "/login", state: { from: props.location } }} />
<Toaster />
</>
)
Expand All @@ -69,27 +40,13 @@ const PrivateRoutes = ({ component: Component, ...prop }) => (
const Routes = () => (
<BrowserRouter history={history}>
<Switch>
<Route
exact
path="/login"
component={() => <LoginScreen history={history} />}
/>
<PrivateRoutes
exact
path="/ver-registro/:id"
component={() => <ViewRecord />}
/>
<Route exact path="/login" component={() => <LoginScreen history={history} />} />
<PrivateRoutes exact path="/ver-registro/:id" component={() => <ViewRecord />} />
<PrivateRoutes path="/tela-inicial" component={() => <HomePage />} />
<PrivateRoutes
path="/criar-registro"
component={() => <CreateRecord />}
/>
<PrivateRoutes path="/criar-registro" component={() => <CreateRecord />} />
<PrivateRoutes path="/criar-usuario" component={() => <CreateUser />} />
<PrivateRoutes path="/usuario" component={() => <ViewProfile />} />
<PrivateRoutes
path="/alterar-senha"
component={() => <ChangePassword />}
/>
<PrivateRoutes path="/alterar-senha" component={() => <ChangePassword />} />
<PrivateRoutes
path="/visualizar-registros"
component={() => <AllRegistersScreen />}
Expand All @@ -98,22 +55,10 @@ const Routes = () => (
path="/visualizar-departamentos"
component={() => <AllDepartmentsScreen />}
/>
<PrivateRoutes
path="/todos-os-campos"
component={() => <ViewAllFields />}
/>
<PrivateRoutes
path="/criar-departamento"
component={() => <CreateDepartment />}
/>
<PrivateRoutes
path="/visualizar-usuarios"
component={() => <ViewAllUsers />}
/>
<PrivateRoutes
path="/editar-registro/:id"
component={() => <EditRecord />}
/>
<PrivateRoutes path="/todos-os-campos" component={() => <ViewAllFields />} />
<PrivateRoutes path="/criar-departamento" component={() => <CreateDepartment />} />
<PrivateRoutes path="/visualizar-usuarios" component={() => <ViewAllUsers />} />
<PrivateRoutes path="/editar-registro/:id" component={() => <EditRecord />} />
<PrivateRoutes
exact
path="/editar-departamento/:id"
Expand Down

0 comments on commit d09b708

Please sign in to comment.