fix: handle access_token only response from switchContext

This commit is contained in:
Usman Baig
2026-01-22 01:36:56 +01:00
parent 806b149bc7
commit cbb2255024
5 changed files with 23 additions and 17 deletions

View File

@@ -97,9 +97,11 @@ export async function exchangeAuthCode(code: string, codeVerifier: string, redir
}
}
export async function setSessionAction(accessToken: string, refreshToken: string) {
export async function setSessionAction(accessToken: string, refreshToken?: string) {
try {
console.log('[setSessionAction] Decoding token...')
if (!accessToken) throw new Error('Access token is missing')
const payloadPart = accessToken.split('.')[1]
const payload: UserPayload = JSON.parse(Buffer.from(payloadPart, 'base64').toString())
@@ -119,14 +121,17 @@ export async function setSessionAction(accessToken: string, refreshToken: string
maxAge: 60 * 15
})
cookieStore.set('refresh_token', refreshToken, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
domain: cookieDomain,
maxAge: 60 * 60 * 24 * 30
})
// * Only update refresh token if provided
if (refreshToken) {
cookieStore.set('refresh_token', refreshToken, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
domain: cookieDomain,
maxAge: 60 * 60 * 24 * 30
})
}
console.log('[setSessionAction] Cookies set successfully')

View File

@@ -25,8 +25,8 @@ export default function LayoutContent({ children }: { children: React.ReactNode
const handleSwitchWorkspace = async (orgId: string) => {
try {
const { token, refresh_token } = await switchContext(orgId)
await setSessionAction(token, refresh_token)
const { access_token } = await switchContext(orgId)
await setSessionAction(access_token)
window.location.reload()
} catch (err) {
console.error('Failed to switch workspace', err)

View File

@@ -28,10 +28,11 @@ export default function WorkspaceSwitcher({ orgs, activeOrgId }: { orgs: Organiz
return
}
const { token, refresh_token } = await switchContext(orgId)
const { access_token } = await switchContext(orgId)
// * Update session cookie via server action
await setSessionAction(token, refresh_token)
// * Note: switchContext only returns access_token, we keep existing refresh token
await setSessionAction(access_token)
// Force reload to pick up new permissions
window.location.reload()

View File

@@ -37,9 +37,9 @@ export async function createOrganization(name: string, slug: string): Promise<Or
}
// * Switch context to organization (returns new token)
export async function switchContext(organizationId: string): Promise<{ token: string, refresh_token: string }> {
export async function switchContext(organizationId: string): Promise<{ access_token: string }> {
// * Route in main.go is /api/v1/auth/switch-context
return apiRequest<{ token: string, refresh_token: string }>('/auth/switch-context', {
return apiRequest<{ access_token: string }>('/auth/switch-context', {
method: 'POST',
body: JSON.stringify({ organization_id: organizationId }),
})

View File

@@ -123,10 +123,10 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
console.log('Auto-switching to organization:', firstOrg.organization_name)
try {
const { token, refresh_token } = await switchContext(firstOrg.organization_id)
const { access_token } = await switchContext(firstOrg.organization_id)
// * Update session cookie
const result = await setSessionAction(token, refresh_token)
const result = await setSessionAction(access_token)
if (result.success && result.user) {
setUser(result.user)
localStorage.setItem('user', JSON.stringify(result.user))