@@ -12,7 +12,9 @@ const wait = (milliseconds: number): Promise<void> => {
1212 return new Promise ( resolve => setTimeout ( resolve , milliseconds ) ) ;
1313} ;
1414
15- const TOKEN_FILE = path . resolve ( __dirname , "token.json" ) ;
15+
16+
17+ const TOKEN_FILE = process . env . TOKEN_FILE || path . resolve ( __dirname , "token.json" ) ;
1618
1719export interface AuthState {
1820 deviceCode : string ;
@@ -131,15 +133,12 @@ export async function pollToken() {
131133}
132134
133135export function saveToken ( token : OAuthToken ) {
136+ globalState . auth = true ;
137+ authState . token = token ;
134138 fs . writeFileSync ( TOKEN_FILE , JSON . stringify ( { token } ) ) ;
135139 log ( "info" , "Token saved to file." ) ;
136140}
137141
138- export function getToken ( ) : OAuthToken | undefined {
139- if ( ! authState . token ) { loadToken ( ) ; }
140- return authState . token ;
141- }
142-
143142function loadToken ( ) : { token ?: OAuthToken } | undefined {
144143 if ( fs . existsSync ( TOKEN_FILE ) ) {
145144 try {
@@ -152,6 +151,7 @@ function loadToken(): { token?: OAuthToken } | undefined {
152151 return data ;
153152 }
154153 log ( "info" , "Token file exists but doesn't contain a valid token structure" ) ;
154+
155155 } catch ( error ) {
156156 log ( "error" , `Error parsing token file: ${ error } ` ) ;
157157 }
@@ -165,22 +165,29 @@ export async function isAuthenticated(): Promise<boolean> {
165165 if ( globalState . auth ) {
166166 return true ;
167167 }
168+
169+ const token = await getToken ( ) ;
170+ return globalState . auth ;
171+ }
168172
173+ export async function getToken ( ) : Promise < OAuthToken | null > {
169174 // Try to load token from file if not already loaded
170175 if ( ! authState . token ) {
171176 loadToken ( ) ;
172177 }
173178
174179 if ( ! authState . token ) {
180+ globalState . auth = false ;
175181 log ( "info" , "No token found after loading" ) ;
176- return false ;
182+ return null ;
177183 }
178184
179185 // Validate the existing token
180186 try {
181187 log ( "info" , "Validating token..." ) ;
182188 if ( validateToken ( authState . token ) ) {
183- return true ;
189+ globalState . auth = true ;
190+ return authState . token ;
184191 }
185192
186193 // If the token is invalid, attempt to refresh it
@@ -191,15 +198,15 @@ export async function isAuthenticated(): Promise<boolean> {
191198 globalState . auth = true ;
192199 log ( "info" , "Token refreshed successfully." ) ;
193200 saveToken ( refreshedToken ) ;
194- return true ;
201+ return refreshedToken ;
195202 }
196203 log ( "error" , "Failed to refresh token." ) ;
197204 } catch ( error ) {
198205 log ( "error" , `Error during token validation or refresh: ${ error } ` ) ;
199206 }
200207
201208 globalState . auth = false ;
202- return false ;
209+ return null ;
203210}
204211
205212function validateToken ( tokenData : OAuthToken ) : boolean {
@@ -211,7 +218,7 @@ function validateToken(tokenData: OAuthToken): boolean {
211218
212219 // If expiry is zero value (not set), consider token not expired (like in Go)
213220 if ( ! tokenData . expiry ) {
214- return true ;
221+ return false ;
215222 }
216223
217224 // Match the Go code's expiryDelta concept (10 seconds)
@@ -254,7 +261,12 @@ async function refreshToken(token: string): Promise<OAuthToken | null> {
254261
255262 if ( response . ok ) {
256263 const data = await response . json ( ) as OAuthToken ;
257- return data ;
264+
265+ const buf = Buffer . from ( data . access_token . split ( '.' ) [ 1 ] , 'base64' ) . toString ( )
266+ const jwt = JSON . parse ( buf ) ;
267+ const expiry = new Date ( jwt . exp * 1000 ) ;
268+
269+ return { ...data , expiry} ;
258270 }
259271 } catch ( error ) {
260272 log ( "info" , `Error refreshing token: ${ error } ` ) ;
0 commit comments