@@ -11,6 +11,7 @@ import { RefreshTimer } from "./RefreshTimer"
1111import { getUserAgent } from "./utils"
1212
1313export interface AuthServiceEvents {
14+ "attempting-session" : [ data : { previousState : AuthState } ]
1415 "inactive-session" : [ data : { previousState : AuthState } ]
1516 "active-session" : [ data : { previousState : AuthState } ]
1617 "logged-out" : [ data : { previousState : AuthState } ]
@@ -26,7 +27,7 @@ type AuthCredentials = z.infer<typeof authCredentialsSchema>
2627
2728const AUTH_STATE_KEY = "clerk-auth-state"
2829
29- type AuthState = "initializing" | "logged-out" | "active-session" | "inactive-session"
30+ type AuthState = "initializing" | "logged-out" | "active-session" | "attempting-session" | " inactive-session"
3031
3132const clerkSignInResponseSchema = z . object ( {
3233 response : z . object ( {
@@ -93,6 +94,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
9394 private credentials : AuthCredentials | null = null
9495 private sessionToken : string | null = null
9596 private userInfo : CloudUserInfo | null = null
97+ private isFirstRefreshAttempt : boolean = false
9698
9799 constructor ( context : vscode . ExtensionContext , log ?: ( ...args : unknown [ ] ) => void ) {
98100 super ( )
@@ -129,7 +131,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
129131 this . credentials . clientToken !== credentials . clientToken ||
130132 this . credentials . sessionId !== credentials . sessionId
131133 ) {
132- this . transitionToInactiveSession ( credentials )
134+ this . transitionToAttemptingSession ( credentials )
133135 }
134136 } else {
135137 if ( this . state !== "logged-out" ) {
@@ -156,19 +158,32 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
156158 this . log ( "[auth] Transitioned to logged-out state" )
157159 }
158160
159- private transitionToInactiveSession ( credentials : AuthCredentials ) : void {
161+ private transitionToAttemptingSession ( credentials : AuthCredentials ) : void {
160162 this . credentials = credentials
161163
162164 const previousState = this . state
163- this . state = "inactive -session"
165+ this . state = "attempting -session"
164166
165167 this . sessionToken = null
166168 this . userInfo = null
169+ this . isFirstRefreshAttempt = true
167170
168- this . emit ( "inactive -session" , { previousState } )
171+ this . emit ( "attempting -session" , { previousState } )
169172
170173 this . timer . start ( )
171174
175+ this . log ( "[auth] Transitioned to attempting-session state" )
176+ }
177+
178+ private transitionToInactiveSession ( ) : void {
179+ const previousState = this . state
180+ this . state = "inactive-session"
181+
182+ this . sessionToken = null
183+ this . userInfo = null
184+
185+ this . emit ( "inactive-session" , { previousState } )
186+
172187 this . log ( "[auth] Transitioned to inactive-session state" )
173188 }
174189
@@ -329,16 +344,27 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
329344 /**
330345 * Check if the user is authenticated
331346 *
332- * @returns True if the user is authenticated (has an active or inactive session)
347+ * @returns True if the user is authenticated (has an active, attempting, or inactive session)
333348 */
334349 public isAuthenticated ( ) : boolean {
335- return this . state === "active-session" || this . state === "inactive-session"
350+ return (
351+ this . state === "active-session" || this . state === "attempting-session" || this . state === "inactive-session"
352+ )
336353 }
337354
338355 public hasActiveSession ( ) : boolean {
339356 return this . state === "active-session"
340357 }
341358
359+ /**
360+ * Check if the user has an active session or is currently attempting to acquire one
361+ *
362+ * @returns True if the user has an active session or is attempting to get one
363+ */
364+ public hasOrIsAcquiringActiveSession ( ) : boolean {
365+ return this . state === "active-session" || this . state === "attempting-session"
366+ }
367+
342368 /**
343369 * Refresh the session
344370 *
@@ -364,6 +390,9 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
364390 if ( error instanceof InvalidClientTokenError ) {
365391 this . log ( "[auth] Invalid/Expired client token: clearing credentials" )
366392 this . clearCredentials ( )
393+ } else if ( this . isFirstRefreshAttempt && this . state === "attempting-session" ) {
394+ this . isFirstRefreshAttempt = false
395+ this . transitionToInactiveSession ( )
367396 }
368397 this . log ( "[auth] Failed to refresh session" , error )
369398 throw error
0 commit comments