@pentops/j5-react-realm-auth
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -128,2 +128,8 @@ import React from 'react'; | ||
} | ||
function defaultContextChangeActiveAccessSelector(context, activeAccess, realmAccessIdGetter = (defaultRealmAccessIdGetter)) { | ||
if (context && activeAccess) { | ||
return context.accesses.find((ctxAccess) => realmAccessIdGetter(ctxAccess) === realmAccessIdGetter(activeAccess)); | ||
} | ||
return context === null || context === void 0 ? void 0 : context.accesses[0]; | ||
} | ||
const defaultInitialState = { | ||
@@ -136,7 +142,12 @@ activeAccess: undefined, | ||
function createRealmAuthStore(initialState, realmAccessIdGetter = (defaultRealmAccessIdGetter)) { | ||
return create((set, get) => (Object.assign(Object.assign(Object.assign({}, defaultInitialState), { setContext: (cb, defaultActiveAccessSelector, isAuthenticating = false) => { | ||
const { context: prevContext } = get(); | ||
return create((set, get) => (Object.assign(Object.assign(Object.assign({}, defaultInitialState), { setContext: (cb, contextChangeActiveAccessSelector = (defaultContextChangeActiveAccessSelector), isAuthenticating = false) => { | ||
const { context: prevContext, activeAccess } = get(); | ||
const result = cb(prevContext); | ||
const isAuthenticated = Boolean(result); | ||
set({ context: result, isAuthenticating, isAuthenticated, activeAccess: result ? defaultActiveAccessSelector === null || defaultActiveAccessSelector === void 0 ? void 0 : defaultActiveAccessSelector(result) : undefined }); | ||
set({ | ||
context: result, | ||
isAuthenticating, | ||
isAuthenticated, | ||
activeAccess: result ? contextChangeActiveAccessSelector === null || contextChangeActiveAccessSelector === void 0 ? void 0 : contextChangeActiveAccessSelector(result, activeAccess, realmAccessIdGetter) : undefined, | ||
}); | ||
return result; | ||
@@ -184,2 +195,2 @@ }, setActiveAccess: (realmAccessId) => { | ||
export { J5RealmV1RealmStatus, J5RealmV1TenantStatus, createRealmAuthStore, createRealmAuthStoreHooks, defaultRealmAccessIdGetter, selectActiveAccess, selectContext, selectIsAuthenticated, selectIsAuthenticating, selectReset, selectSetActiveAccess, selectSetContext }; | ||
export { J5RealmV1RealmStatus, J5RealmV1TenantStatus, createRealmAuthStore, createRealmAuthStoreHooks, defaultContextChangeActiveAccessSelector, defaultInitialState, defaultRealmAccessIdGetter, selectActiveAccess, selectContext, selectIsAuthenticated, selectIsAuthenticating, selectReset, selectSetActiveAccess, selectSetContext }; |
@@ -9,3 +9,3 @@ import type { RealmAuthStore } from './store'; | ||
export declare function selectSetActiveAccess<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>>(state: RealmAuthStore<TRealmAccess, TContext>): (realmAccessId: string) => TRealmAccess | undefined; | ||
export declare function selectSetContext<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>>(state: RealmAuthStore<TRealmAccess, TContext>): (cb: (context: TContext | undefined) => TContext | undefined, defaultActiveAccessSelector?: import("./store").DefaultActiveAccessSelector<TRealmAccess, TContext> | undefined, isAuthenticating?: boolean) => TContext | undefined; | ||
export declare function selectSetContext<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>>(state: RealmAuthStore<TRealmAccess, TContext>): (cb: (context: TContext | undefined) => TContext | undefined, defaultActiveAccessSelector?: import("./store").ContextChangeActiveAccessSelector<TRealmAccess, TContext> | undefined, isAuthenticating?: boolean) => TContext | undefined; | ||
//# sourceMappingURL=selectors.d.ts.map |
import { type RealmAccess, type WhoAmIContext } from './types'; | ||
export type RealmAccessIdGetter<TRealmAccess extends RealmAccess = RealmAccess> = (realmAccess: TRealmAccess) => string; | ||
export declare function defaultRealmAccessIdGetter<TRealmAccess extends RealmAccess = RealmAccess>(realmAccess: TRealmAccess): string; | ||
export type DefaultActiveAccessSelector<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>> = (context: TContext) => TRealmAccess | undefined; | ||
export type ContextChangeActiveAccessSelector<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>> = (context: TContext | undefined, activeAccess: TRealmAccess | undefined, realmAccessIdGetter: RealmAccessIdGetter<TRealmAccess>) => TRealmAccess | undefined; | ||
export declare function defaultContextChangeActiveAccessSelector<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>>(context: TContext | undefined, activeAccess: TRealmAccess | undefined, realmAccessIdGetter?: RealmAccessIdGetter<TRealmAccess>): TRealmAccess | undefined; | ||
export interface RealmAuthStore<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>> { | ||
@@ -10,17 +11,23 @@ activeAccess: TRealmAccess | undefined; | ||
isAuthenticated: boolean; | ||
setContext: (cb: (context: TContext | undefined) => TContext | undefined, defaultActiveAccessSelector?: DefaultActiveAccessSelector<TRealmAccess, TContext>, isAuthenticating?: boolean) => TContext | undefined; | ||
setContext: (cb: (context: TContext | undefined) => TContext | undefined, defaultActiveAccessSelector?: ContextChangeActiveAccessSelector<TRealmAccess, TContext>, isAuthenticating?: boolean) => TContext | undefined; | ||
setActiveAccess: (realmAccessId: string) => TRealmAccess | undefined; | ||
reset(): void; | ||
} | ||
export declare const defaultInitialState: { | ||
readonly activeAccess: undefined; | ||
readonly context: undefined; | ||
readonly isAuthenticating: true; | ||
readonly isAuthenticated: false; | ||
}; | ||
export declare function createRealmAuthStore<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>>(initialState?: Partial<RealmAuthStore<TRealmAccess, TContext>>, realmAccessIdGetter?: RealmAccessIdGetter<TRealmAccess>): import("zustand").UseBoundStore<import("zustand").StoreApi<RealmAuthStore<TRealmAccess, TContext>>>; | ||
export declare function createRealmAuthStoreHooks<TRealmAccess extends RealmAccess = RealmAccess, TContext extends WhoAmIContext<TRealmAccess> = WhoAmIContext<TRealmAccess>>(initialState?: Partial<RealmAuthStore<TRealmAccess, TContext>>, realmAccessIdGetter?: RealmAccessIdGetter<TRealmAccess>): { | ||
useRealmAuthStore: import("zustand").UseBoundStore<import("zustand").StoreApi<RealmAuthStore<TRealmAccess, TContext>>>; | ||
useRealmAuthActiveAccess: () => TRealmAccess | undefined; | ||
useRealmAuthContext: () => TContext | undefined; | ||
useRealmAuthIsAuthenticating: () => boolean; | ||
useRealmAuthIsAuthenticated: () => boolean; | ||
useRealmAuthReset: () => () => void; | ||
useRealmAuthSetActiveAccess: () => (realmAccessId: string) => TRealmAccess | undefined; | ||
useRealmAuthSetContext: () => (cb: (context: TContext | undefined) => TContext | undefined, defaultActiveAccessSelector?: DefaultActiveAccessSelector<TRealmAccess, TContext> | undefined, isAuthenticating?: boolean) => TContext | undefined; | ||
readonly useRealmAuthStore: import("zustand").UseBoundStore<import("zustand").StoreApi<RealmAuthStore<TRealmAccess, TContext>>>; | ||
readonly useRealmAuthActiveAccess: () => TRealmAccess | undefined; | ||
readonly useRealmAuthContext: () => TContext | undefined; | ||
readonly useRealmAuthIsAuthenticating: () => boolean; | ||
readonly useRealmAuthIsAuthenticated: () => boolean; | ||
readonly useRealmAuthReset: () => () => void; | ||
readonly useRealmAuthSetActiveAccess: () => (realmAccessId: string) => TRealmAccess | undefined; | ||
readonly useRealmAuthSetContext: () => (cb: (context: TContext | undefined) => TContext | undefined, defaultActiveAccessSelector?: ContextChangeActiveAccessSelector<TRealmAccess, TContext> | undefined, isAuthenticating?: boolean) => TContext | undefined; | ||
}; | ||
//# sourceMappingURL=store.d.ts.map |
{ | ||
"name": "@pentops/j5-react-realm-auth", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21554
290