Skip to content

Commit a67e9f3

Browse files
fix auth initialization
1 parent 77814bd commit a67e9f3

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

extensions-web/src/jan-provider-web/api.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Handles API requests to Jan backend for models and chat completions
44
*/
55

6-
import { JanAuthService } from '../shared/auth'
6+
import { getSharedAuthService, JanAuthService } from '../shared'
77
import { JanModel, janProviderStore } from './store'
88

99
// JAN_API_BASE is defined in vite.config.ts
@@ -77,7 +77,7 @@ export class JanApiClient {
7777
private authService: JanAuthService
7878

7979
private constructor() {
80-
this.authService = JanAuthService.getInstance()
80+
this.authService = getSharedAuthService()
8181
}
8282

8383
static getInstance(): JanApiClient {
@@ -216,12 +216,9 @@ export class JanApiClient {
216216

217217
async initialize(): Promise<void> {
218218
try {
219-
await this.authService.initialize()
220219
janProviderStore.setAuthenticated(true)
221-
222220
// Fetch initial models
223221
await this.getModels()
224-
225222
console.log('Jan API client initialized successfully')
226223
} catch (error) {
227224
const errorMessage = error instanceof Error ? error.message : 'Failed to initialize API client'

extensions-web/src/mcp-web/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { MCPExtension, MCPTool, MCPToolCallResult } from '@janhq/core'
8-
import { JanAuthService } from '../shared/auth'
8+
import { getSharedAuthService, JanAuthService } from '../shared'
99
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
1010
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
1111
import { JanMCPOAuthProvider } from './oauth-provider'
@@ -30,14 +30,12 @@ export default class MCPExtensionWeb extends MCPExtension {
3030
version?: string
3131
) {
3232
super(url, name, productName, active, description, version)
33-
this.authService = JanAuthService.getInstance()
33+
this.authService = getSharedAuthService()
3434
this.oauthProvider = new JanMCPOAuthProvider(this.authService)
3535
}
3636

3737
async onLoad(): Promise<void> {
3838
try {
39-
// Initialize authentication first
40-
await this.authService.initialize()
4139
// Initialize MCP client with OAuth
4240
await this.initializeMCPClient()
4341
// Then fetch tools

extensions-web/src/shared/auth.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,13 @@ const AUTH_STORAGE_KEY = 'jan_auth_tokens'
2020
const TOKEN_EXPIRY_BUFFER = 60 * 1000 // 1 minute buffer before actual expiry
2121

2222
export 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
}

extensions-web/src/shared/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { getSharedDB } from './db'
2-
export { JanAuthService } from './auth'
2+
export { JanAuthService, getSharedAuthService } from './auth'
33
export type { AuthTokens, AuthResponse } from './auth'

0 commit comments

Comments
 (0)