fix: handle access_token only response from switchContext
This commit is contained in:
@@ -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 {
|
try {
|
||||||
console.log('[setSessionAction] Decoding token...')
|
console.log('[setSessionAction] Decoding token...')
|
||||||
|
if (!accessToken) throw new Error('Access token is missing')
|
||||||
|
|
||||||
const payloadPart = accessToken.split('.')[1]
|
const payloadPart = accessToken.split('.')[1]
|
||||||
const payload: UserPayload = JSON.parse(Buffer.from(payloadPart, 'base64').toString())
|
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
|
maxAge: 60 * 15
|
||||||
})
|
})
|
||||||
|
|
||||||
cookieStore.set('refresh_token', refreshToken, {
|
// * Only update refresh token if provided
|
||||||
httpOnly: true,
|
if (refreshToken) {
|
||||||
secure: process.env.NODE_ENV === 'production',
|
cookieStore.set('refresh_token', refreshToken, {
|
||||||
sameSite: 'lax',
|
httpOnly: true,
|
||||||
path: '/',
|
secure: process.env.NODE_ENV === 'production',
|
||||||
domain: cookieDomain,
|
sameSite: 'lax',
|
||||||
maxAge: 60 * 60 * 24 * 30
|
path: '/',
|
||||||
})
|
domain: cookieDomain,
|
||||||
|
maxAge: 60 * 60 * 24 * 30
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
console.log('[setSessionAction] Cookies set successfully')
|
console.log('[setSessionAction] Cookies set successfully')
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ export default function LayoutContent({ children }: { children: React.ReactNode
|
|||||||
|
|
||||||
const handleSwitchWorkspace = async (orgId: string) => {
|
const handleSwitchWorkspace = async (orgId: string) => {
|
||||||
try {
|
try {
|
||||||
const { token, refresh_token } = await switchContext(orgId)
|
const { access_token } = await switchContext(orgId)
|
||||||
await setSessionAction(token, refresh_token)
|
await setSessionAction(access_token)
|
||||||
window.location.reload()
|
window.location.reload()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to switch workspace', err)
|
console.error('Failed to switch workspace', err)
|
||||||
|
|||||||
@@ -28,10 +28,11 @@ export default function WorkspaceSwitcher({ orgs, activeOrgId }: { orgs: Organiz
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { token, refresh_token } = await switchContext(orgId)
|
const { access_token } = await switchContext(orgId)
|
||||||
|
|
||||||
// * Update session cookie via server action
|
// * 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
|
// Force reload to pick up new permissions
|
||||||
window.location.reload()
|
window.location.reload()
|
||||||
|
|||||||
@@ -37,9 +37,9 @@ export async function createOrganization(name: string, slug: string): Promise<Or
|
|||||||
}
|
}
|
||||||
|
|
||||||
// * Switch context to organization (returns new token)
|
// * 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
|
// * 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',
|
method: 'POST',
|
||||||
body: JSON.stringify({ organization_id: organizationId }),
|
body: JSON.stringify({ organization_id: organizationId }),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -123,10 +123,10 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
console.log('Auto-switching to organization:', firstOrg.organization_name)
|
console.log('Auto-switching to organization:', firstOrg.organization_name)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { token, refresh_token } = await switchContext(firstOrg.organization_id)
|
const { access_token } = await switchContext(firstOrg.organization_id)
|
||||||
|
|
||||||
// * Update session cookie
|
// * Update session cookie
|
||||||
const result = await setSessionAction(token, refresh_token)
|
const result = await setSessionAction(access_token)
|
||||||
if (result.success && result.user) {
|
if (result.success && result.user) {
|
||||||
setUser(result.user)
|
setUser(result.user)
|
||||||
localStorage.setItem('user', JSON.stringify(result.user))
|
localStorage.setItem('user', JSON.stringify(result.user))
|
||||||
|
|||||||
Reference in New Issue
Block a user