1
+ import state from './state' ;
1
2
import { appendQuery , getQuery } from "./lib/utils" ;
2
- import http from " ./lib/http" ;
3
+ import * as github from ' ./lib/github' ;
3
4
4
5
const STORAGE_TOKEN_KEY = 'STORAGE_TOKEN_KEY' ;
5
6
@@ -11,26 +12,15 @@ class GitComment {
11
12
*/
12
13
config ( options ) {
13
14
const { client_id, client_secret } = options ;
14
- this . client_id = client_id ;
15
- this . client_secret = client_secret ;
15
+ state . client_id = client_id ;
16
+ state . client_secret = client_secret ;
16
17
this . init ( ) ;
17
18
}
18
19
19
20
//#region fields
20
21
21
- client_id = ''
22
-
23
- client_secret = ''
24
-
25
- access_token = ''
26
-
27
- /**
28
- * 是否登陆
29
- *
30
- * @memberof GitHub
31
- */
32
- ifLogin = false
33
-
22
+ state = state
23
+
34
24
//#endregion
35
25
36
26
//#region private methods
@@ -67,16 +57,12 @@ class GitComment {
67
57
* @memberof GitComment
68
58
*/
69
59
_getToken ( code ) {
70
- http . post ( 'https://github.com/login/oauth/access_token' , {
71
- client_id : this . client_id ,
72
- client_secret : this . client_secret ,
73
- code
74
- } , true )
75
- . then ( body => {
76
- let token = getQuery ( body , 'access_token' ) ;
60
+ let replaceUrl = getQuery ( window . location . search , 'state' ) ;
61
+ replaceUrl = decodeURIComponent ( replaceUrl ) ;
62
+
63
+ github . getToken ( state . client_id , state . client_secret , code )
64
+ . then ( token => {
77
65
this . _updateToken ( token ) ;
78
- let replaceUrl = getQuery ( window . location . search , 'state' ) ;
79
- replaceUrl = decodeURIComponent ( replaceUrl ) ;
80
66
window . history . replaceState ( null , null , replaceUrl ) ;
81
67
} )
82
68
. catch ( err => console . log ( err ) ) ;
@@ -90,14 +76,14 @@ class GitComment {
90
76
*/
91
77
_updateToken ( token ) {
92
78
if ( token && token . length ) {
93
- this . ifLogin = true ;
94
- this . access_token = token ;
79
+ state . ifLogin = true ;
80
+ state . access_token = token ;
95
81
}
96
82
else {
97
- this . ifLogin = false ;
98
- this . access_token = '' ;
83
+ state . ifLogin = false ;
84
+ state . access_token = '' ;
99
85
}
100
- window . localStorage . setItem ( STORAGE_TOKEN_KEY , this . access_token ) ;
86
+ window . localStorage . setItem ( STORAGE_TOKEN_KEY , state . access_token ) ;
101
87
}
102
88
103
89
//#endregion
@@ -110,14 +96,11 @@ class GitComment {
110
96
* @memberof GitHub
111
97
*/
112
98
login ( ) {
113
- let url = 'https://github.com/login/oauth/authorize' ;
114
- url = appendQuery ( url , {
115
- client_id : this . client_id ,
116
- redirect_uri : window . location . href ,
117
- scope : 'public_repo' ,
118
- state : window . location . href
119
- } ) ;
120
- window . location . href = url ;
99
+ github . toAuthorize ( state . client_id ) ;
100
+ }
101
+
102
+ getUserInfo ( ) {
103
+ github . getAuthUser ( ) . then ( body => console . log ( body ) ) ;
121
104
}
122
105
123
106
//#endregion
0 commit comments