-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-auth0-actions): Add authorization and fetch user's permissi…
…ons (#283)
- Loading branch information
1 parent
4cbb458
commit 69464ad
Showing
5 changed files
with
1,797 additions
and
1,708 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
import prisma from '../utils/database'; | ||
|
||
export const getUserPermissions = async (req: Request, res: Response) => { | ||
try { | ||
const { userId } = req.query; | ||
|
||
if (!userId) { | ||
return res.status(400).send('Invalid Request'); | ||
} | ||
|
||
const userPermissions = await prisma.user.findUnique({ | ||
where: { | ||
id: userId as string, | ||
}, | ||
select: { | ||
id: true, | ||
OrganizationUser: { | ||
select: { | ||
role: true, | ||
organizationId: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!userPermissions) { | ||
return res.status(404).send('User not found'); | ||
} | ||
|
||
return res.status(200).json({ userPermissions }); | ||
} catch (err) { | ||
console.error(err); | ||
return res.status(500); | ||
} | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
|
||
export const authorize = (req: Request, res: Response, next: NextFunction) => { | ||
const { authorization } = req.headers; | ||
|
||
if (!authorization) { | ||
return res.status(401).send('Unauthorized'); | ||
} | ||
|
||
const token = authorization.split(' ')[1]; | ||
|
||
if (!token) { | ||
return res.status(401).send('Unauthorized'); | ||
} | ||
|
||
if (token !== process.env.AUTHORIZATION_SECRET) { | ||
return res.status(401).send('Unauthorized'); | ||
} | ||
|
||
next(); | ||
}; |
Oops, something went wrong.