mirror of
https://github.com/hanzoai/ui.git
synced 2026-11-20 20:07:34 +00:00
- IamAuthService: OIDC/PKCE-based auth, auto-registers when IAM config present - Enhanced types: avatar, organization fields on HanzoUserInfo - AuthServiceConf: IAM fields (iamServerUrl, iamClientId, etc.) - Firebase becomes optional integration, IAM is the default - Fix npm-publish workflow: add id-token permission for provenance Co-authored-by: Hanzo Dev <[email protected]>
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { enableStaticRendering } from 'mobx-react-lite'
|
|
|
|
import type AuthService from './auth-service'
|
|
import type { AuthServiceConf, HanzoUserInfoValue } from '../types'
|
|
import { getActiveProvider, hasAuthProvider, registerAuthProvider } from './provider-registry'
|
|
import { StubAuthService } from './impl/stub-auth-service'
|
|
import { IamAuthService } from './impl/iam-auth-service'
|
|
|
|
enableStaticRendering(typeof window === "undefined")
|
|
|
|
// https://dev.to/ivandotv/mobx-server-side-rendering-with-next-js-4m18
|
|
let instance: AuthService | undefined = undefined
|
|
|
|
const getSingleton = (
|
|
conf: AuthServiceConf,
|
|
serverSideUser: HanzoUserInfoValue | null
|
|
): AuthService => {
|
|
|
|
// Auto-register IAM provider when IAM config is present
|
|
if (conf.iamServerUrl && conf.iamClientId && !hasAuthProvider()) {
|
|
registerAuthProvider('iam', IamAuthService)
|
|
}
|
|
|
|
// Get the registered auth provider, or use stub if none configured
|
|
const AuthServiceClass = getActiveProvider() ?? StubAuthService
|
|
|
|
// For server side rendering always create a new store
|
|
if (typeof window === "undefined") {
|
|
return new AuthServiceClass(conf, serverSideUser)
|
|
}
|
|
|
|
// Client side, create the store only once in the client
|
|
if (!instance) {
|
|
instance = new AuthServiceClass(conf, serverSideUser)
|
|
}
|
|
else if ('setServerSideUser' in instance && typeof instance.setServerSideUser === 'function') {
|
|
instance.setServerSideUser(serverSideUser)
|
|
}
|
|
|
|
return instance
|
|
}
|
|
|
|
export default getSingleton
|