Skip to content

Commit b8b343b

Browse files
committed
Reset codebuff client on login
1 parent 7a9ab98 commit b8b343b

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

cli/src/hooks/use-auth-query.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
logoutUser as logoutUserUtil,
99
type User,
1010
} from '../utils/auth'
11+
import { resetCodebuffClient } from '../utils/codebuff-client'
1112
import { logger as defaultLogger } from '../utils/logger'
1213

1314
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
@@ -159,6 +160,8 @@ export function useLogoutMutation(deps: UseLogoutMutationDeps = {}) {
159160
return useMutation({
160161
mutationFn: logoutUser,
161162
onSuccess: () => {
163+
// Reset the SDK client after logout
164+
resetCodebuffClient()
162165
// Clear all auth-related cache
163166
queryClient.removeQueries({ queryKey: authQueryKeys.all })
164167
},

cli/src/hooks/use-auth-state.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useCallback, useEffect, useRef, useState } from 'react'
22

33
import { useAuthQuery, useLogoutMutation } from './use-auth-query'
44
import { getUserCredentials } from '../utils/auth'
5-
import { logger } from '../utils/logger'
5+
import { resetCodebuffClient } from '../utils/codebuff-client'
66

77
import type { MultilineInputHandle } from '../components/multiline-input'
88
import type { User } from '../utils/auth'
@@ -63,6 +63,8 @@ export const useAuthState = ({
6363
// Handle successful login
6464
const handleLoginSuccess = useCallback(
6565
(loggedInUser: User) => {
66+
// Reset the SDK client to pick up new credentials
67+
resetCodebuffClient()
6668
resetChatStore()
6769
setInputFocused(true)
6870
setUser(loggedInUser)

cli/src/utils/codebuff-client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import { logger } from './logger'
88

99
let clientInstance: CodebuffClient | null = null
1010

11+
/**
12+
* Reset the cached CodebuffClient instance.
13+
* This should be called after login to ensure the client is re-initialized with new credentials.
14+
*/
15+
export function resetCodebuffClient(): void {
16+
clientInstance = null
17+
}
18+
1119
export function getCodebuffClient(): CodebuffClient | null {
1220
if (!clientInstance) {
1321
const { token: apiKey, source } = getAuthTokenDetails()

0 commit comments

Comments
 (0)