You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the process of ensuring the functionality of my authentication middleware, I have written two pieces of code that you may find interesting for your own usage in cognito-express's development:
An e2e test suite that tests the basic use-cases for token validation
A quick & dirty utility script used to force reset the password of a user created via the Cognito User Pool interface
For context, my authenticationMiddleware is implemented as follows:
functionauthenticationMiddleware(poolOptions=null){// Initializing CognitoExpress constructorconstcognitoExpress=newCognitoExpress(typeofpoolOptions==='object'&&poolOptions!==null
? poolOptions
: {region: process.env.COGNITO_REGION,cognitoUserPoolId: process.env.COGNITO_USERPOOL_ID,tokenUse: 'access',// Possible Values: access | idtokenExpiration: parseInt(process.env.COGNITO_TOKEN_EXPIRATION,10),});cognitoExpress.validate=util.promisify(cognitoExpress.validate);returnasyncfunctioninnerAuthenticationMiddleware(req,res,next){// I'm passing in the access token in header under key accessTokenconstauthJwtToken=req.headers.Authorization;// Fail if token not present in header.if(!authJwtToken){returnres.status(401).send('Access Token missing from header');}try{constauthResponse=awaitcognitoExpress.validate(authJwtToken);// API has been authenticated. Proceed.req.locals.user=authResponse;next();}catch(err){// If API is not authenticated, Return 401 with error message.returnres.status(401).send(err);}};}
Hope that these can be of use!
Have a great day 🚀
Cheers,
Phil
The text was updated successfully, but these errors were encountered:
Hi!
In the process of ensuring the functionality of my authentication middleware, I have written two pieces of code that you may find interesting for your own usage in
cognito-express
's development:For context, my authenticationMiddleware is implemented as follows:
Hope that these can be of use!
Have a great day 🚀
Cheers,
Phil
The text was updated successfully, but these errors were encountered: