@@ -11,9 +11,11 @@ import {
11
11
Appearance ,
12
12
AuthOptions ,
13
13
AuthState ,
14
+ AuthTokenOptions ,
14
15
SettingsState ,
15
16
} from '../types' ;
16
- import { authGitHub , getToken } from '../utils/auth' ;
17
+ import { apiRequestAuth } from '../utils/api-requests' ;
18
+ import { addAccount , authGitHub , getToken } from '../utils/auth' ;
17
19
import { clearState , loadState , saveState } from '../utils/storage' ;
18
20
import { setAppearance } from '../utils/appearance' ;
19
21
import { setAutoLaunch } from '../utils/comms' ;
@@ -39,6 +41,7 @@ interface AppContextState {
39
41
isLoggedIn : boolean ;
40
42
login : ( ) => void ;
41
43
loginEnterprise : ( data : AuthOptions ) => void ;
44
+ validateToken : ( data : AuthTokenOptions ) => void ;
42
45
logout : ( ) => void ;
43
46
44
47
notifications : AccountNotifications [ ] ;
@@ -110,16 +113,31 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
110
113
const { token } = await getToken ( authCode ) ;
111
114
setAccounts ( { ...accounts , token } ) ;
112
115
saveState ( { ...accounts , token } , settings ) ;
113
- } , [ accounts ] ) ;
116
+ } , [ accounts , settings ] ) ;
114
117
115
118
const loginEnterprise = useCallback (
116
119
async ( data : AuthOptions ) => {
117
120
const { authOptions, authCode } = await authGitHub ( data ) ;
118
- const { token } = await getToken ( authCode , authOptions ) ;
119
- setAccounts ( { ...accounts , token } ) ;
120
- saveState ( { ...accounts , token } , settings ) ;
121
+ const { token, hostname } = await getToken ( authCode , authOptions ) ;
122
+ const updatedAccounts = addAccount ( accounts , token , hostname ) ;
123
+ setAccounts ( updatedAccounts ) ;
124
+ saveState ( updatedAccounts , settings ) ;
125
+ } ,
126
+ [ accounts , settings ]
127
+ ) ;
128
+
129
+ const validateToken = useCallback (
130
+ async ( { token, hostname } : AuthTokenOptions ) => {
131
+ await apiRequestAuth (
132
+ `https://api.${ hostname } /notifications` ,
133
+ 'HEAD' ,
134
+ token
135
+ ) ;
136
+ const updatedAccounts = addAccount ( accounts , token , hostname ) ;
137
+ setAccounts ( updatedAccounts ) ;
138
+ saveState ( updatedAccounts , settings ) ;
121
139
} ,
122
- [ accounts ]
140
+ [ accounts , settings ]
123
141
) ;
124
142
125
143
const logout = useCallback ( ( ) => {
@@ -169,6 +187,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
169
187
isLoggedIn,
170
188
login,
171
189
loginEnterprise,
190
+ validateToken,
172
191
logout,
173
192
174
193
notifications,
0 commit comments