Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed id token decoding not working with mocks #164

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/oidc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ import { createMockReactOidc } from 'oidc-spa/mock/react'
import { createReactOidc } from 'oidc-spa/react'
import { z } from 'zod'

const decodedIdTokenSchema = z.object({
sid: z.string(),
sub: z.string(),
preferred_username: z.string(),
})

export const { OidcProvider, useOidc, getOidc } =
import.meta.env.VITE_OIDC_ENABLED === 'false'
? createMockReactOidc({ isUserInitiallyLoggedIn: false })
? createMockReactOidc({
isUserInitiallyLoggedIn: false,
mockedTokens: {
decodedIdToken: {
sid: `mock-${self.crypto.randomUUID()}`,
sub: 'mock-sub',
preferred_username: 'mock-user',
} satisfies z.infer<typeof decodedIdTokenSchema>,
},
})
: createReactOidc({
clientId: import.meta.env.VITE_OIDC_CLIENT_ID,
issuerUri: import.meta.env.VITE_OIDC_ISSUER,
publicUrl: import.meta.env.BASE_URL,
decodedIdTokenSchema: z.object({
sid: z.string(),
sub: z.string(),
preferred_username: z.string(),
}),
decodedIdTokenSchema,
})
18 changes: 6 additions & 12 deletions src/router/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@ import {
Outlet,
ScrollRestoration,
} from '@tanstack/react-router'
import { decodeJwt } from 'oidc-spa/tools/decodeJwt'
import { memo, useEffect, useState } from 'react'
import { memo, useEffect } from 'react'
import { Toaster } from 'react-hot-toast'

// eslint-disable-next-line react-refresh/only-export-components
const RootComponent = memo(() => {
const { oidcTokens } = useOidc()
const { isTelemetryDisabled, setDefaultValues } = useTelemetry()
const [sid, setSID] = useState<string | undefined>(undefined)

// Retrieve the OIDC's session id (different for each session of the user
// agent used by the end-user which allows to identify distinct sessions)
useEffect(() => {
setSID(oidcTokens ? decodeJwt(oidcTokens?.idToken)?.sid : undefined)
}, [oidcTokens])

useEffect(() => {
if (!isTelemetryDisabled && sid) {
// Retrieve the OIDC's session id (different for each session of the user
// agent used by the end-user which allows to identify distinct sessions)
setDefaultValues({ sid })
if (!isTelemetryDisabled && oidcTokens?.decodedIdToken.sid) {
setDefaultValues({ sid: oidcTokens?.decodedIdToken.sid })
}
}, [isTelemetryDisabled, sid, setDefaultValues])
}, [isTelemetryDisabled, oidcTokens?.decodedIdToken.sid, setDefaultValues])

return (
<div
Expand Down