Skip to content

Commit 7d315f0

Browse files
refac(auth.context): refact auth context
1 parent e1c307c commit 7d315f0

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

src/components/LoginBox/index.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { VscGithubInverted } from 'react-icons/vsc';
2+
import { API_URL_GITHUB_AUTH } from '../../constants';
23
import { useAuthContext } from '../../contexts/auth';
34
import styles from './styles.module.scss';
45

56
export function LoginBox() {
6-
const {singInUrl, user} = useAuthContext();
7+
const {user} = useAuthContext();
78

89
console.log(user);
910
return (
1011
<div className={styles.loginBoxWrapper}>
1112
<strong>Entre e compartilhe sua mensagem</strong>
12-
<a href={singInUrl} className={styles.singInWithGithub}>
13+
<a href={API_URL_GITHUB_AUTH} className={styles.singInWithGithub}>
1314
<VscGithubInverted size="24" />
1415
Entrar com o Github
1516
</a>

src/contexts/auth.tsx

+32-39
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { createContext, ReactNode, useContext, useEffect, useState } from "react";
2-
import { api } from "../services/api";
2+
import { LOCAL_STORAGE_TOKEN_USER_NAME } from "../constants";
3+
import { AuthService } from "../services/AuthService";
4+
import { UserService } from "../services/UserService";
35
import { User } from "../types";
46

57
interface AuthContextData {
68
user: User | null;
7-
singInUrl: string;
89
singOut: () => void;
910
}
1011

11-
interface AuthResponse {
12-
token: string;
13-
user: User
14-
};
15-
1612
interface AuthProvider {
1713
children: ReactNode;
1814
}
@@ -21,24 +17,8 @@ const AuthContext = createContext({} as AuthContextData);
2117

2218
export function AuthProvider({children}: AuthProvider) {
2319
const [user, setUser] = useState<User | null>(null);
24-
const singInUrl = 'http://localhost:4000/github';
25-
26-
async function singIn(githubCode: string) {
27-
const response = await api.post<AuthResponse>('/authenticate', {
28-
code: githubCode,
29-
});
3020

31-
const {token, user} = response.data;
32-
localStorage.setItem('@dowhile:token', token);
33-
setUser(user);
34-
}
35-
36-
function singOut() {
37-
setUser(null);
38-
localStorage.removeItem('@dowhile:token')
39-
}
40-
41-
useEffect(()=>{
21+
function getGithubCodeFromQueryString() {
4222
const url = window.location.href;
4323
const hasGithubCode = url.includes('?code=');
4424

@@ -47,27 +27,40 @@ export function AuthProvider({children}: AuthProvider) {
4727
window.history.pushState({}, '', urlWithoutCode);
4828
singIn(githubCode);
4929
}
50-
}, []);
30+
}
31+
32+
async function singIn(githubCode: string) {
33+
try {
34+
const response = await AuthService.singIn(githubCode);
35+
const {token, user} = response.data;
36+
localStorage.setItem(LOCAL_STORAGE_TOKEN_USER_NAME, token);
37+
setUser(user);
38+
} catch (error) {
39+
console.error(error);
40+
}
41+
}
5142

52-
useEffect(()=>{
53-
const token = localStorage.getItem('@dowhile:token');
43+
function loadUserFromLocalStorageWhenExists() {
44+
const token = localStorage.getItem(LOCAL_STORAGE_TOKEN_USER_NAME);
5445
if (token) {
55-
// all request before this, send authorization in header :)
56-
api.defaults.headers.common.authorization = `Bearer ${token}`;
57-
api.get<User>('/profile').then(response => {
58-
setUser(response.data);
59-
});
46+
UserService.loadProfile(token)
47+
.then(response => {
48+
setUser(response.data);
49+
});
6050
}
61-
},[])
51+
}
52+
53+
function singOut() {
54+
setUser(null);
55+
localStorage.removeItem('@dowhile:token')
56+
}
57+
58+
useEffect(() => getGithubCodeFromQueryString(), []);
6259

63-
const providerValue = {
64-
singInUrl: singInUrl,
65-
singOut: singOut,
66-
user: user,
67-
};
60+
useEffect(() => loadUserFromLocalStorageWhenExists(), []);
6861

6962
return (
70-
<AuthContext.Provider value={providerValue}>
63+
<AuthContext.Provider value={{singOut: singOut, user: user,}}>
7164
{children}
7265
</AuthContext.Provider>
7366
);

0 commit comments

Comments
 (0)