@stytch/nextjs 14.0.0 → 16.0.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @stytch/nextjs
2
2
 
3
+ ## 16.0.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [2379b29]
8
+ - @stytch/vanilla-js@4.1.0
9
+
10
+ ## 15.0.0
11
+
12
+ ### Minor Changes
13
+
14
+ - 6890694: Mark stytch.member as deprecated in favor of stytch.self
15
+ Adds RBAC functionality
16
+
17
+ ### Patch Changes
18
+
19
+ - Updated dependencies [9ee61b3]
20
+ - Updated dependencies [b34293a]
21
+ - Updated dependencies [c3c108b]
22
+ - Updated dependencies [6890694]
23
+ - @stytch/vanilla-js@4.0.0
24
+
3
25
  ## 14.0.0
4
26
 
5
27
  ### Patch Changes
@@ -3,6 +3,7 @@ import React from "react";
3
3
  import { ReactNode } from "react";
4
4
  import { Member, MemberSession, StytchB2BUIClient } from "@stytch/vanilla-js/b2b";
5
5
  import { StytchB2BHeadlessClient } from "@stytch/vanilla-js/b2b/headless";
6
+ import { PermissionsMap } from "@stytch/core/public";
6
7
  import { Callbacks, StyleConfig, StytchB2BUIConfig } from "@stytch/vanilla-js";
7
8
  /**
8
9
  * The Stytch Client object passed in to <StytchProvider /> in your `_app.js`.
@@ -59,6 +60,24 @@ declare const useStytchMember: () => SWRMember;
59
60
  * }, [session, isInitialized]);
60
61
  */
61
62
  declare const useStytchMemberSession: () => SWRMemberSession;
63
+ type SWRIsAuthorized = {
64
+ isAuthorized: boolean;
65
+ fromCache: boolean;
66
+ isInitialized: boolean;
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
+ declare const useStytchIsAuthorized: (resourceId: string, action: string) => SWRIsAuthorized;
62
81
  /**
63
82
  * Returns the Stytch client stored in the Stytch context.
64
83
  *
@@ -68,9 +87,9 @@ declare const useStytchMemberSession: () => SWRMemberSession;
68
87
  * stytch.magicLinks.authenticate('...')
69
88
  * }, [stytch]);
70
89
  */
71
- declare const useStytchB2BClient: () => StytchB2BClient;
90
+ declare const useStytchB2BClient: () => StytchB2BHeadlessClient;
72
91
  declare const withStytchB2BClient: <T extends object>(Component: React.ComponentType<T & {
73
- stytch: StytchB2BClient;
92
+ stytch: StytchB2BHeadlessClient;
74
93
  }>) => React.ComponentType<T>;
75
94
  declare const withStytchMember: <T extends object>(Component: React.ComponentType<T & {
76
95
  stytchMember: Member | null;
@@ -82,6 +101,30 @@ declare const withStytchMemberSession: <T extends object>(Component: React.Compo
82
101
  stytchMemberSessionIsInitialized: boolean;
83
102
  stytchMemberSessionIsFromCache: boolean;
84
103
  }>) => React.ComponentType<T>;
104
+ /**
105
+ * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
106
+ * Evaluates all permissions granted to the logged-in member.
107
+ * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
108
+ * Each boolean will be `true` if the member can perform the action, `false` otherwise.
109
+ *
110
+ * If the member is not logged in, all values will be false.
111
+ *
112
+ * Remember - authorization checks for sensitive actions should always occur on the backend as well.
113
+ * @example
114
+ * type Permissions = {
115
+ * document: 'create' | 'read' | 'write
116
+ * image: 'create' | 'read'
117
+ * }
118
+ *
119
+ * const MyComponent = (props) => {
120
+ * const canEditDocuments = props.stytchPermissions.document.edit;
121
+ * const canReadImages = props.stytchPermissions.image.read;
122
+ * }
123
+ * return withStytchPermissions<Permissions>(MyComponent)
124
+ */
125
+ declare const withStytchPermissions: <Permissions_1 extends Record<string, string>, T extends object>(Component: React.ComponentType<T & {
126
+ stytchPermissions: PermissionsMap<Permissions_1>;
127
+ }>) => React.ComponentType<T>;
85
128
  interface StytchB2BProviderProps {
86
129
  /**
87
130
  * A Stytch client instance, created using either {@link createStytchHeadlessClient} or {@link createStytchUIClient}
@@ -204,5 +247,5 @@ interface StytchB2BProps {
204
247
  * @param props {@link StytchB2BProps}
205
248
  */
206
249
  declare const StytchB2B: ({ styles, callbacks, config }: StytchB2BProps) => React.JSX.Element;
207
- export { StytchB2BProvider, useStytchB2BClient, useStytchMemberSession, useStytchMember, withStytchB2BClient, withStytchMemberSession, withStytchMember, StytchB2B };
250
+ export { StytchB2BProvider, useStytchB2BClient, useStytchMemberSession, useStytchMember, useStytchIsAuthorized, withStytchB2BClient, withStytchMemberSession, withStytchMember, withStytchPermissions, StytchB2B };
208
251
  export type { StytchB2BProviderProps };
@@ -3,6 +3,7 @@ import React from "react";
3
3
  import { ReactNode } from "react";
4
4
  import { Member, MemberSession, StytchB2BUIClient } from "@stytch/vanilla-js/b2b";
5
5
  import { StytchB2BHeadlessClient } from "@stytch/vanilla-js/b2b/headless";
6
+ import { PermissionsMap } from "@stytch/core/public";
6
7
  import { Callbacks, StyleConfig, StytchB2BUIConfig } from "@stytch/vanilla-js";
7
8
  /**
8
9
  * The Stytch Client object passed in to <StytchProvider /> in your `_app.js`.
@@ -59,6 +60,24 @@ declare const useStytchMember: () => SWRMember;
59
60
  * }, [session, isInitialized]);
60
61
  */
61
62
  declare const useStytchMemberSession: () => SWRMemberSession;
63
+ type SWRIsAuthorized = {
64
+ isAuthorized: boolean;
65
+ fromCache: boolean;
66
+ isInitialized: boolean;
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
+ declare const useStytchIsAuthorized: (resourceId: string, action: string) => SWRIsAuthorized;
62
81
  /**
63
82
  * Returns the Stytch client stored in the Stytch context.
64
83
  *
@@ -68,9 +87,9 @@ declare const useStytchMemberSession: () => SWRMemberSession;
68
87
  * stytch.magicLinks.authenticate('...')
69
88
  * }, [stytch]);
70
89
  */
71
- declare const useStytchB2BClient: () => StytchB2BClient;
90
+ declare const useStytchB2BClient: () => StytchB2BHeadlessClient;
72
91
  declare const withStytchB2BClient: <T extends object>(Component: React.ComponentType<T & {
73
- stytch: StytchB2BClient;
92
+ stytch: StytchB2BHeadlessClient;
74
93
  }>) => React.ComponentType<T>;
75
94
  declare const withStytchMember: <T extends object>(Component: React.ComponentType<T & {
76
95
  stytchMember: Member | null;
@@ -82,6 +101,30 @@ declare const withStytchMemberSession: <T extends object>(Component: React.Compo
82
101
  stytchMemberSessionIsInitialized: boolean;
83
102
  stytchMemberSessionIsFromCache: boolean;
84
103
  }>) => React.ComponentType<T>;
104
+ /**
105
+ * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
106
+ * Evaluates all permissions granted to the logged-in member.
107
+ * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
108
+ * Each boolean will be `true` if the member can perform the action, `false` otherwise.
109
+ *
110
+ * If the member is not logged in, all values will be false.
111
+ *
112
+ * Remember - authorization checks for sensitive actions should always occur on the backend as well.
113
+ * @example
114
+ * type Permissions = {
115
+ * document: 'create' | 'read' | 'write
116
+ * image: 'create' | 'read'
117
+ * }
118
+ *
119
+ * const MyComponent = (props) => {
120
+ * const canEditDocuments = props.stytchPermissions.document.edit;
121
+ * const canReadImages = props.stytchPermissions.image.read;
122
+ * }
123
+ * return withStytchPermissions<Permissions>(MyComponent)
124
+ */
125
+ declare const withStytchPermissions: <Permissions_1 extends Record<string, string>, T extends object>(Component: React.ComponentType<T & {
126
+ stytchPermissions: PermissionsMap<Permissions_1>;
127
+ }>) => React.ComponentType<T>;
85
128
  interface StytchB2BProviderProps {
86
129
  /**
87
130
  * A Stytch client instance, created using either {@link createStytchHeadlessClient} or {@link createStytchUIClient}
@@ -204,5 +247,5 @@ interface StytchB2BProps {
204
247
  * @param props {@link StytchB2BProps}
205
248
  */
206
249
  declare const StytchB2B: ({ styles, callbacks, config }: StytchB2BProps) => React.JSX.Element;
207
- export { StytchB2BProvider, useStytchB2BClient, useStytchMemberSession, useStytchMember, withStytchB2BClient, withStytchMemberSession, withStytchMember, StytchB2B };
250
+ export { StytchB2BProvider, useStytchB2BClient, useStytchMemberSession, useStytchMember, useStytchIsAuthorized, withStytchB2BClient, withStytchMemberSession, withStytchMember, withStytchPermissions, StytchB2B };
208
251
  export type { StytchB2BProviderProps };
@@ -89,6 +89,47 @@ const useStytchMemberSession = () => {
89
89
  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchMemberSession', 'StytchB2BProvider'));
90
90
  return useContext(StytchMemberSessionContext);
91
91
  };
92
+ /**
93
+ * Determines whether the logged-in member is allowed to perform the specified action on the specified resource.
94
+ * Returns `true` if the member can perform the action, `false` otherwise.
95
+ *
96
+ * If the member is not logged in, this method will always return false.
97
+ * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.
98
+ *
99
+ * Remember - authorization checks for sensitive actions should always occur on the backend as well.
100
+ * @example
101
+ * const isAuthorized = useStytchIsAuthorized<Permissions>('documents', 'edit');
102
+ * return <button disabled={!isAuthorized}>Edit</button>
103
+ */
104
+ const useStytchIsAuthorized = (resourceId, action) => {
105
+ invariant(useIsMounted__INTERNAL(), noProviderError('useStytchIsAuthorized', 'StytchB2BProvider'));
106
+ const client = useStytchB2BClient();
107
+ const { session } = useStytchMemberSession();
108
+ const [isAuthorized, setIsAuthorized] = useAsyncState({
109
+ isInitialized: false,
110
+ fromCache: false,
111
+ isAuthorized: false,
112
+ });
113
+ useEffect(() => {
114
+ if (isStytchSSRProxy(client)) {
115
+ return;
116
+ }
117
+ setIsAuthorized({
118
+ isInitialized: true,
119
+ fromCache: true,
120
+ isAuthorized: client.rbac.isAuthorizedSync(resourceId, action),
121
+ });
122
+ }, []);
123
+ useEffect(() => {
124
+ if (isStytchSSRProxy(client)) {
125
+ return;
126
+ }
127
+ client.rbac.isAuthorized(resourceId, action).then((isAuthorized) => {
128
+ setIsAuthorized({ isAuthorized, fromCache: false, isInitialized: true });
129
+ });
130
+ }, [client, session === null || session === void 0 ? void 0 : session.roles, resourceId, action]);
131
+ return isAuthorized;
132
+ };
92
133
  /**
93
134
  * Returns the Stytch client stored in the Stytch context.
94
135
  *
@@ -129,6 +170,46 @@ const withStytchMemberSession = (Component) => {
129
170
  WithStytchSession.displayName = `withStytchMemberSession(${Component.displayName || Component.name || 'Component'})`;
130
171
  return WithStytchSession;
131
172
  };
173
+ /**
174
+ * Wrap your component with this HOC in order to receive the permissions for the logged-in member.
175
+ * Evaluates all permissions granted to the logged-in member.
176
+ * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
177
+ * Each boolean will be `true` if the member can perform the action, `false` otherwise.
178
+ *
179
+ * If the member is not logged in, all values will be false.
180
+ *
181
+ * Remember - authorization checks for sensitive actions should always occur on the backend as well.
182
+ * @example
183
+ * type Permissions = {
184
+ * document: 'create' | 'read' | 'write
185
+ * image: 'create' | 'read'
186
+ * }
187
+ *
188
+ * const MyComponent = (props) => {
189
+ * const canEditDocuments = props.stytchPermissions.document.edit;
190
+ * const canReadImages = props.stytchPermissions.image.read;
191
+ * }
192
+ * return withStytchPermissions<Permissions>(MyComponent)
193
+ */
194
+ const withStytchPermissions = (Component) => {
195
+ const WithStytchPermissions = (props) => {
196
+ invariant(useIsMounted__INTERNAL(), noProviderError('useRBACPermissions', 'StytchB2BProvider'));
197
+ const client = useStytchB2BClient();
198
+ const { session } = useStytchMemberSession();
199
+ const [permissions, setPermissions] = useAsyncState({ loaded: false, value: null });
200
+ useEffect(() => {
201
+ client.rbac
202
+ .allPermissions()
203
+ .then((permissions) => setPermissions({ loaded: true, value: permissions }));
204
+ }, [client, session === null || session === void 0 ? void 0 : session.roles]);
205
+ if (!permissions.loaded) {
206
+ return null;
207
+ }
208
+ return React.createElement(Component, Object.assign({}, props, { stytchPermissions: permissions.value }));
209
+ };
210
+ WithStytchPermissions.displayName = `withStytchPermissions(${Component.displayName || Component.name || 'Component'})`;
211
+ return WithStytchPermissions;
212
+ };
132
213
  /**
133
214
  * The Stytch Context Provider.
134
215
  * Wrap your application with this component in `_app.js` in order to use Stytch everywhere in your app.
@@ -150,7 +231,7 @@ const StytchB2BProvider = ({ stytch, children }) => {
150
231
  return;
151
232
  }
152
233
  setMember({
153
- member: stytch.member.getSync(),
234
+ member: stytch.self.getSync(),
154
235
  fromCache: true,
155
236
  isInitialized: true,
156
237
  });
@@ -159,7 +240,7 @@ const StytchB2BProvider = ({ stytch, children }) => {
159
240
  fromCache: true,
160
241
  isInitialized: true,
161
242
  });
162
- const unsubscribeMember = stytch.member.onChange((member) => setMember({ member, fromCache: false, isInitialized: true }));
243
+ const unsubscribeMember = stytch.self.onChange((member) => setMember({ member, fromCache: false, isInitialized: true }));
163
244
  const unsubscribeMemberSession = stytch.session.onChange((session) => setMemberSession({ session, fromCache: false, isInitialized: true }));
164
245
  return () => {
165
246
  unsubscribeMember();
@@ -232,4 +313,4 @@ const StytchB2B = ({ styles, callbacks, config }) => {
232
313
  return React.createElement("div", { ref: containerEl });
233
314
  };
234
315
 
235
- export { StytchB2B, StytchB2BProvider, useStytchB2BClient, useStytchMember, useStytchMemberSession, withStytchB2BClient, withStytchMember, withStytchMemberSession };
316
+ export { StytchB2B, StytchB2BProvider, useStytchB2BClient, useStytchIsAuthorized, useStytchMember, useStytchMemberSession, withStytchB2BClient, withStytchMember, withStytchMemberSession, withStytchPermissions };