-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bffc135
commit 221f747
Showing
4 changed files
with
136 additions
and
33 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
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,40 @@ | ||
import { ClerkExpressWithAuth } from '@clerk/clerk-sdk-node'; | ||
import dotenv from 'dotenv'; | ||
import type { WithAuthProp } from '@clerk/clerk-sdk-node'; | ||
import type { Request, Response, NextFunction } from 'express'; | ||
dotenv.config(); | ||
|
||
/** | ||
* Middleware function to validate user authentication. | ||
* If the environment is not test, it uses ClerkExpressWithAuth to validate the user's session. | ||
* If the user is authenticated, it calls the next middleware function. | ||
* If the user is not authenticated, it returns a 403 status code. | ||
* If the environment is test, it calls the next middleware function without authentication. | ||
* | ||
* @param req - The Express request object. | ||
* @param res - The Express response object. | ||
* @param next - The next middleware function. | ||
*/ | ||
const validateAuth = ( | ||
req: WithAuthProp<Request>, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
// If the environment is not test, use ClerkExpressWithAuth to validate the user's session | ||
if (process.env.ENVIRONMENT !== 'test') { | ||
// Use ClerkExpressWithAuth to validate the user's session then call next() if the user is authenticated | ||
ClerkExpressWithAuth({})(req, res, async () => { | ||
if (req.auth.sessionId && req.auth.userId) { | ||
return next(); | ||
} | ||
|
||
// If the user is not authenticated, return a 403 status code | ||
return res.status(401).send('Unauthorized'); | ||
}); | ||
} else { | ||
// If the environment is test, call next() to continue (no authentication is required in test environment) | ||
return next(); | ||
} | ||
}; | ||
|
||
export default validateAuth; |
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