@icar/atenea-lib
v1.6.2
Published
session/scopes middleware for atenea
Downloads
42
Readme
🔐 ATENEA LIB
a simple Atenea auth middleware for node js
Usage
You can create a middleware to validate session and scopes
function authMiddleware(scopes: Array<string>){
return async function (
req: Request,
res: Response,
next: NextFunction){
const bearerToken = req.headers.authorization? || ''
const token = bearerToken.replace('Bearer ', '')
const atenea = new Atenea(token);
// Check if request authorization token is valid
const validToken: Boolean = await atenea.verifyToken()
if(!validToken){
const error = new Error('invalid or expired token');
next(error);
}
// Check if user has access to this resource
const validScope: Boolean = await atenea.verifyScope(scopes)
if(!validScope){
const error = new Error('access denied');
next(error);
}
next();
}
}
export default authMiddleware;then use it on routes
route.get('/api/v1/cats',
authMiddleware(['fake.scope.r']),
async (req: Request, res: Response, next: NextFunction) => {
...
}you can also retrive token info
req.body.user_id = atenea.tokenInfo?.company_uid