Complete the code to check if the user has the 'admin' role.
if (user.roles.includes([1])) { return <AdminPanel />; } else { return <AccessDenied />; }
The code checks if the user's roles include the string 'admin'. This is the correct role to allow admin access.
Complete the code to redirect users without the 'editor' role to the home page.
if (!user.roles.includes([1])) { router.push('/'); }
The code redirects users who do not have the 'editor' role. So the blank must be 'editor'.
Fix the error in the code that checks if the user has any role from a list.
const hasAccess = roles.some(role => user.roles.[1](role));The includes() method checks if the user's roles array contains the current role. This is the correct method to use here.
Fill both blanks to create a higher-order component that restricts access based on roles.
function withRole(Component, [1]) { return function Wrapped(props) { const user = useUser(); if (!user.roles.[2](role => [1].includes(role))) { return <AccessDenied />; } return <Component {...props} />; }; }
The first blank is the parameter name for allowed roles. The second blank is the array method some(), which checks if any user role is in allowedRoles.
Fill all three blanks to implement server-side role check in Next.js getServerSideProps.
export async function getServerSideProps(context) {
const session = await getSession(context);
if (!session || !session.user.roles.[1](role => role === [2])) {
return {
redirect: {
destination: [3],
permanent: false,
},
};
}
return { props: {} };
}The some() method checks if the user has the 'admin' role. If not, the user is redirected to '/login'.