11import axios from 'axios' ;
22
3- import {
4- TOKEN_RIGHT ,
5- TOKEN_WRONG ,
6- TOKEN_RESET ,
7- TOKEN_LOADING
8- } from './type' ;
3+ import { TOKEN_RIGHT , TOKEN_WRONG , TOKEN_RESET , TOKEN_LOADING } from './type' ;
94
10- import { getLocalStorageData , setLocalStorageData , unSetLocalStorageData } from '../utils' ;
5+ import {
6+ getLocalStorageData ,
7+ setLocalStorageData ,
8+ unSetLocalStorageData ,
9+ } from '../utils' ;
1110
1211const GITHUB_LINK = `https://api.github.com` ;
1312const BITLY_LINK = `https://api-ssl.bitly.com/v4` ;
1413
1514export const getLocalAuth = ( ) => {
16- const [ github_token , bitly_token ] = getLocalStorageData ( [ 'github' , 'bitly' ] ) ;
17- return dispatch => {
18- if ( github_token ) {
19- dispatch ( {
20- type : TOKEN_RIGHT ,
21- payload : {
22- name : 'github' ,
23- token : github_token . token
15+ const [ github_token , bitly_token ] = getLocalStorageData ( [
16+ 'github' ,
17+ 'bitly' ,
18+ ] ) ;
19+ return dispatch => {
20+ if ( github_token ) {
21+ dispatch ( {
22+ type : TOKEN_RIGHT ,
23+ payload : {
24+ name : 'github' ,
25+ token : github_token . token ,
26+ } ,
27+ } ) ;
2428 }
25- } ) ;
26- }
27- if ( bitly_token ) {
28- dispatch ( {
29- type : TOKEN_RIGHT ,
30- payload : {
31- name : 'bitly' ,
32- token : {
33- token : bitly_token . token . token ,
34- id : bitly_token . token . id
35- }
29+ if ( bitly_token ) {
30+ dispatch ( {
31+ type : TOKEN_RIGHT ,
32+ payload : {
33+ name : 'bitly' ,
34+ token : {
35+ token : bitly_token . token . token ,
36+ id : bitly_token . token . id ,
37+ } ,
38+ } ,
39+ } ) ;
3640 }
37- } ) ;
38- }
39- }
40- }
41+ } ;
42+ } ;
4143
4244export const verifyGithubToken = token => {
43- return async dispatch => {
44- dispatch ( {
45- type : TOKEN_LOADING ,
46- payload : {
47- name : 'github'
48- }
49- } ) ;
50- try {
51- const { data } = await axios ( `${ GITHUB_LINK } /gists` , {
52- headers : {
53- Authorization : `Bearer ${ token } `
54- }
55- } ) ;
56- if ( data . length ) {
45+ return async dispatch => {
5746 dispatch ( {
58- type : TOKEN_RIGHT ,
59- payload : {
60- name : 'github' ,
61- token
62- }
47+ type : TOKEN_LOADING ,
48+ payload : {
49+ name : 'github' ,
50+ } ,
6351 } ) ;
64- setLocalStorageData ( 'github' , { token } ) ;
65- }
66- } catch ( e ) {
67- dispatch ( {
68- type : TOKEN_WRONG ,
69- payload : {
70- name : 'github' ,
71- error : true
52+ try {
53+ const { data } = await axios ( `${ GITHUB_LINK } /user` , {
54+ headers : {
55+ Authorization : `Bearer ${ token } ` ,
56+ } ,
57+ } ) ;
58+ if ( data . login ) {
59+ dispatch ( {
60+ type : TOKEN_RIGHT ,
61+ payload : {
62+ name : 'github' ,
63+ token,
64+ } ,
65+ } ) ;
66+ setLocalStorageData ( 'github' , { token } ) ;
67+ }
68+ } catch ( e ) {
69+ dispatch ( {
70+ type : TOKEN_WRONG ,
71+ payload : {
72+ name : 'github' ,
73+ error : true ,
74+ } ,
75+ } ) ;
7276 }
73- } ) ;
74- }
75- }
76- }
77+ } ;
78+ } ;
7779
7880export const verifyBitlyToken = token => {
79- return async dispatch => {
80- dispatch ( {
81- type : TOKEN_LOADING ,
82- payload : {
83- name : 'bitly'
84- }
85- } ) ;
86- try {
87- const { data } = await axios ( `${ BITLY_LINK } /user` , {
88- headers : {
89- Authorization : `Bearer ${ token } `
90- }
91- } ) ;
92- if ( data . login ) {
81+ return async dispatch => {
9382 dispatch ( {
94- type : TOKEN_RIGHT ,
95- payload : {
96- name : 'bitly' ,
97- token : {
98- token,
99- id : data . default_group_guid
100- }
101- }
83+ type : TOKEN_LOADING ,
84+ payload : {
85+ name : 'bitly' ,
86+ } ,
10287 } ) ;
103- setLocalStorageData ( 'bitly' , { token : { token, id : data . default_group_guid } } ) ;
104- }
105- } catch ( e ) {
106- dispatch ( {
107- type : TOKEN_WRONG ,
108- payload : {
109- name : 'bitly' ,
110- error : true
88+ try {
89+ const { data } = await axios ( `${ BITLY_LINK } /user` , {
90+ headers : {
91+ Authorization : `Bearer ${ token } ` ,
92+ } ,
93+ } ) ;
94+ if ( data . login ) {
95+ dispatch ( {
96+ type : TOKEN_RIGHT ,
97+ payload : {
98+ name : 'bitly' ,
99+ token : {
100+ token,
101+ id : data . default_group_guid ,
102+ } ,
103+ } ,
104+ } ) ;
105+ setLocalStorageData ( 'bitly' , {
106+ token : { token, id : data . default_group_guid } ,
107+ } ) ;
108+ }
109+ } catch ( e ) {
110+ dispatch ( {
111+ type : TOKEN_WRONG ,
112+ payload : {
113+ name : 'bitly' ,
114+ error : true ,
115+ } ,
116+ } ) ;
111117 }
112- } ) ;
113- }
114- }
115- }
118+ } ;
119+ } ;
116120
117121export const resetTokens = ( ) => {
118- return dispatch => {
119- dispatch ( {
120- type : TOKEN_RESET
121- } ) ;
122- unSetLocalStorageData ( [ 'github' , 'bitly' ] ) ;
123- }
124- }
122+ return dispatch => {
123+ dispatch ( {
124+ type : TOKEN_RESET ,
125+ } ) ;
126+ unSetLocalStorageData ( [ 'github' , 'bitly' ] ) ;
127+ } ;
128+ } ;
0 commit comments