-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useContext } from "react"; | ||
import { Redirect, Route } from "react-router"; | ||
import { GlobalContext } from "./store"; | ||
|
||
const ProtectedRoute = ({ children, ...rest }) => { | ||
const { state } = useContext(GlobalContext); | ||
const { auth } = state; | ||
console.log("Protected Route was called"); | ||
return ( | ||
<Route | ||
{...rest} | ||
render={({ location }) => | ||
auth.isAuth ? ( | ||
children | ||
) : ( | ||
<Redirect | ||
to={{ | ||
pathname: "/login", | ||
state: { from: location }, | ||
}} | ||
/> | ||
) | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
export default ProtectedRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React, { useContext, useEffect } from "react"; | ||
import { GlobalContext } from "../store"; | ||
import { Redirect } from "react-router"; | ||
|
||
export const Login = () => { | ||
const { dispatch } = useContext(GlobalContext); | ||
return <div onClick={() => dispatch({ type: "LOGIN" })}>Login</div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import React from "react"; | ||
import React, { useContext } from "react"; | ||
import { GlobalContext } from "../store"; | ||
|
||
const Secrets = () => { | ||
return <div>Secrets</div>; | ||
const { state } = useContext(GlobalContext); | ||
React.useEffect(() => { | ||
console.log(state); | ||
}, [state]); | ||
return <div>{state.auth.isLoaded}</div>; | ||
}; | ||
|
||
export default Secrets; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { useReducer } from "react"; | ||
|
||
//Gobal initialState | ||
export const initialState = { | ||
auth: { | ||
isLoaded: false, | ||
isAuth: false, | ||
authObj: null, | ||
}, | ||
theme: { | ||
light: { background: "#fff" }, | ||
dark: { background: "#111" }, | ||
}, | ||
}; | ||
|
||
//Global Context | ||
export const GlobalContext = React.createContext(); | ||
|
||
//Global Reducer | ||
const GlobalReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case "LOGIN": | ||
console.log("logged in"); | ||
return { ...state, auth: { isAuth: true, isLoaded: true } }; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
//GlobalContext Provider | ||
export const GlobalProvider = ({ children }) => { | ||
const [state, dispatch] = useReducer(GlobalReducer, initialState); | ||
return ( | ||
<GlobalContext.Provider value={{ state, dispatch }}> | ||
{children} | ||
</GlobalContext.Provider> | ||
); | ||
}; |