For certain use cases, you may want a mock adapter to simulate user authentication without involving an actual authentication server.
This approach is useful when building an app where user authentication is a feature but not a requirement. It also proves beneficial for running tests or in Storybook environments.
Vanilla API React API
Copy import { createOidc } from "oidc-spa" ;
import { createMockOidc } from "oidc-spa/mock" ;
import { z } from "zod" ;
const decodedIdTokenSchema = z .object ({
sub : z .string () ,
preferred_username : z .string ()
});
const publicUrl = import . meta . env . BASE_URL ;
const oidc = !import . meta . env . VITE_OIDC_ISSUER
? createMockOidc ({
isUserInitiallyLoggedIn : false ,
publicUrl ,
mockedTokens : {
decodedIdToken : {
sub : "123" ,
preferred_username : "john doe"
} satisfies z . infer < typeof decodedIdTokenSchema>
}
})
: await createOidc ({
issuerUri : import . meta . env . VITE_OIDC_ISSUER ,
clientId : import . meta . env . VITE_OIDC_CLIENT_ID ,
publicUrl
});
Copy import { createReactOidc } from "oidc-spa/react" ;
import { createMockReactOidc } from "oidc-spa/mock/react" ;
import { z } from "zod" ;
const decodedIdTokenSchema = z .object ({
sub : z .string () ,
preferred_username : z .string ()
});
const publicUrl = import . meta . env . BASE_URL ;
export const { OidcProvider , useOidc , prOidc } =
!import . meta . env . VITE_OIDC_ISSUER ?
createMockReactOidc ({
isUserInitiallyLoggedIn : false ,
publicUrl ,
mockedTokens : {
decodedIdToken : {
sub : "123" ,
preferred_username : "john doe"
} satisfies z . infer < typeof decodedIdTokenSchema>
}
}) :
createReactOidc ({
issuerUri : import . meta . env . VITE_OIDC_ISSUER ,
clientId : import . meta . env . VITE_OIDC_CLIENT_ID ,
publicUrl ,
decodedIdTokenSchema
});
Last updated 7 months ago