-
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
ricardo
committed
Mar 14, 2024
1 parent
cf65efb
commit 0f47db6
Showing
15 changed files
with
680 additions
and
367 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,20 +1,25 @@ | ||
import { Container,Profile,Logout} from "./style"; | ||
import {RiShutDownLine} from'react-icons/ri' | ||
|
||
export function Header(){ | ||
return( | ||
|
||
<Container> | ||
<Profile to="/profile"> | ||
<img src="https://github.com/rickccastro.png" alt="foto do usuario" /> | ||
<div> | ||
<span>bem vindo</span> | ||
<strong>Ricardo Castro</strong> | ||
</div> | ||
</Profile> | ||
<Logout> | ||
<RiShutDownLine /> | ||
</Logout> | ||
</Container> | ||
) | ||
} | ||
import { Container,Profile,Logout} from "./style"; | ||
import {RiShutDownLine} from'react-icons/ri' | ||
import { api } from "../../services/api"; | ||
import { useAuth } from "../../hooks/auth"; | ||
|
||
export function Header(){ | ||
const {signOut,user}=useAuth() | ||
|
||
const avatarUrl=user.avatar ? `${api.defaults.baseURL}/files/${user.avatar}` : avatarPlacehoalder | ||
|
||
return( | ||
<Container> | ||
<Profile to="/profile"> | ||
<img src={avatarUrl} alt={user.name} /> | ||
<div> | ||
<span>bem vindo</span> | ||
<strong>{user.name}</strong> | ||
</div> | ||
</Profile> | ||
<Logout onClick={signOut}> | ||
<RiShutDownLine /> | ||
</Logout> | ||
</Container> | ||
) | ||
} |
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,32 +1,32 @@ | ||
import styled from "styled-components"; | ||
|
||
|
||
export const Container = styled.div` | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
background-color: ${({ theme }) => theme.COLORS.BACKGROUND_900} ; | ||
color: ${({ theme }) => theme.COLORS.GRAY_300}; | ||
margin-bottom: 8px; | ||
border-radius: 10px; | ||
> input { | ||
height: 56px; | ||
width: 100%; | ||
padding: 12px; | ||
color: ${({ theme }) => theme.COLORS.WHITE}; | ||
background: transparent; | ||
border: 0; | ||
&:placeholder { | ||
color: ${({ theme }) => theme.COLORS.GRAY_300} | ||
} | ||
} | ||
> svg { | ||
margin-left: 16px; | ||
} | ||
import styled from "styled-components"; | ||
|
||
|
||
export const Container = styled.div` | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
background-color: ${({ theme }) => theme.COLORS.BACKGROUND_900} ; | ||
color: ${({ theme }) => theme.COLORS.GRAY_300}; | ||
margin-bottom: 8px; | ||
border-radius: 10px; | ||
> input { | ||
height: 56px; | ||
width: 100%; | ||
padding: 12px; | ||
color: ${({ theme }) => theme.COLORS.WHITE}; | ||
background: transparent; | ||
border: 0; | ||
&::placeholder { | ||
color: ${({ theme }) => theme.COLORS.GRAY_300} | ||
} | ||
} | ||
> svg { | ||
margin-left: 16px; | ||
} | ||
` |
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,101 @@ | ||
import { createContext,useContext,useState,useEffect } from "react"; | ||
|
||
import { api } from "../services/api"; | ||
|
||
export const AuthContext=createContext({}) | ||
|
||
function AuthProvider({children}){ | ||
const[data,setData]=useState({}) | ||
|
||
async function signIn({email,password}){ | ||
try{ | ||
const response=await api.post("/sessions",{email,password}) | ||
const {user,token}=response.data | ||
|
||
localStorage.setItem("@rocketnotes:user",JSON.stringify(user)); | ||
localStorage.setItem("@rocketnotes:token",token); | ||
|
||
api.defaults.headers.common['Authorization']=`Bearer ${token}`; | ||
|
||
setData({user,token}) | ||
} | ||
catch(error){ | ||
if(error.response){ | ||
alert(error.response.data.message) | ||
}else{ | ||
alert("não foi possivel entrar") | ||
} | ||
} | ||
} | ||
|
||
function signOut(){ | ||
const token =localStorage.removeItem("@rocketnotes:token"); | ||
const user = localStorage.removeItem("@rocketnotes:user"); | ||
|
||
setData({}); | ||
} | ||
|
||
async function updateProfile({user,avatarFile}){ | ||
try{ | ||
if(avatarFile){ | ||
const fileUpdloadForm=new FormData(); | ||
|
||
fileUpdloadForm.append("avatar",avatarFile) | ||
|
||
const response =await api.patch("/users/avatar",fileUpdloadForm) | ||
|
||
user.avatar =response.data.avatar | ||
} | ||
|
||
|
||
await api.put("/users",user); | ||
localStorage.setItem("@rocketnotes:user",JSON.stringify(user)) | ||
|
||
setData({user, token: data.token}) | ||
alert("perfil atualizado") | ||
|
||
} | ||
catch(error){ | ||
if(error.response){ | ||
console.log(data.token) | ||
console.error(error); | ||
alert(error.response.data.message) | ||
}else{ | ||
alert("não foi possivel entrar") | ||
} | ||
} | ||
|
||
} | ||
|
||
useEffect(()=>{ | ||
const token =localStorage.getItem("@rocketnotes:token"); | ||
const user = localStorage.getItem("@rocketnotes:user"); | ||
|
||
if(token && user){ | ||
api.defaults.headers.common['Authorization']=`Bearer ${token}`; | ||
setData({ | ||
token, | ||
user:JSON.parse(user) | ||
}) | ||
} | ||
|
||
},[]) | ||
return( | ||
<AuthContext.Provider value={ | ||
{signIn, | ||
signOut, | ||
updateProfile, | ||
user:data.user, | ||
}}> | ||
{children} | ||
|
||
</AuthContext.Provider> | ||
) | ||
} | ||
|
||
function useAuth(){ | ||
const context= useContext(AuthContext) | ||
return context | ||
} | ||
|
||
export {AuthProvider,useAuth} |
Oops, something went wrong.