Skip to content

Commit

Permalink
use get permissions instead of mapping over individual permissions an…
Browse files Browse the repository at this point in the history
…d creating multiple asynchronous calls.
  • Loading branch information
yyaskriloff committed Apr 28, 2024
1 parent b74442d commit 3d2abc2
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/handlers/protect.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {redirect} from 'next/navigation';
const protectPage =
(page, config = {redirect: '/api/login', statusCode: 302}) =>
async (props) => {
const {isAuthenticated, getAccessToken, getPermission} = kinde();
const {isAuthenticated, getAccessToken, getPermission, getPermissions} =
kinde();
const isSignedIn = await isAuthenticated();

if (!isSignedIn) {
Expand All @@ -38,10 +39,12 @@ const protectPage =
}

if (Array.isArray(config.permissions)) {
const hasPermission = await Promise.all(
config.permissions.map((permission) => getPermission(permission))
);
if (!hasPermission.some((permission) => permission)) {
const permissions = await getPermissions();
if (
!config.permissions.some((permission) =>
permissions.includes(permission)
)
) {
return redirect(config.redirect, {statusCode});
}
}
Expand Down

0 comments on commit 3d2abc2

Please sign in to comment.