@stytch/react 19.4.3 → 19.5.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +7 -7
  3. package/b2b/headless/package.json +6 -0
  4. package/b2b/ui/package.json +6 -0
  5. package/dist/StytchB2BContext-9c062712.d.ts +272 -0
  6. package/dist/{StytchB2BContext-081081ee.js → StytchB2BContext-9c062712.js} +94 -49
  7. package/dist/StytchB2BContext-c5062f9b.d.ts +272 -0
  8. package/dist/StytchB2BContext-c5062f9b.js +280 -0
  9. package/dist/StytchSSRProxy-34c789b5.d.ts +3 -0
  10. package/dist/StytchSSRProxy-34c789b5.js +48 -0
  11. package/dist/StytchSSRProxy-86bc42b3.d.ts +3 -0
  12. package/dist/StytchSSRProxy-86bc42b3.js +54 -0
  13. package/dist/adminPortal/index.d.ts +1 -1
  14. package/dist/adminPortal/index.esm.d.ts +1 -1
  15. package/dist/adminPortal/index.esm.js +5 -4
  16. package/dist/adminPortal/index.js +5 -4
  17. package/dist/b2b/index.d.ts +180 -19
  18. package/dist/b2b/index.esm.d.ts +180 -19
  19. package/dist/b2b/index.esm.js +91 -18
  20. package/dist/b2b/index.headless.d.ts +16 -0
  21. package/dist/b2b/index.headless.esm.d.ts +16 -0
  22. package/dist/b2b/index.headless.esm.js +24 -0
  23. package/dist/b2b/index.headless.js +28 -0
  24. package/dist/b2b/index.js +99 -26
  25. package/dist/b2b/index.ui.d.ts +17 -0
  26. package/dist/b2b/index.ui.esm.d.ts +17 -0
  27. package/dist/b2b/index.ui.esm.js +25 -0
  28. package/dist/b2b/index.ui.js +29 -0
  29. package/dist/errors-d9d5fbc8.d.ts +5 -0
  30. package/dist/index-b14d4efe.d.ts +1 -1
  31. package/dist/index.d.ts +134 -77
  32. package/dist/index.esm.d.ts +134 -77
  33. package/dist/index.esm.js +153 -94
  34. package/dist/index.headless.d.ts +16 -0
  35. package/dist/index.headless.esm.d.ts +16 -0
  36. package/dist/index.headless.esm.js +24 -0
  37. package/dist/index.headless.js +28 -0
  38. package/dist/index.js +167 -108
  39. package/dist/index.ui.d.ts +17 -0
  40. package/dist/index.ui.esm.d.ts +17 -0
  41. package/dist/index.ui.esm.js +25 -0
  42. package/dist/index.ui.js +29 -0
  43. package/dist/useIsomorphicLayoutEffect-1babb81e.d.ts +24 -0
  44. package/dist/{invariant-568a7633.js → useIsomorphicLayoutEffect-1babb81e.js} +5 -13
  45. package/dist/useIsomorphicLayoutEffect-65746ef3.d.ts +24 -0
  46. package/dist/{invariant-ae5a5bce.js → useIsomorphicLayoutEffect-65746ef3.js} +4 -16
  47. package/package.json +5 -3
  48. package/dist/StytchB2BContext-081081ee.d.ts +0 -165
  49. package/dist/StytchB2BContext-865b6947.d.ts +0 -165
  50. package/dist/StytchB2BContext-865b6947.js +0 -235
  51. package/dist/invariant-568a7633.d.ts +0 -27
  52. package/dist/invariant-ae5a5bce.d.ts +0 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stytch/react",
3
- "version": "19.4.3",
3
+ "version": "19.5.0",
4
4
  "description": "Stytch's official React Library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -32,10 +32,12 @@
32
32
  "user"
33
33
  ],
34
34
  "devDependencies": {
35
- "@babel/runtime": "7.26.10",
35
+ "@babel/runtime": "7.27.0",
36
+ "@stytch/internal-react-shared": "0.0.0",
37
+ "@stytch/internal-test-utils": "0.0.0",
36
38
  "@stytch/internal-tsconfigs": "0.0.0",
37
39
  "@stytch/js-utils": "0.0.1",
38
- "@stytch/vanilla-js": "5.22.1",
40
+ "@stytch/vanilla-js": "5.22.7",
39
41
  "@testing-library/react": "14.0.0",
40
42
  "@types/jest": "29.5.14",
41
43
  "eslint-config-custom": "0.0.1",
@@ -1,165 +0,0 @@
1
- /// <reference types="react" />
2
- import React from 'react';
3
- import { ReactNode } from "react";
4
- import { Member, MemberSession, Organization, StytchB2BUIClient } from '@stytch/vanilla-js/b2b';
5
- import { StytchB2BHeadlessClient, StytchProjectConfigurationInput } from '@stytch/vanilla-js/b2b/headless';
6
- import { PermissionsMap } from '@stytch/core/public';
7
- /**
8
- * The Stytch Client object passed in to <StytchProvider /> in your application root.
9
- */
10
- type StytchB2BClient<TProjectConfiguration extends StytchProjectConfigurationInput> = StytchB2BHeadlessClient<TProjectConfiguration> | StytchB2BUIClient<TProjectConfiguration>;
11
- type SWRMember = {
12
- /**
13
- * Either the active {@link Member} object, or null if the member is not logged in.
14
- */
15
- member: Member | null;
16
- /**
17
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
18
- */
19
- fromCache: boolean;
20
- };
21
- type SWRMemberSession = {
22
- /**
23
- * Either the active {@link MemberSession} object, or null if the member is not logged in.
24
- */
25
- session: MemberSession | null;
26
- /**
27
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
28
- */
29
- fromCache: boolean;
30
- };
31
- type SWROrganization = {
32
- /**
33
- * Either the active {@link Organization} object, or null if the member is not logged in.
34
- */
35
- organization: Organization | null;
36
- /**
37
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
38
- */
39
- fromCache: boolean;
40
- };
41
- declare const useIsMounted__INTERNAL: () => boolean;
42
- declare const isUIClient: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration>>(client: StytchB2BClient<TProjectConfiguration>) => client is StytchB2BUIClient<TProjectConfiguration>;
43
- /**
44
- * Returns the active Member.
45
- * Check the fromCache property to determine if the member data is from persistent storage.
46
- * @example
47
- * const {member} = useStytchMember();
48
- * return (<h1>Welcome, {member.name}</h1>);
49
- * @returns A {@link SWRUser}
50
- */
51
- declare const useStytchMember: () => SWRMember;
52
- /**
53
- * Returns the active member's Stytch member session.
54
- * @example
55
- * const { session } = useStytchMemberSession();
56
- * useEffect(() => {
57
- * if (!session) {
58
- * router.replace('/login')
59
- * }
60
- * }, [session]);
61
- * @returns A {@link SWRMemberSession}
62
- */
63
- declare const useStytchMemberSession: () => SWRMemberSession;
64
- /**
65
- * Returns the active Stytch organization.
66
- * @example
67
- * const { organization } = useStytchOrganization();
68
- * return organization ? <p>Welcome to {organization.organization_name}</p> : <p>Log in to continue!</p>;
69
- * @returns A {@link SWROrganization}
70
- */
71
- declare const useStytchOrganization: () => SWROrganization;
72
- type SWRIsAuthorized = {
73
- /**
74
- * Whether the logged-in member is allowed to perform the specified action on the specified resource.
75
- */
76
- isAuthorized: boolean;
77
- /**
78
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
79
- */
80
- fromCache: boolean;
81
- };
82
- /**
83
- * Determines whether the logged-in member is allowed to perform the specified action on the specified resource.
84
- * Returns `true` if the member can perform the action, `false` otherwise.
85
- *
86
- * If the member is not logged in, this method will always return false.
87
- * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.
88
- *
89
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
90
- * @example
91
- * const isAuthorized = useStytchIsAuthorized<Permissions>('documents', 'edit');
92
- * return <button disabled={!isAuthorized}>Edit</button>
93
- */
94
- declare const useStytchIsAuthorized: (resourceId: string, action: string) => SWRIsAuthorized;
95
- /**
96
- * Returns the Stytch B2B client stored in the Stytch context.
97
- *
98
- * @example
99
- * const stytch = useStytchB2BClient();
100
- * useEffect(() => {
101
- * stytch.magicLinks.authenticate('...')
102
- * }, [stytch]);
103
- */
104
- declare const useStytchB2BClient: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration> = Stytch.DefaultProjectConfiguration>() => StytchB2BHeadlessClient<TProjectConfiguration>;
105
- declare const withStytchB2BClient: <T extends object, TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration>>(Component: React.ComponentType<T & {
106
- stytch: StytchB2BHeadlessClient<TProjectConfiguration>;
107
- }>) => React.ComponentType<T>;
108
- declare const withStytchMember: <T extends object>(Component: React.ComponentType<T & {
109
- stytchMember: Member | null;
110
- stytchMemberIsFromCache: boolean;
111
- }>) => React.ComponentType<T>;
112
- declare const withStytchMemberSession: <T extends object>(Component: React.ComponentType<T & {
113
- stytchMemberSession: MemberSession | null;
114
- stytchMemberSessionIsFromCache: boolean;
115
- }>) => React.ComponentType<T>;
116
- declare const withStytchOrganization: <T extends object>(Component: React.ComponentType<T & {
117
- stytchOrganization: Organization | null;
118
- stytchOrganizationIsFromCache: boolean;
119
- }>) => React.ComponentType<T>;
120
- /**
121
- * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
122
- * Evaluates all permissions granted to the logged-in member.
123
- * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
124
- * Each boolean will be `true` if the member can perform the action, `false` otherwise.
125
- *
126
- * If the member is not logged in, all values will be false.
127
- *
128
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
129
- * @example
130
- * type Permissions = {
131
- * document: 'create' | 'read' | 'write
132
- * image: 'create' | 'read'
133
- * }
134
- *
135
- * const MyComponent = (props) => {
136
- * const canEditDocuments = props.stytchPermissions.document.edit;
137
- * const canReadImages = props.stytchPermissions.image.read;
138
- * }
139
- * return withStytchPermissions<Permissions>(MyComponent)
140
- */
141
- declare const withStytchPermissions: <Permissions_1 extends Record<string, string>, T extends object>(Component: React.ComponentType<T & {
142
- stytchPermissions: PermissionsMap<Permissions_1>;
143
- }>) => React.ComponentType<T>;
144
- type StytchB2BProviderProps<TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration> = {
145
- /**
146
- * A Stytch client instance {@link StytchB2BHeadlessClient}
147
- */
148
- stytch: StytchB2BClient<TProjectConfiguration>;
149
- children?: ReactNode;
150
- };
151
- /**
152
- * The Stytch Context Provider.
153
- * Wrap your application with this component in the root file in order to use Stytch everywhere in your app.
154
- * @example
155
- * const stytch = new StytchB2BHeadlessClient('public-token-<find yours in the stytch dashboard>')
156
- *
157
- * ReactDOM.render(
158
- * <StytchB2BProvider stytch={stytch}>
159
- * <App />
160
- * </StytchProvider>,
161
- * document.getElementById('root'),
162
- * )
163
- */
164
- declare const StytchB2BProvider: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration> = Stytch.DefaultProjectConfiguration>({ stytch, children, }: StytchB2BProviderProps<TProjectConfiguration>) => JSX.Element;
165
- export { useIsMounted__INTERNAL, isUIClient, useStytchMember, useStytchMemberSession, useStytchOrganization, useStytchIsAuthorized, useStytchB2BClient, withStytchB2BClient, withStytchMember, withStytchMemberSession, withStytchOrganization, withStytchPermissions, StytchB2BProviderProps, StytchB2BProvider };
@@ -1,165 +0,0 @@
1
- /// <reference types="react" />
2
- import React from 'react';
3
- import { ReactNode } from "react";
4
- import { Member, MemberSession, Organization, StytchB2BUIClient } from '@stytch/vanilla-js/b2b';
5
- import { StytchB2BHeadlessClient, StytchProjectConfigurationInput } from '@stytch/vanilla-js/b2b/headless';
6
- import { PermissionsMap } from '@stytch/core/public';
7
- /**
8
- * The Stytch Client object passed in to <StytchProvider /> in your application root.
9
- */
10
- type StytchB2BClient<TProjectConfiguration extends StytchProjectConfigurationInput> = StytchB2BHeadlessClient<TProjectConfiguration> | StytchB2BUIClient<TProjectConfiguration>;
11
- type SWRMember = {
12
- /**
13
- * Either the active {@link Member} object, or null if the member is not logged in.
14
- */
15
- member: Member | null;
16
- /**
17
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
18
- */
19
- fromCache: boolean;
20
- };
21
- type SWRMemberSession = {
22
- /**
23
- * Either the active {@link MemberSession} object, or null if the member is not logged in.
24
- */
25
- session: MemberSession | null;
26
- /**
27
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
28
- */
29
- fromCache: boolean;
30
- };
31
- type SWROrganization = {
32
- /**
33
- * Either the active {@link Organization} object, or null if the member is not logged in.
34
- */
35
- organization: Organization | null;
36
- /**
37
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
38
- */
39
- fromCache: boolean;
40
- };
41
- declare const useIsMounted__INTERNAL: () => boolean;
42
- declare const isUIClient: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration>>(client: StytchB2BClient<TProjectConfiguration>) => client is StytchB2BUIClient<TProjectConfiguration>;
43
- /**
44
- * Returns the active Member.
45
- * Check the fromCache property to determine if the member data is from persistent storage.
46
- * @example
47
- * const {member} = useStytchMember();
48
- * return (<h1>Welcome, {member.name}</h1>);
49
- * @returns A {@link SWRUser}
50
- */
51
- declare const useStytchMember: () => SWRMember;
52
- /**
53
- * Returns the active member's Stytch member session.
54
- * @example
55
- * const { session } = useStytchMemberSession();
56
- * useEffect(() => {
57
- * if (!session) {
58
- * router.replace('/login')
59
- * }
60
- * }, [session]);
61
- * @returns A {@link SWRMemberSession}
62
- */
63
- declare const useStytchMemberSession: () => SWRMemberSession;
64
- /**
65
- * Returns the active Stytch organization.
66
- * @example
67
- * const { organization } = useStytchOrganization();
68
- * return organization ? <p>Welcome to {organization.organization_name}</p> : <p>Log in to continue!</p>;
69
- * @returns A {@link SWROrganization}
70
- */
71
- declare const useStytchOrganization: () => SWROrganization;
72
- type SWRIsAuthorized = {
73
- /**
74
- * Whether the logged-in member is allowed to perform the specified action on the specified resource.
75
- */
76
- isAuthorized: boolean;
77
- /**
78
- * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
79
- */
80
- fromCache: boolean;
81
- };
82
- /**
83
- * Determines whether the logged-in member is allowed to perform the specified action on the specified resource.
84
- * Returns `true` if the member can perform the action, `false` otherwise.
85
- *
86
- * If the member is not logged in, this method will always return false.
87
- * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.
88
- *
89
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
90
- * @example
91
- * const isAuthorized = useStytchIsAuthorized<Permissions>('documents', 'edit');
92
- * return <button disabled={!isAuthorized}>Edit</button>
93
- */
94
- declare const useStytchIsAuthorized: (resourceId: string, action: string) => SWRIsAuthorized;
95
- /**
96
- * Returns the Stytch B2B client stored in the Stytch context.
97
- *
98
- * @example
99
- * const stytch = useStytchB2BClient();
100
- * useEffect(() => {
101
- * stytch.magicLinks.authenticate('...')
102
- * }, [stytch]);
103
- */
104
- declare const useStytchB2BClient: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration> = Stytch.DefaultProjectConfiguration>() => StytchB2BHeadlessClient<TProjectConfiguration>;
105
- declare const withStytchB2BClient: <T extends object, TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration>>(Component: React.ComponentType<T & {
106
- stytch: StytchB2BHeadlessClient<TProjectConfiguration>;
107
- }>) => React.ComponentType<T>;
108
- declare const withStytchMember: <T extends object>(Component: React.ComponentType<T & {
109
- stytchMember: Member | null;
110
- stytchMemberIsFromCache: boolean;
111
- }>) => React.ComponentType<T>;
112
- declare const withStytchMemberSession: <T extends object>(Component: React.ComponentType<T & {
113
- stytchMemberSession: MemberSession | null;
114
- stytchMemberSessionIsFromCache: boolean;
115
- }>) => React.ComponentType<T>;
116
- declare const withStytchOrganization: <T extends object>(Component: React.ComponentType<T & {
117
- stytchOrganization: Organization | null;
118
- stytchOrganizationIsFromCache: boolean;
119
- }>) => React.ComponentType<T>;
120
- /**
121
- * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
122
- * Evaluates all permissions granted to the logged-in member.
123
- * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
124
- * Each boolean will be `true` if the member can perform the action, `false` otherwise.
125
- *
126
- * If the member is not logged in, all values will be false.
127
- *
128
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
129
- * @example
130
- * type Permissions = {
131
- * document: 'create' | 'read' | 'write
132
- * image: 'create' | 'read'
133
- * }
134
- *
135
- * const MyComponent = (props) => {
136
- * const canEditDocuments = props.stytchPermissions.document.edit;
137
- * const canReadImages = props.stytchPermissions.image.read;
138
- * }
139
- * return withStytchPermissions<Permissions>(MyComponent)
140
- */
141
- declare const withStytchPermissions: <Permissions_1 extends Record<string, string>, T extends object>(Component: React.ComponentType<T & {
142
- stytchPermissions: PermissionsMap<Permissions_1>;
143
- }>) => React.ComponentType<T>;
144
- type StytchB2BProviderProps<TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration> = {
145
- /**
146
- * A Stytch client instance {@link StytchB2BHeadlessClient}
147
- */
148
- stytch: StytchB2BClient<TProjectConfiguration>;
149
- children?: ReactNode;
150
- };
151
- /**
152
- * The Stytch Context Provider.
153
- * Wrap your application with this component in the root file in order to use Stytch everywhere in your app.
154
- * @example
155
- * const stytch = new StytchB2BHeadlessClient('public-token-<find yours in the stytch dashboard>')
156
- *
157
- * ReactDOM.render(
158
- * <StytchB2BProvider stytch={stytch}>
159
- * <App />
160
- * </StytchProvider>,
161
- * document.getElementById('root'),
162
- * )
163
- */
164
- declare const StytchB2BProvider: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration> = Stytch.DefaultProjectConfiguration>({ stytch, children, }: StytchB2BProviderProps<TProjectConfiguration>) => JSX.Element;
165
- export { useIsMounted__INTERNAL, isUIClient, useStytchMember, useStytchMemberSession, useStytchOrganization, useStytchIsAuthorized, useStytchB2BClient, withStytchB2BClient, withStytchMember, withStytchMemberSession, withStytchOrganization, withStytchPermissions, StytchB2BProviderProps, StytchB2BProvider };
@@ -1,235 +0,0 @@
1
- 'use strict';
2
-
3
- var React = require('react');
4
- var invariant = require('./invariant-ae5a5bce.js');
5
-
6
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7
-
8
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
9
-
10
- const initialMember = {
11
- member: null,
12
- fromCache: false,
13
- };
14
- const initialMemberSession = {
15
- session: null,
16
- fromCache: false,
17
- };
18
- const initialOrganization = {
19
- organization: null,
20
- fromCache: false,
21
- };
22
- const StytchB2BContext = React.createContext({ isMounted: false });
23
- const StytchMemberContext = React.createContext(initialMember);
24
- const StytchMemberSessionContext = React.createContext(initialMemberSession);
25
- const StytchOrganizationContext = React.createContext(initialOrganization);
26
- const useIsMounted__INTERNAL = () => React.useContext(StytchB2BContext).isMounted;
27
- const isUIClient = (client) => {
28
- return client.mount !== undefined;
29
- };
30
- /**
31
- * Returns the active Member.
32
- * Check the fromCache property to determine if the member data is from persistent storage.
33
- * @example
34
- * const {member} = useStytchMember();
35
- * return (<h1>Welcome, {member.name}</h1>);
36
- * @returns A {@link SWRUser}
37
- */
38
- const useStytchMember = () => {
39
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('useStytchMember', 'StytchB2BProvider'));
40
- return React.useContext(StytchMemberContext);
41
- };
42
- /**
43
- * Returns the active member's Stytch member session.
44
- * @example
45
- * const { session } = useStytchMemberSession();
46
- * useEffect(() => {
47
- * if (!session) {
48
- * router.replace('/login')
49
- * }
50
- * }, [session]);
51
- * @returns A {@link SWRMemberSession}
52
- */
53
- const useStytchMemberSession = () => {
54
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('useStytchMemberSession', 'StytchB2BProvider'));
55
- return React.useContext(StytchMemberSessionContext);
56
- };
57
- /**
58
- * Returns the active Stytch organization.
59
- * @example
60
- * const { organization } = useStytchOrganization();
61
- * return organization ? <p>Welcome to {organization.organization_name}</p> : <p>Log in to continue!</p>;
62
- * @returns A {@link SWROrganization}
63
- */
64
- const useStytchOrganization = () => {
65
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('useStytchOrganization', 'StytchB2BProvider'));
66
- return React.useContext(StytchOrganizationContext);
67
- };
68
- /**
69
- * Determines whether the logged-in member is allowed to perform the specified action on the specified resource.
70
- * Returns `true` if the member can perform the action, `false` otherwise.
71
- *
72
- * If the member is not logged in, this method will always return false.
73
- * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.
74
- *
75
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
76
- * @example
77
- * const isAuthorized = useStytchIsAuthorized<Permissions>('documents', 'edit');
78
- * return <button disabled={!isAuthorized}>Edit</button>
79
- */
80
- const useStytchIsAuthorized = (resourceId, action) => {
81
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('useStytchIsAuthorized', 'StytchB2BProvider'));
82
- const client = useStytchB2BClient();
83
- const { session } = useStytchMemberSession();
84
- const [isAuthorized, setIsAuthorized] = invariant.useAsyncState({
85
- fromCache: true,
86
- isAuthorized: client.rbac.isAuthorizedSync(resourceId, action),
87
- });
88
- React.useEffect(() => {
89
- client.rbac.isAuthorized(resourceId, action).then((isAuthorized) => setIsAuthorized({
90
- fromCache: false,
91
- isAuthorized,
92
- }));
93
- }, [client, session === null || session === void 0 ? void 0 : session.roles, resourceId, action, setIsAuthorized]);
94
- return isAuthorized;
95
- };
96
- /**
97
- * Returns the Stytch B2B client stored in the Stytch context.
98
- *
99
- * @example
100
- * const stytch = useStytchB2BClient();
101
- * useEffect(() => {
102
- * stytch.magicLinks.authenticate('...')
103
- * }, [stytch]);
104
- */
105
- const useStytchB2BClient = () => {
106
- const ctx = React.useContext(StytchB2BContext);
107
- invariant.invariant(ctx.isMounted, invariant.noProviderError('useStytchB2BClient', 'StytchB2BProvider'));
108
- return ctx.client;
109
- };
110
- const withStytchB2BClient = (Component) => {
111
- const WithStytch = (props) => {
112
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('withStytchB2BClient', 'StytchB2BProvider'));
113
- return React__default["default"].createElement(Component, Object.assign({}, props, { stytch: useStytchB2BClient() }));
114
- };
115
- WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
116
- return WithStytch;
117
- };
118
- const withStytchMember = (Component) => {
119
- const WithStytchUser = (props) => {
120
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('withStytchMember', 'StytchB2BProvider'));
121
- const { member, fromCache } = useStytchMember();
122
- return React__default["default"].createElement(Component, Object.assign({}, props, { stytchMember: member, stytchMemberIsFromCache: fromCache }));
123
- };
124
- WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
125
- return WithStytchUser;
126
- };
127
- const withStytchMemberSession = (Component) => {
128
- const WithStytchSession = (props) => {
129
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('withStytchMemberSession', 'StytchB2BProvider'));
130
- const { session, fromCache } = useStytchMemberSession();
131
- return React__default["default"].createElement(Component, Object.assign({}, props, { stytchMemberSession: session, stytchMemberSessionIsFromCache: fromCache }));
132
- };
133
- WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
134
- return WithStytchSession;
135
- };
136
- const withStytchOrganization = (Component) => {
137
- const WithStytchOrganization = (props) => {
138
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('withStytchOrganization', 'StytchB2BProvider'));
139
- const { organization, fromCache } = useStytchOrganization();
140
- return React__default["default"].createElement(Component, Object.assign({}, props, { stytchOrganization: organization, stytchOrganizationIsFromCache: fromCache }));
141
- };
142
- WithStytchOrganization.displayName = `withStytchOrganization(${Component.displayName || Component.name || 'Component'})`;
143
- return WithStytchOrganization;
144
- };
145
- /**
146
- * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
147
- * Evaluates all permissions granted to the logged-in member.
148
- * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
149
- * Each boolean will be `true` if the member can perform the action, `false` otherwise.
150
- *
151
- * If the member is not logged in, all values will be false.
152
- *
153
- * Remember - authorization checks for sensitive actions should always occur on the backend as well.
154
- * @example
155
- * type Permissions = {
156
- * document: 'create' | 'read' | 'write
157
- * image: 'create' | 'read'
158
- * }
159
- *
160
- * const MyComponent = (props) => {
161
- * const canEditDocuments = props.stytchPermissions.document.edit;
162
- * const canReadImages = props.stytchPermissions.image.read;
163
- * }
164
- * return withStytchPermissions<Permissions>(MyComponent)
165
- */
166
- const withStytchPermissions = (Component) => {
167
- const WithStytchPermissions = (props) => {
168
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('useRBACPermissions', 'StytchB2BProvider'));
169
- const client = useStytchB2BClient();
170
- const { session } = useStytchMemberSession();
171
- const [permissions, setPermissions] = invariant.useAsyncState({ loaded: false, value: null });
172
- React.useEffect(() => {
173
- client.rbac
174
- .allPermissions()
175
- .then((permissions) => setPermissions({ loaded: true, value: permissions }));
176
- }, [client, session === null || session === void 0 ? void 0 : session.roles, setPermissions]);
177
- if (!permissions.loaded) {
178
- return null;
179
- }
180
- return React__default["default"].createElement(Component, Object.assign({}, props, { stytchPermissions: permissions.value }));
181
- };
182
- WithStytchPermissions.displayName = `withStytchPermissions(${Component.displayName || Component.name || 'Component'})`;
183
- return WithStytchPermissions;
184
- };
185
- /**
186
- * The Stytch Context Provider.
187
- * Wrap your application with this component in the root file in order to use Stytch everywhere in your app.
188
- * @example
189
- * const stytch = new StytchB2BHeadlessClient('public-token-<find yours in the stytch dashboard>')
190
- *
191
- * ReactDOM.render(
192
- * <StytchB2BProvider stytch={stytch}>
193
- * <App />
194
- * </StytchProvider>,
195
- * document.getElementById('root'),
196
- * )
197
- */
198
- const StytchB2BProvider = ({ stytch, children, }) => {
199
- invariant.invariant(!useIsMounted__INTERNAL(), invariant.B2BProviderMustBeUniqueError);
200
- invariant.invariant(typeof window !== 'undefined', invariant.noSSRError);
201
- const ctx = React.useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);
202
- const [{ member, session, organization }, setClientState] = invariant.useAsyncState({
203
- session: stytch.session.getInfo(),
204
- member: stytch.self.getInfo(),
205
- organization: stytch.organization.getInfo(),
206
- });
207
- React.useEffect(() => stytch.onStateChange(() => {
208
- setClientState((oldState) => {
209
- const newState = {
210
- session: stytch.session.getInfo(),
211
- member: stytch.self.getInfo(),
212
- organization: stytch.organization.getInfo(),
213
- };
214
- return invariant.mergeWithStableProps(oldState, newState);
215
- });
216
- }), [setClientState, stytch]);
217
- return (React__default["default"].createElement(StytchB2BContext.Provider, { value: ctx },
218
- React__default["default"].createElement(StytchOrganizationContext.Provider, { value: organization },
219
- React__default["default"].createElement(StytchMemberContext.Provider, { value: member },
220
- React__default["default"].createElement(StytchMemberSessionContext.Provider, { value: session }, children)))));
221
- };
222
-
223
- exports.StytchB2BProvider = StytchB2BProvider;
224
- exports.isUIClient = isUIClient;
225
- exports.useIsMounted__INTERNAL = useIsMounted__INTERNAL;
226
- exports.useStytchB2BClient = useStytchB2BClient;
227
- exports.useStytchIsAuthorized = useStytchIsAuthorized;
228
- exports.useStytchMember = useStytchMember;
229
- exports.useStytchMemberSession = useStytchMemberSession;
230
- exports.useStytchOrganization = useStytchOrganization;
231
- exports.withStytchB2BClient = withStytchB2BClient;
232
- exports.withStytchMember = withStytchMember;
233
- exports.withStytchMemberSession = withStytchMemberSession;
234
- exports.withStytchOrganization = withStytchOrganization;
235
- exports.withStytchPermissions = withStytchPermissions;
@@ -1,27 +0,0 @@
1
- import { Dispatch, SetStateAction } from "react";
2
- declare const noProviderError: (item: string, provider?: string) => string;
3
- declare const providerMustBeUniqueError = "You cannot render a <StytchProvider> inside another <StytchProvider>.";
4
- declare const B2BProviderMustBeUniqueError = "You cannot render a <StytchB2BProvider> inside another <StytchB2BProvider>.";
5
- declare const noSSRError = "The @stytch/react library is not meant for use with serverside environments like NextJS.\nUse the @stytch/nextjs library instead - \nnpm remove @stytch/react && npm install @stytch/nextjs\n";
6
- declare const noHeadlessClientError = "Tried to create a Stytch Login UI element using the Stytch Headless SDK.\nYou must use the UI SDK to use UI elements.\nPlease make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.";
7
- // useState can cause memory leaks if it is set after the component unmounted. For example, if it is
8
- // set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
9
- declare const useAsyncState: <T>(initialState: T | (() => T)) => [
10
- T,
11
- Dispatch<SetStateAction<T>>
12
- ];
13
- /**
14
- * Returns a version of `newValue` whose properties that are deeply equal to
15
- * those in `oldValue` are replaced with those from `oldValue`. This provides a
16
- * limited form of "structural sharing" that provides a stable reference for
17
- * unchanged slices of the object.
18
- *
19
- * If `oldValue` and `newValue` are referentially equal, the same value is
20
- * returned.
21
- *
22
- * @param oldValue The old value
23
- * @param newValue The new value
24
- */
25
- declare const mergeWithStableProps: <T extends Record<string, unknown>, U extends Record<string, unknown> = T>(oldValue: U, newValue: T) => T;
26
- declare function invariant(cond: any, message: string): asserts cond;
27
- export { noProviderError, providerMustBeUniqueError, B2BProviderMustBeUniqueError, noSSRError, noHeadlessClientError, useAsyncState, mergeWithStableProps, invariant };
@@ -1,27 +0,0 @@
1
- import { Dispatch, SetStateAction } from "react";
2
- declare const noProviderError: (item: string, provider?: string) => string;
3
- declare const providerMustBeUniqueError = "You cannot render a <StytchProvider> inside another <StytchProvider>.";
4
- declare const B2BProviderMustBeUniqueError = "You cannot render a <StytchB2BProvider> inside another <StytchB2BProvider>.";
5
- declare const noSSRError = "The @stytch/react library is not meant for use with serverside environments like NextJS.\nUse the @stytch/nextjs library instead - \nnpm remove @stytch/react && npm install @stytch/nextjs\n";
6
- declare const noHeadlessClientError = "Tried to create a Stytch Login UI element using the Stytch Headless SDK.\nYou must use the UI SDK to use UI elements.\nPlease make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.";
7
- // useState can cause memory leaks if it is set after the component unmounted. For example, if it is
8
- // set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
9
- declare const useAsyncState: <T>(initialState: T | (() => T)) => [
10
- T,
11
- Dispatch<SetStateAction<T>>
12
- ];
13
- /**
14
- * Returns a version of `newValue` whose properties that are deeply equal to
15
- * those in `oldValue` are replaced with those from `oldValue`. This provides a
16
- * limited form of "structural sharing" that provides a stable reference for
17
- * unchanged slices of the object.
18
- *
19
- * If `oldValue` and `newValue` are referentially equal, the same value is
20
- * returned.
21
- *
22
- * @param oldValue The old value
23
- * @param newValue The new value
24
- */
25
- declare const mergeWithStableProps: <T extends Record<string, unknown>, U extends Record<string, unknown> = T>(oldValue: U, newValue: T) => T;
26
- declare function invariant(cond: any, message: string): asserts cond;
27
- export { noProviderError, providerMustBeUniqueError, B2BProviderMustBeUniqueError, noSSRError, noHeadlessClientError, useAsyncState, mergeWithStableProps, invariant };