@stytch/react 18.0.0 → 18.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,166 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import React from "react";
3
- import { ReactNode } from "react";
4
- import { Member, MemberSession, Organization, StytchB2BUIClient } from "@stytch/vanilla-js/b2b";
5
- import { StytchB2BHeadlessClient } from "@stytch/vanilla-js/b2b/headless";
6
- import { PermissionsMap } from "@stytch/core/public";
7
3
  import { Callbacks, StyleConfig, StytchB2BUIConfig } from "@stytch/vanilla-js";
8
- /**
9
- * The Stytch Client object passed in to <StytchProvider /> in your application root.
10
- */
11
- type StytchB2BClient = StytchB2BHeadlessClient | StytchB2BUIClient;
12
- type SWRMember = {
13
- /**
14
- * Either the active {@link Member} object, or null if the member is not logged in.
15
- */
16
- member: Member | null;
17
- /**
18
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
19
- */
20
- fromCache: boolean;
21
- };
22
- type SWRMemberSession = {
23
- /**
24
- * Either the active {@link MemberSession} object, or null if the member is not logged in.
25
- */
26
- session: MemberSession | null;
27
- /**
28
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
29
- */
30
- fromCache: boolean;
31
- };
32
- type SWROrganization = {
33
- /**
34
- * Either the active {@link Organization} object, or null if the member is not logged in.
35
- */
36
- organization: Organization | null;
37
- /**
38
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
39
- */
40
- fromCache: boolean;
41
- };
42
- /**
43
- * Returns the active Member.
44
- * Check the fromCache property to determine if the member data is from persistent storage.
45
- * @example
46
- * const {member} = useStytchMember();
47
- * return (<h1>Welcome, {member.name}</h1>);
48
- * @returns A {@link SWRUser}
49
- */
50
- declare const useStytchMember: () => SWRMember;
51
- /**
52
- * Returns the active member's Stytch member session.
53
- * @example
54
- * const { session } = useStytchMemberSession();
55
- * useEffect(() => {
56
- * if (!session) {
57
- * router.replace('/login')
58
- * }
59
- * }, [session]);
60
- * @returns A {@link SWRMemberSession}
61
- */
62
- declare const useStytchMemberSession: () => SWRMemberSession;
63
- /**
64
- * Returns the active Stytch organization.
65
- * @example
66
- * const { organization } = useStytchOrganization();
67
- * return organization ? <p>Welcome to {organization.organization_name}</p> : <p>Log in to continue!</p>;
68
- * @returns A {@link SWROrganization}
69
- */
70
- declare const useStytchOrganization: () => SWROrganization;
71
- type SWRIsAuthorized = {
72
- /**
73
- * Whether the logged-in member is allowed to perform the specified action on the specified resource.
74
- */
75
- isAuthorized: boolean;
76
- /**
77
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
78
- */
79
- fromCache: boolean;
80
- };
81
- /**
82
- * Determines whether the logged-in member is allowed to perform the specified action on the specified resource.
83
- * Returns `true` if the member can perform the action, `false` otherwise.
84
- *
85
- * If the member is not logged in, this method will always return false.
86
- * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.
87
- *
88
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
89
- * @example
90
- * const isAuthorized = useStytchIsAuthorized<Permissions>('documents', 'edit');
91
- * return <button disabled={!isAuthorized}>Edit</button>
92
- */
93
- declare const useStytchIsAuthorized: (resourceId: string, action: string) => SWRIsAuthorized;
94
- /**
95
- * Returns the Stytch B2B client stored in the Stytch context.
96
- *
97
- * @example
98
- * const stytch = useStytchB2BClient();
99
- * useEffect(() => {
100
- * stytch.magicLinks.authenticate('...')
101
- * }, [stytch]);
102
- */
103
- declare const useStytchB2BClient: () => StytchB2BHeadlessClient;
104
- declare const withStytchB2BClient: <T extends object>(Component: React.ComponentType<T & {
105
- stytch: StytchB2BHeadlessClient;
106
- }>) => React.ComponentType<T>;
107
- declare const withStytchMember: <T extends object>(Component: React.ComponentType<T & {
108
- stytchMember: Member | null;
109
- stytchMemberIsFromCache: boolean;
110
- }>) => React.ComponentType<T>;
111
- declare const withStytchMemberSession: <T extends object>(Component: React.ComponentType<T & {
112
- stytchMemberSession: MemberSession | null;
113
- stytchMemberSessionIsFromCache: boolean;
114
- }>) => React.ComponentType<T>;
115
- declare const withStytchOrganization: <T extends object>(Component: React.ComponentType<T & {
116
- stytchOrganization: Organization | null;
117
- stytchOrganizationIsFromCache: boolean;
118
- }>) => React.ComponentType<T>;
119
- /**
120
- * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
121
- * Evaluates all permissions granted to the logged-in member.
122
- * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
123
- * Each boolean will be `true` if the member can perform the action, `false` otherwise.
124
- *
125
- * If the member is not logged in, all values will be false.
126
- *
127
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
128
- * @example
129
- * type Permissions = {
130
- * document: 'create' | 'read' | 'write
131
- * image: 'create' | 'read'
132
- * }
133
- *
134
- * const MyComponent = (props) => {
135
- * const canEditDocuments = props.stytchPermissions.document.edit;
136
- * const canReadImages = props.stytchPermissions.image.read;
137
- * }
138
- * return withStytchPermissions<Permissions>(MyComponent)
139
- */
140
- declare const withStytchPermissions: <Permissions_1 extends Record<string, string>, T extends object>(Component: React.ComponentType<T & {
141
- stytchPermissions: PermissionsMap<Permissions_1>;
142
- }>) => React.ComponentType<T>;
143
- type StytchB2BProviderProps = {
144
- /**
145
- * A Stytch client instance {@link StytchB2BHeadlessClient}
146
- */
147
- stytch: StytchB2BClient;
148
- children?: ReactNode;
149
- };
150
- /**
151
- * The Stytch Context Provider.
152
- * Wrap your application with this component in the root file in order to use Stytch everywhere in your app.
153
- * @example
154
- * const stytch = new StytchB2BHeadlessClient('public-token-<find yours in the stytch dashboard>')
155
- *
156
- * ReactDOM.render(
157
- * <StytchB2BProvider stytch={stytch}>
158
- * <App />
159
- * </StytchProvider>,
160
- * document.getElementById('root'),
161
- * )
162
- */
163
- declare const StytchB2BProvider: ({ stytch, children }: StytchB2BProviderProps) => JSX.Element;
164
4
  interface StytchB2BProps {
165
5
  /**
166
6
  * An optional {@link StyleConfig} to customize the look and feel of the screen.
@@ -263,5 +103,6 @@ interface StytchB2BProps {
263
103
  * @param props {@link StytchB2BProps}
264
104
  */
265
105
  declare const StytchB2B: ({ styles, callbacks, config }: StytchB2BProps) => React.JSX.Element;
266
- export { StytchB2BProvider, useStytchB2BClient, useStytchMemberSession, useStytchMember, useStytchIsAuthorized, useStytchOrganization, withStytchB2BClient, withStytchMemberSession, withStytchMember, withStytchOrganization, withStytchPermissions, StytchB2B };
267
- export type { StytchB2BProviderProps };
106
+ export { StytchB2BProvider, useStytchB2BClient, useStytchMemberSession, useStytchMember, useStytchIsAuthorized, useStytchOrganization, withStytchB2BClient, withStytchMemberSession, withStytchMember, withStytchOrganization, withStytchPermissions } from "../StytchB2BContext-ce9e254c.js";
107
+ export { StytchB2B };
108
+ export type { StytchB2BProviderProps } from "../StytchB2BContext-ce9e254c.js";
@@ -1,166 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import React from "react";
3
- import { ReactNode } from "react";
4
- import { Member, MemberSession, Organization, StytchB2BUIClient } from "@stytch/vanilla-js/b2b";
5
- import { StytchB2BHeadlessClient } from "@stytch/vanilla-js/b2b/headless";
6
- import { PermissionsMap } from "@stytch/core/public";
7
3
  import { Callbacks, StyleConfig, StytchB2BUIConfig } from "@stytch/vanilla-js";
8
- /**
9
- * The Stytch Client object passed in to <StytchProvider /> in your application root.
10
- */
11
- type StytchB2BClient = StytchB2BHeadlessClient | StytchB2BUIClient;
12
- type SWRMember = {
13
- /**
14
- * Either the active {@link Member} object, or null if the member is not logged in.
15
- */
16
- member: Member | null;
17
- /**
18
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
19
- */
20
- fromCache: boolean;
21
- };
22
- type SWRMemberSession = {
23
- /**
24
- * Either the active {@link MemberSession} object, or null if the member is not logged in.
25
- */
26
- session: MemberSession | null;
27
- /**
28
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
29
- */
30
- fromCache: boolean;
31
- };
32
- type SWROrganization = {
33
- /**
34
- * Either the active {@link Organization} object, or null if the member is not logged in.
35
- */
36
- organization: Organization | null;
37
- /**
38
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
39
- */
40
- fromCache: boolean;
41
- };
42
- /**
43
- * Returns the active Member.
44
- * Check the fromCache property to determine if the member data is from persistent storage.
45
- * @example
46
- * const {member} = useStytchMember();
47
- * return (<h1>Welcome, {member.name}</h1>);
48
- * @returns A {@link SWRUser}
49
- */
50
- declare const useStytchMember: () => SWRMember;
51
- /**
52
- * Returns the active member's Stytch member session.
53
- * @example
54
- * const { session } = useStytchMemberSession();
55
- * useEffect(() => {
56
- * if (!session) {
57
- * router.replace('/login')
58
- * }
59
- * }, [session]);
60
- * @returns A {@link SWRMemberSession}
61
- */
62
- declare const useStytchMemberSession: () => SWRMemberSession;
63
- /**
64
- * Returns the active Stytch organization.
65
- * @example
66
- * const { organization } = useStytchOrganization();
67
- * return organization ? <p>Welcome to {organization.organization_name}</p> : <p>Log in to continue!</p>;
68
- * @returns A {@link SWROrganization}
69
- */
70
- declare const useStytchOrganization: () => SWROrganization;
71
- type SWRIsAuthorized = {
72
- /**
73
- * Whether the logged-in member is allowed to perform the specified action on the specified resource.
74
- */
75
- isAuthorized: boolean;
76
- /**
77
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
78
- */
79
- fromCache: boolean;
80
- };
81
- /**
82
- * Determines whether the logged-in member is allowed to perform the specified action on the specified resource.
83
- * Returns `true` if the member can perform the action, `false` otherwise.
84
- *
85
- * If the member is not logged in, this method will always return false.
86
- * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.
87
- *
88
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
89
- * @example
90
- * const isAuthorized = useStytchIsAuthorized<Permissions>('documents', 'edit');
91
- * return <button disabled={!isAuthorized}>Edit</button>
92
- */
93
- declare const useStytchIsAuthorized: (resourceId: string, action: string) => SWRIsAuthorized;
94
- /**
95
- * Returns the Stytch B2B client stored in the Stytch context.
96
- *
97
- * @example
98
- * const stytch = useStytchB2BClient();
99
- * useEffect(() => {
100
- * stytch.magicLinks.authenticate('...')
101
- * }, [stytch]);
102
- */
103
- declare const useStytchB2BClient: () => StytchB2BHeadlessClient;
104
- declare const withStytchB2BClient: <T extends object>(Component: React.ComponentType<T & {
105
- stytch: StytchB2BHeadlessClient;
106
- }>) => React.ComponentType<T>;
107
- declare const withStytchMember: <T extends object>(Component: React.ComponentType<T & {
108
- stytchMember: Member | null;
109
- stytchMemberIsFromCache: boolean;
110
- }>) => React.ComponentType<T>;
111
- declare const withStytchMemberSession: <T extends object>(Component: React.ComponentType<T & {
112
- stytchMemberSession: MemberSession | null;
113
- stytchMemberSessionIsFromCache: boolean;
114
- }>) => React.ComponentType<T>;
115
- declare const withStytchOrganization: <T extends object>(Component: React.ComponentType<T & {
116
- stytchOrganization: Organization | null;
117
- stytchOrganizationIsFromCache: boolean;
118
- }>) => React.ComponentType<T>;
119
- /**
120
- * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
121
- * Evaluates all permissions granted to the logged-in member.
122
- * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
123
- * Each boolean will be `true` if the member can perform the action, `false` otherwise.
124
- *
125
- * If the member is not logged in, all values will be false.
126
- *
127
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
128
- * @example
129
- * type Permissions = {
130
- * document: 'create' | 'read' | 'write
131
- * image: 'create' | 'read'
132
- * }
133
- *
134
- * const MyComponent = (props) => {
135
- * const canEditDocuments = props.stytchPermissions.document.edit;
136
- * const canReadImages = props.stytchPermissions.image.read;
137
- * }
138
- * return withStytchPermissions<Permissions>(MyComponent)
139
- */
140
- declare const withStytchPermissions: <Permissions_1 extends Record<string, string>, T extends object>(Component: React.ComponentType<T & {
141
- stytchPermissions: PermissionsMap<Permissions_1>;
142
- }>) => React.ComponentType<T>;
143
- type StytchB2BProviderProps = {
144
- /**
145
- * A Stytch client instance {@link StytchB2BHeadlessClient}
146
- */
147
- stytch: StytchB2BClient;
148
- children?: ReactNode;
149
- };
150
- /**
151
- * The Stytch Context Provider.
152
- * Wrap your application with this component in the root file in order to use Stytch everywhere in your app.
153
- * @example
154
- * const stytch = new StytchB2BHeadlessClient('public-token-<find yours in the stytch dashboard>')
155
- *
156
- * ReactDOM.render(
157
- * <StytchB2BProvider stytch={stytch}>
158
- * <App />
159
- * </StytchProvider>,
160
- * document.getElementById('root'),
161
- * )
162
- */
163
- declare const StytchB2BProvider: ({ stytch, children }: StytchB2BProviderProps) => JSX.Element;
164
4
  interface StytchB2BProps {
165
5
  /**
166
6
  * An optional {@link StyleConfig} to customize the look and feel of the screen.
@@ -263,5 +103,6 @@ interface StytchB2BProps {
263
103
  * @param props {@link StytchB2BProps}
264
104
  */
265
105
  declare const StytchB2B: ({ styles, callbacks, config }: StytchB2BProps) => React.JSX.Element;
266
- export { StytchB2BProvider, useStytchB2BClient, useStytchMemberSession, useStytchMember, useStytchIsAuthorized, useStytchOrganization, withStytchB2BClient, withStytchMemberSession, withStytchMember, withStytchOrganization, withStytchPermissions, StytchB2B };
267
- export type { StytchB2BProviderProps };
106
+ export { StytchB2BProvider, useStytchB2BClient, useStytchMemberSession, useStytchMember, useStytchIsAuthorized, useStytchOrganization, withStytchB2BClient, withStytchMemberSession, withStytchMember, withStytchOrganization, withStytchPermissions } from "../StytchB2BContext-8acafb28.js";
107
+ export { StytchB2B };
108
+ export type { StytchB2BProviderProps } from "../StytchB2BContext-8acafb28.js";
@@ -1,301 +1,7 @@
1
- import React, { useRef, useState, useEffect, useCallback, createContext, useContext, useMemo, useLayoutEffect } from 'react';
2
-
3
- const createDeepEqual = ({ KEYS_TO_EXCLUDE = [] } = {}) => {
4
- // If comparing functions, this may need some work. Not sure the
5
- // best path for this: compare instance (what it currently does),
6
- // stringify and compare, etc.
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- const deepEqual = (a, b) => {
9
- // Ensures type is the same
10
- if (typeof a !== typeof b)
11
- return false;
12
- // arrays, null, and objects all have type 'object'
13
- if (a === null || b === null)
14
- return a === b;
15
- if (typeof a === 'object') {
16
- if (Object.keys(a).length !== Object.keys(b).length || Object.keys(a).some((k) => !(k in b)))
17
- return false;
18
- return Object.entries(a)
19
- .filter(([k]) => !KEYS_TO_EXCLUDE.includes(k))
20
- .every(([k, v]) => deepEqual(v, b[k]));
21
- }
22
- // boolean, string, number, undefined
23
- return a === b;
24
- };
25
- return deepEqual;
26
- };
27
-
28
- const deepEqual = createDeepEqual();
29
- /**
30
- * Returns a version of `newValue` whose properties that are deeply equal to
31
- * those in `oldValue` are replaced with those from `oldValue`. This provides a
32
- * limited form of "structural sharing" that provides a stable reference for
33
- * unchanged slices of the object.
34
- *
35
- * If `oldValue` and `newValue` are referentially equal, the same value is
36
- * returned.
37
- *
38
- * @param oldValue The old value
39
- * @param newValue The new value
40
- */
41
- const mergeWithStableProps = (oldValue, newValue) => {
42
- // If the values are already referentially the same, just return the new value
43
- if (oldValue === newValue) {
44
- return newValue;
45
- }
46
- return Object.keys(oldValue).reduce((acc, key) => {
47
- if (key in newValue && deepEqual(oldValue[key], newValue[key])) {
48
- acc[key] = oldValue[key];
49
- }
50
- return acc;
51
- }, Object.assign({}, newValue));
52
- };
53
-
54
- const noProviderError = (item, provider = 'StytchProvider') => `${item} can only be used inside <${provider}>.`;
55
- const B2BProviderMustBeUniqueError = 'You cannot render a <StytchB2BProvider> inside another <StytchB2BProvider>.';
56
- const noSSRError = `The @stytch/react library is not meant for use with serverside environments like NextJS.
57
- Use the @stytch/nextjs library instead -
58
- npm remove @stytch/react && npm install @stytch/nextjs
59
- `;
60
- const noHeadlessClientError = `Tried to create a Stytch Login UI element using the Stytch Headless SDK.
61
- You must use the UI SDK to use UI elements.
62
- Please make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.`;
63
-
64
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
- function invariant(cond, message) {
66
- if (!cond)
67
- throw new Error(message);
68
- }
69
-
70
- // useState can cause memory leaks if it is set after the component unmounted. For example, if it is
71
- // set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
72
- const useAsyncState = (initialState) => {
73
- const isMounted = useRef(true);
74
- const [state, setState] = useState(initialState);
75
- useEffect(() => {
76
- isMounted.current = true;
77
- return () => {
78
- isMounted.current = false;
79
- };
80
- }, []);
81
- const setStateAction = useCallback((newState) => {
82
- isMounted.current && setState(newState);
83
- }, []);
84
- return [state, setStateAction];
85
- };
86
-
87
- const initialMember = {
88
- member: null,
89
- fromCache: false,
90
- };
91
- const initialMemberSession = {
92
- session: null,
93
- fromCache: false,
94
- };
95
- const initialOrganization = {
96
- organization: null,
97
- fromCache: false,
98
- };
99
- const StytchB2BContext = createContext({ isMounted: false });
100
- const StytchMemberContext = createContext(initialMember);
101
- const StytchMemberSessionContext = createContext(initialMemberSession);
102
- const StytchOrganizationContext = createContext(initialOrganization);
103
- const useIsMounted__INTERNAL = () => useContext(StytchB2BContext).isMounted;
104
- const isUIClient = (client) => {
105
- return client.mount !== undefined;
106
- };
107
- /**
108
- * Returns the active Member.
109
- * Check the fromCache property to determine if the member data is from persistent storage.
110
- * @example
111
- * const {member} = useStytchMember();
112
- * return (<h1>Welcome, {member.name}</h1>);
113
- * @returns A {@link SWRUser}
114
- */
115
- const useStytchMember = () => {
116
- invariant(useIsMounted__INTERNAL(), noProviderError('useStytchMember', 'StytchB2BProvider'));
117
- return useContext(StytchMemberContext);
118
- };
119
- /**
120
- * Returns the active member's Stytch member session.
121
- * @example
122
- * const { session } = useStytchMemberSession();
123
- * useEffect(() => {
124
- * if (!session) {
125
- * router.replace('/login')
126
- * }
127
- * }, [session]);
128
- * @returns A {@link SWRMemberSession}
129
- */
130
- const useStytchMemberSession = () => {
131
- invariant(useIsMounted__INTERNAL(), noProviderError('useStytchMemberSession', 'StytchB2BProvider'));
132
- return useContext(StytchMemberSessionContext);
133
- };
134
- /**
135
- * Returns the active Stytch organization.
136
- * @example
137
- * const { organization } = useStytchOrganization();
138
- * return organization ? <p>Welcome to {organization.organization_name}</p> : <p>Log in to continue!</p>;
139
- * @returns A {@link SWROrganization}
140
- */
141
- const useStytchOrganization = () => {
142
- invariant(useIsMounted__INTERNAL(), noProviderError('useStytchOrganization', 'StytchB2BProvider'));
143
- return useContext(StytchOrganizationContext);
144
- };
145
- /**
146
- * Determines whether the logged-in member is allowed to perform the specified action on the specified resource.
147
- * Returns `true` if the member can perform the action, `false` otherwise.
148
- *
149
- * If the member is not logged in, this method will always return false.
150
- * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.
151
- *
152
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
153
- * @example
154
- * const isAuthorized = useStytchIsAuthorized<Permissions>('documents', 'edit');
155
- * return <button disabled={!isAuthorized}>Edit</button>
156
- */
157
- const useStytchIsAuthorized = (resourceId, action) => {
158
- invariant(useIsMounted__INTERNAL(), noProviderError('useStytchIsAuthorized', 'StytchB2BProvider'));
159
- const client = useStytchB2BClient();
160
- const { session } = useStytchMemberSession();
161
- const [isAuthorized, setIsAuthorized] = useAsyncState({
162
- fromCache: true,
163
- isAuthorized: client.rbac.isAuthorizedSync(resourceId, action),
164
- });
165
- useEffect(() => {
166
- client.rbac.isAuthorized(resourceId, action).then((isAuthorized) => setIsAuthorized({
167
- fromCache: false,
168
- isAuthorized,
169
- }));
170
- }, [client, session === null || session === void 0 ? void 0 : session.roles, resourceId, action, setIsAuthorized]);
171
- return isAuthorized;
172
- };
173
- /**
174
- * Returns the Stytch B2B client stored in the Stytch context.
175
- *
176
- * @example
177
- * const stytch = useStytchB2BClient();
178
- * useEffect(() => {
179
- * stytch.magicLinks.authenticate('...')
180
- * }, [stytch]);
181
- */
182
- const useStytchB2BClient = () => {
183
- const ctx = useContext(StytchB2BContext);
184
- invariant(ctx.isMounted, noProviderError('useStytchB2BClient', 'StytchB2BProvider'));
185
- return ctx.client;
186
- };
187
- const withStytchB2BClient = (Component) => {
188
- const WithStytch = (props) => {
189
- invariant(useIsMounted__INTERNAL(), noProviderError('withStytchB2BClient', 'StytchB2BProvider'));
190
- return React.createElement(Component, Object.assign({}, props, { stytch: useStytchB2BClient() }));
191
- };
192
- WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
193
- return WithStytch;
194
- };
195
- const withStytchMember = (Component) => {
196
- const WithStytchUser = (props) => {
197
- invariant(useIsMounted__INTERNAL(), noProviderError('withStytchMember', 'StytchB2BProvider'));
198
- const { member, fromCache } = useStytchMember();
199
- return React.createElement(Component, Object.assign({}, props, { stytchMember: member, stytchMemberIsFromCache: fromCache }));
200
- };
201
- WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
202
- return WithStytchUser;
203
- };
204
- const withStytchMemberSession = (Component) => {
205
- const WithStytchSession = (props) => {
206
- invariant(useIsMounted__INTERNAL(), noProviderError('withStytchMemberSession', 'StytchB2BProvider'));
207
- const { session, fromCache } = useStytchMemberSession();
208
- return React.createElement(Component, Object.assign({}, props, { stytchMemberSession: session, stytchMemberSessionIsFromCache: fromCache }));
209
- };
210
- WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
211
- return WithStytchSession;
212
- };
213
- const withStytchOrganization = (Component) => {
214
- const WithStytchOrganization = (props) => {
215
- invariant(useIsMounted__INTERNAL(), noProviderError('withStytchOrganization', 'StytchB2BProvider'));
216
- const { organization, fromCache } = useStytchOrganization();
217
- return React.createElement(Component, Object.assign({}, props, { stytchOrganization: organization, stytchOrganizationIsFromCache: fromCache }));
218
- };
219
- WithStytchOrganization.displayName = `withStytchOrganization(${Component.displayName || Component.name || 'Component'})`;
220
- return WithStytchOrganization;
221
- };
222
- /**
223
- * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
224
- * Evaluates all permissions granted to the logged-in member.
225
- * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
226
- * Each boolean will be `true` if the member can perform the action, `false` otherwise.
227
- *
228
- * If the member is not logged in, all values will be false.
229
- *
230
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
231
- * @example
232
- * type Permissions = {
233
- * document: 'create' | 'read' | 'write
234
- * image: 'create' | 'read'
235
- * }
236
- *
237
- * const MyComponent = (props) => {
238
- * const canEditDocuments = props.stytchPermissions.document.edit;
239
- * const canReadImages = props.stytchPermissions.image.read;
240
- * }
241
- * return withStytchPermissions<Permissions>(MyComponent)
242
- */
243
- const withStytchPermissions = (Component) => {
244
- const WithStytchPermissions = (props) => {
245
- invariant(useIsMounted__INTERNAL(), noProviderError('useRBACPermissions', 'StytchB2BProvider'));
246
- const client = useStytchB2BClient();
247
- const { session } = useStytchMemberSession();
248
- const [permissions, setPermissions] = useAsyncState({ loaded: false, value: null });
249
- useEffect(() => {
250
- client.rbac
251
- .allPermissions()
252
- .then((permissions) => setPermissions({ loaded: true, value: permissions }));
253
- }, [client, session === null || session === void 0 ? void 0 : session.roles, setPermissions]);
254
- if (!permissions.loaded) {
255
- return null;
256
- }
257
- return React.createElement(Component, Object.assign({}, props, { stytchPermissions: permissions.value }));
258
- };
259
- WithStytchPermissions.displayName = `withStytchPermissions(${Component.displayName || Component.name || 'Component'})`;
260
- return WithStytchPermissions;
261
- };
262
- /**
263
- * The Stytch Context Provider.
264
- * Wrap your application with this component in the root file in order to use Stytch everywhere in your app.
265
- * @example
266
- * const stytch = new StytchB2BHeadlessClient('public-token-<find yours in the stytch dashboard>')
267
- *
268
- * ReactDOM.render(
269
- * <StytchB2BProvider stytch={stytch}>
270
- * <App />
271
- * </StytchProvider>,
272
- * document.getElementById('root'),
273
- * )
274
- */
275
- const StytchB2BProvider = ({ stytch, children }) => {
276
- invariant(!useIsMounted__INTERNAL(), B2BProviderMustBeUniqueError);
277
- invariant(typeof window !== 'undefined', noSSRError);
278
- const ctx = useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);
279
- const [{ member, session, organization }, setClientState] = useAsyncState({
280
- session: stytch.session.getInfo(),
281
- member: stytch.self.getInfo(),
282
- organization: stytch.organization.getInfo(),
283
- });
284
- useEffect(() => stytch.onStateChange(() => {
285
- setClientState((oldState) => {
286
- const newState = {
287
- session: stytch.session.getInfo(),
288
- member: stytch.self.getInfo(),
289
- organization: stytch.organization.getInfo(),
290
- };
291
- return mergeWithStableProps(oldState, newState);
292
- });
293
- }), [setClientState, stytch]);
294
- return (React.createElement(StytchB2BContext.Provider, { value: ctx },
295
- React.createElement(StytchOrganizationContext.Provider, { value: organization },
296
- React.createElement(StytchMemberContext.Provider, { value: member },
297
- React.createElement(StytchMemberSessionContext.Provider, { value: session }, children)))));
298
- };
1
+ import { u as useIsMounted__INTERNAL, a as useStytchB2BClient, i as isUIClient } from '../StytchB2BContext-8acafb28.js';
2
+ export { S as StytchB2BProvider, a as useStytchB2BClient, d as useStytchIsAuthorized, c as useStytchMember, b as useStytchMemberSession, e as useStytchOrganization, w as withStytchB2BClient, g as withStytchMember, f as withStytchMemberSession, h as withStytchOrganization, j as withStytchPermissions } from '../StytchB2BContext-8acafb28.js';
3
+ import React, { useRef, useLayoutEffect } from 'react';
4
+ import { i as invariant, b as noHeadlessClientError, n as noProviderError } from '../invariant-568a7633.js';
299
5
 
300
6
  /**
301
7
  * The Stytch B2B UI component.
@@ -357,4 +63,4 @@ const StytchB2B = ({ styles, callbacks, config }) => {
357
63
  return React.createElement("div", { ref: containerEl });
358
64
  };
359
65
 
360
- export { StytchB2B, StytchB2BProvider, useStytchB2BClient, useStytchIsAuthorized, useStytchMember, useStytchMemberSession, useStytchOrganization, withStytchB2BClient, withStytchMember, withStytchMemberSession, withStytchOrganization, withStytchPermissions };
66
+ export { StytchB2B };