1
1
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" ;
3
5
import { User } from "../types" ;
4
6
5
7
interface AuthContextData {
6
8
user : User | null ;
7
- singInUrl : string ;
8
9
singOut : ( ) => void ;
9
10
}
10
11
11
- interface AuthResponse {
12
- token : string ;
13
- user : User
14
- } ;
15
-
16
12
interface AuthProvider {
17
13
children : ReactNode ;
18
14
}
@@ -21,24 +17,8 @@ const AuthContext = createContext({} as AuthContextData);
21
17
22
18
export function AuthProvider ( { children} : AuthProvider ) {
23
19
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
- } ) ;
30
20
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 ( ) {
42
22
const url = window . location . href ;
43
23
const hasGithubCode = url . includes ( '?code=' ) ;
44
24
@@ -47,27 +27,40 @@ export function AuthProvider({children}: AuthProvider) {
47
27
window . history . pushState ( { } , '' , urlWithoutCode ) ;
48
28
singIn ( githubCode ) ;
49
29
}
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
+ }
51
42
52
- useEffect ( ( ) => {
53
- const token = localStorage . getItem ( '@dowhile:token' ) ;
43
+ function loadUserFromLocalStorageWhenExists ( ) {
44
+ const token = localStorage . getItem ( LOCAL_STORAGE_TOKEN_USER_NAME ) ;
54
45
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
+ } ) ;
60
50
}
61
- } , [ ] )
51
+ }
52
+
53
+ function singOut ( ) {
54
+ setUser ( null ) ;
55
+ localStorage . removeItem ( '@dowhile:token' )
56
+ }
57
+
58
+ useEffect ( ( ) => getGithubCodeFromQueryString ( ) , [ ] ) ;
62
59
63
- const providerValue = {
64
- singInUrl : singInUrl ,
65
- singOut : singOut ,
66
- user : user ,
67
- } ;
60
+ useEffect ( ( ) => loadUserFromLocalStorageWhenExists ( ) , [ ] ) ;
68
61
69
62
return (
70
- < AuthContext . Provider value = { providerValue } >
63
+ < AuthContext . Provider value = { { singOut : singOut , user : user , } } >
71
64
{ children }
72
65
</ AuthContext . Provider >
73
66
) ;
0 commit comments