From 2cf66ec515c8c8af31f3ccc03a557a9857262f45 Mon Sep 17 00:00:00 2001 From: senaarth Date: Wed, 13 Oct 2021 21:28:27 -0300 Subject: [PATCH] feat: creating auth flow --- src/App.tsx | 17 ++++-------- src/hooks/useAuth.tsx | 64 +++++++++++++++++++++++++++++++++++++++++++ src/routes.tsx | 20 ++++++++++++++ 3 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 src/hooks/useAuth.tsx create mode 100644 src/routes.tsx diff --git a/src/App.tsx b/src/App.tsx index 6b99151..b160c45 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,22 @@ import React from "react"; -// import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; - -// Pages Imports -// import { Home } from "./pages/Home"; // Components Imports import { Header } from "./components/Header"; import { Footer } from "./components/Footer"; +import { Router } from "./routes"; +import { AuthProvider } from "./hooks/useAuth"; + // Styles Imports import "./styles/global.css"; function App() { return ( -
+
- {/* - - - - */} +
-
+ ); } diff --git a/src/hooks/useAuth.tsx b/src/hooks/useAuth.tsx new file mode 100644 index 0000000..56b0f9d --- /dev/null +++ b/src/hooks/useAuth.tsx @@ -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(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 { + setUser({ + name: "Nome de Teste", + email: "teste@teste.com", + }); + + localStorage.setItem("@PetStop:user", JSON.stringify(user)); + } + + async function signOut() { + setUser(null); + localStorage.removeItem("@PetStop:user"); + } + + return ( + + {children} + + ); +}; + +export function useAuth() { + const context = useContext(AuthContext); + + return context; +} diff --git a/src/routes.tsx b/src/routes.tsx new file mode 100644 index 0000000..86d8da6 --- /dev/null +++ b/src/routes.tsx @@ -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 ( + + + + + + ); +}