@@ -20,21 +20,13 @@ const AUTH_STORAGE_KEY = 'jan_auth_tokens'
2020const TOKEN_EXPIRY_BUFFER = 60 * 1000 // 1 minute buffer before actual expiry
2121
2222export class JanAuthService {
23- private static instance : JanAuthService
2423 private tokens : AuthTokens | null = null
2524 private tokenExpiryTime : number = 0
2625
27- private constructor ( ) {
26+ constructor ( ) {
2827 this . loadTokensFromStorage ( )
2928 }
3029
31- static getInstance ( ) : JanAuthService {
32- if ( ! JanAuthService . instance ) {
33- JanAuthService . instance = new JanAuthService ( )
34- }
35- return JanAuthService . instance
36- }
37-
3830 private loadTokensFromStorage ( ) : void {
3931 try {
4032 const storedTokens = localStorage . getItem ( AUTH_STORAGE_KEY )
@@ -169,16 +161,6 @@ export class JanAuthService {
169161 return this . tokens . access_token
170162 }
171163
172- async initialize ( ) : Promise < void > {
173- try {
174- await this . getValidAccessToken ( )
175- console . log ( 'Jan auth service initialized successfully' )
176- } catch ( error ) {
177- console . error ( 'Failed to initialize Jan auth service:' , error )
178- throw error
179- }
180- }
181-
182164 async getAuthHeader ( ) : Promise < { Authorization : string } > {
183165 const token = await this . getValidAccessToken ( )
184166 return {
@@ -217,4 +199,21 @@ export class JanAuthService {
217199 logout ( ) : void {
218200 this . clearTokens ( )
219201 }
202+ }
203+
204+ declare global {
205+ interface Window {
206+ janAuthService ?: JanAuthService
207+ }
208+ }
209+
210+ /**
211+ * Gets or creates the shared JanAuthService instance on the window object
212+ * This ensures all extensions use the same auth service instance
213+ */
214+ export function getSharedAuthService ( ) : JanAuthService {
215+ if ( ! window . janAuthService ) {
216+ window . janAuthService = new JanAuthService ( )
217+ }
218+ return window . janAuthService
220219}
0 commit comments