-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(express): Update playground to use correct error handlers (#4167)
- Loading branch information
1 parent
9ccea9b
commit 4a9f6d8
Showing
3 changed files
with
30 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
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 |
---|---|---|
@@ -1,23 +1,39 @@ | ||
import type { Response } from 'express'; | ||
|
||
import { getAuth, requireAuth } from '@clerk/express'; | ||
import { Router, Request as ExpressRequest } from 'express'; | ||
import { getAuth, requireAuth, UnauthorizedError, ForbiddenError } from '@clerk/express'; | ||
import { | ||
Router, | ||
Request as ExpressRequest, | ||
Response as ExpressResponse, | ||
NextFunction | ||
} from 'express'; | ||
|
||
const router = Router(); | ||
|
||
router.use(requireAuth); | ||
|
||
router.get('/me', async (req: ExpressRequest, reply: Response) => { | ||
return reply.json({ auth: getAuth(req) }); | ||
router.get('/me', async (req: ExpressRequest, res: ExpressResponse) => { | ||
return res.json({ auth: getAuth(req) }); | ||
}); | ||
|
||
router.get('/private', async (req, reply: Response) => { | ||
router.get('/admin-only', async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => { | ||
const auth = getAuth(req); | ||
if (!auth.userId) { | ||
return reply.status(403).end(); | ||
|
||
if (!auth.has({ role: 'admin' })) { | ||
return next(new ForbiddenError()); | ||
} | ||
|
||
return reply.json({ hello: 'world', auth }); | ||
}); | ||
return res.json({ message: 'Hello admin!', auth }); | ||
}) | ||
|
||
router.use((err: Error, _req: ExpressRequest, res: ExpressResponse, next: NextFunction) => { | ||
if (err instanceof UnauthorizedError) { | ||
return res.status(401).json({ error: 'Unauthorized' }); | ||
} | ||
|
||
if (err instanceof ForbiddenError) { | ||
return res.status(403).json({ error: 'Forbidden' }); | ||
} | ||
|
||
return next(err) | ||
}) | ||
|
||
export const privateRoutes = router; |