Created
May 14, 2021 08:36
-
-
Save Jerga99/9b3a1689a5fb58afcdadf4a2fb4ae434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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