-
Notifications
You must be signed in to change notification settings - Fork 1
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
3 changed files
with
90 additions
and
11 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,64 @@ | ||
import React, { createContext, useState, useEffect, useContext } from "react"; | ||
|
||
type User = { | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
interface AuthContextProps { | ||
signedIn: boolean; | ||
user: User | null; | ||
signIn: (email: string, name: string) => void; | ||
signOut: () => void; | ||
} | ||
|
||
const AuthContext = createContext({ | ||
signedIn: false, | ||
} as AuthContextProps); | ||
|
||
export const AuthProvider: React.FC = ({ children }) => { | ||
const [user, setUser] = useState<User | null>(null); | ||
|
||
useEffect(() => { | ||
async function getStoredData() { | ||
const _user = localStorage.getItem("@PetStop:user"); | ||
|
||
if (_user) setUser(JSON.parse(_user)); | ||
} | ||
|
||
getStoredData(); | ||
}); | ||
|
||
async function signIn(email: string, password: string): Promise<void> { | ||
setUser({ | ||
name: "Nome de Teste", | ||
email: "[email protected]", | ||
}); | ||
|
||
localStorage.setItem("@PetStop:user", JSON.stringify(user)); | ||
} | ||
|
||
async function signOut() { | ||
setUser(null); | ||
localStorage.removeItem("@PetStop:user"); | ||
} | ||
|
||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
signedIn: !!user, | ||
user, | ||
signIn, | ||
signOut, | ||
}} | ||
> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export function useAuth() { | ||
const context = useContext(AuthContext); | ||
|
||
return context; | ||
} |
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,20 @@ | ||
import React from "react"; | ||
import { BrowserRouter, Switch, Route } from "react-router-dom"; | ||
|
||
// Pages Imports | ||
import { Home } from "./pages/Home"; | ||
|
||
// Utils Imports | ||
// import { useAuth } from "./hooks/useAuth"; | ||
|
||
export function Router() { | ||
// const { signedIn } = useAuth(); | ||
|
||
return ( | ||
<BrowserRouter> | ||
<Switch> | ||
<Route path="/" exact component={Home} /> | ||
</Switch> | ||
</BrowserRouter> | ||
); | ||
} |