Skip to content

Instantly share code, notes, and snippets.

@Jerga99
Created May 14, 2021 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jerga99/9b3a1689a5fb58afcdadf4a2fb4ae434 to your computer and use it in GitHub Desktop.
Save Jerga99/9b3a1689a5fb58afcdadf4a2fb4ae434 to your computer and use it in GitHub Desktop.
export const isAuthorized = (user, role) => {
return user && user[process.env.AUTH0_NAMESPACE + '/roles'].includes(role);
};
export const authorizeUser = async (req, res) => {
const session = await auth0.getSession(req, res);
if (!session || !session.user) {
res.writeHead(302, {
Location: '/api/auth/login',
});
res.end();
return null;
}
return session.user;
};
export const withAuth = (getData) => async ({ req, res }) => {
const session = await auth0.getSession(req, res);
if (!session || !session.user) {
res.writeHead(302, {
Location: '/api/auth/login',
});
res.end();
return { props: {} };
}
const data = getData ? await getData({ req, res }, session.user) : {};
return { props: { user: session.user, ...data } };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment