@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 +22 -0
- package/dist/b2b/index.d.ts +46 -3
- package/dist/b2b/index.esm.d.ts +46 -3
- package/dist/b2b/index.esm.js +84 -3
- package/dist/b2b/index.headless.esm.js +1758 -1032
- package/dist/b2b/index.headless.js +1758 -1032
- package/dist/b2b/index.js +85 -2
- package/package.json +3 -3
package/dist/b2b/index.js
CHANGED
|
@@ -97,6 +97,47 @@ const useStytchMemberSession = () => {
|
|
|
97
97
|
invariant(useIsMounted__INTERNAL(), noProviderError('useStytchMemberSession', 'StytchB2BProvider'));
|
|
98
98
|
return React.useContext(StytchMemberSessionContext);
|
|
99
99
|
};
|
|
100
|
+
/**
|
|
101
|
+
* Determines whether the logged-in member is allowed to perform the specified action on the specified resource.
|
|
102
|
+
* Returns `true` if the member can perform the action, `false` otherwise.
|
|
103
|
+
*
|
|
104
|
+
* If the member is not logged in, this method will always return false.
|
|
105
|
+
* If the resource or action provided are not valid for the configured RBAC policy, this method will return false.
|
|
106
|
+
*
|
|
107
|
+
* Remember - authorization checks for sensitive actions should always occur on the backend as well.
|
|
108
|
+
* @example
|
|
109
|
+
* const isAuthorized = useStytchIsAuthorized<Permissions>('documents', 'edit');
|
|
110
|
+
* return <button disabled={!isAuthorized}>Edit</button>
|
|
111
|
+
*/
|
|
112
|
+
const useStytchIsAuthorized = (resourceId, action) => {
|
|
113
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('useStytchIsAuthorized', 'StytchB2BProvider'));
|
|
114
|
+
const client = useStytchB2BClient();
|
|
115
|
+
const { session } = useStytchMemberSession();
|
|
116
|
+
const [isAuthorized, setIsAuthorized] = useAsyncState({
|
|
117
|
+
isInitialized: false,
|
|
118
|
+
fromCache: false,
|
|
119
|
+
isAuthorized: false,
|
|
120
|
+
});
|
|
121
|
+
React.useEffect(() => {
|
|
122
|
+
if (isStytchSSRProxy(client)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
setIsAuthorized({
|
|
126
|
+
isInitialized: true,
|
|
127
|
+
fromCache: true,
|
|
128
|
+
isAuthorized: client.rbac.isAuthorizedSync(resourceId, action),
|
|
129
|
+
});
|
|
130
|
+
}, []);
|
|
131
|
+
React.useEffect(() => {
|
|
132
|
+
if (isStytchSSRProxy(client)) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
client.rbac.isAuthorized(resourceId, action).then((isAuthorized) => {
|
|
136
|
+
setIsAuthorized({ isAuthorized, fromCache: false, isInitialized: true });
|
|
137
|
+
});
|
|
138
|
+
}, [client, session === null || session === void 0 ? void 0 : session.roles, resourceId, action]);
|
|
139
|
+
return isAuthorized;
|
|
140
|
+
};
|
|
100
141
|
/**
|
|
101
142
|
* Returns the Stytch client stored in the Stytch context.
|
|
102
143
|
*
|
|
@@ -137,6 +178,46 @@ const withStytchMemberSession = (Component) => {
|
|
|
137
178
|
WithStytchSession.displayName = `withStytchMemberSession(${Component.displayName || Component.name || 'Component'})`;
|
|
138
179
|
return WithStytchSession;
|
|
139
180
|
};
|
|
181
|
+
/**
|
|
182
|
+
* Wrap your component with this HOC in order to receive the permissions for the logged-in member.
|
|
183
|
+
* Evaluates all permissions granted to the logged-in member.
|
|
184
|
+
* Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.
|
|
185
|
+
* Each boolean will be `true` if the member can perform the action, `false` otherwise.
|
|
186
|
+
*
|
|
187
|
+
* If the member is not logged in, all values will be false.
|
|
188
|
+
*
|
|
189
|
+
* Remember - authorization checks for sensitive actions should always occur on the backend as well.
|
|
190
|
+
* @example
|
|
191
|
+
* type Permissions = {
|
|
192
|
+
* document: 'create' | 'read' | 'write
|
|
193
|
+
* image: 'create' | 'read'
|
|
194
|
+
* }
|
|
195
|
+
*
|
|
196
|
+
* const MyComponent = (props) => {
|
|
197
|
+
* const canEditDocuments = props.stytchPermissions.document.edit;
|
|
198
|
+
* const canReadImages = props.stytchPermissions.image.read;
|
|
199
|
+
* }
|
|
200
|
+
* return withStytchPermissions<Permissions>(MyComponent)
|
|
201
|
+
*/
|
|
202
|
+
const withStytchPermissions = (Component) => {
|
|
203
|
+
const WithStytchPermissions = (props) => {
|
|
204
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('useRBACPermissions', 'StytchB2BProvider'));
|
|
205
|
+
const client = useStytchB2BClient();
|
|
206
|
+
const { session } = useStytchMemberSession();
|
|
207
|
+
const [permissions, setPermissions] = useAsyncState({ loaded: false, value: null });
|
|
208
|
+
React.useEffect(() => {
|
|
209
|
+
client.rbac
|
|
210
|
+
.allPermissions()
|
|
211
|
+
.then((permissions) => setPermissions({ loaded: true, value: permissions }));
|
|
212
|
+
}, [client, session === null || session === void 0 ? void 0 : session.roles]);
|
|
213
|
+
if (!permissions.loaded) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
return React__default['default'].createElement(Component, Object.assign({}, props, { stytchPermissions: permissions.value }));
|
|
217
|
+
};
|
|
218
|
+
WithStytchPermissions.displayName = `withStytchPermissions(${Component.displayName || Component.name || 'Component'})`;
|
|
219
|
+
return WithStytchPermissions;
|
|
220
|
+
};
|
|
140
221
|
/**
|
|
141
222
|
* The Stytch Context Provider.
|
|
142
223
|
* Wrap your application with this component in `_app.js` in order to use Stytch everywhere in your app.
|
|
@@ -158,7 +239,7 @@ const StytchB2BProvider = ({ stytch, children }) => {
|
|
|
158
239
|
return;
|
|
159
240
|
}
|
|
160
241
|
setMember({
|
|
161
|
-
member: stytch.
|
|
242
|
+
member: stytch.self.getSync(),
|
|
162
243
|
fromCache: true,
|
|
163
244
|
isInitialized: true,
|
|
164
245
|
});
|
|
@@ -167,7 +248,7 @@ const StytchB2BProvider = ({ stytch, children }) => {
|
|
|
167
248
|
fromCache: true,
|
|
168
249
|
isInitialized: true,
|
|
169
250
|
});
|
|
170
|
-
const unsubscribeMember = stytch.
|
|
251
|
+
const unsubscribeMember = stytch.self.onChange((member) => setMember({ member, fromCache: false, isInitialized: true }));
|
|
171
252
|
const unsubscribeMemberSession = stytch.session.onChange((session) => setMemberSession({ session, fromCache: false, isInitialized: true }));
|
|
172
253
|
return () => {
|
|
173
254
|
unsubscribeMember();
|
|
@@ -243,8 +324,10 @@ const StytchB2B = ({ styles, callbacks, config }) => {
|
|
|
243
324
|
exports.StytchB2B = StytchB2B;
|
|
244
325
|
exports.StytchB2BProvider = StytchB2BProvider;
|
|
245
326
|
exports.useStytchB2BClient = useStytchB2BClient;
|
|
327
|
+
exports.useStytchIsAuthorized = useStytchIsAuthorized;
|
|
246
328
|
exports.useStytchMember = useStytchMember;
|
|
247
329
|
exports.useStytchMemberSession = useStytchMemberSession;
|
|
248
330
|
exports.withStytchB2BClient = withStytchB2BClient;
|
|
249
331
|
exports.withStytchMember = withStytchMember;
|
|
250
332
|
exports.withStytchMemberSession = withStytchMemberSession;
|
|
333
|
+
exports.withStytchPermissions = withStytchPermissions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stytch/nextjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "16.0.0",
|
|
4
4
|
"description": "Stytch's official Next.js Library",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.esm.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"author": "Stytch",
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@babel/runtime": "7.18.6",
|
|
25
|
-
"@stytch/vanilla-js": "
|
|
25
|
+
"@stytch/vanilla-js": "4.1.0",
|
|
26
26
|
"@testing-library/react": "14.0.0",
|
|
27
27
|
"eslint-config-custom": "0.0.1",
|
|
28
28
|
"react-dom": ">= 17.0.2",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"typescript": "4.7.4"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@stytch/vanilla-js": "^
|
|
34
|
+
"@stytch/vanilla-js": "^4.1.0",
|
|
35
35
|
"react": ">= 17.0.2",
|
|
36
36
|
"react-dom": ">= 17.0.2"
|
|
37
37
|
},
|