Comments (2)
You can implement it like this:
-
Pass functions to endpoint decorator
@httpPost('/', authenticateToken(), authorizeUser(['administrator']))
-
Function example
export const authenticateToken = (/* PARAMS HERE */) => {
return async (req: Request, res: Response, next: NextFunction) => {
const authHeader = req.headers.authorization;
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return next(new AppError(HttpStatusCode.UNAUTHORIZED, 'error', 'No token'));
}
verify(token, process.env.ACCESS_TOKEN_SECRET, (error, user) => {
if (error) {
console.error(error);
next(new AppError(HttpStatusCode.FORBIDDEN, 'error', 'Invalid or expired token'));
}
req.body.user = user?.user as IJwtUserPayload;
next();
});
};
};
from inversify-express-utils.
You can implement it like this:
- Pass functions to endpoint decorator
@httpPost('/', authenticateToken(), authorizeUser(['administrator']))
- Function example
export const authenticateToken = (/* PARAMS HERE */) => { return async (req: Request, res: Response, next: NextFunction) => { const authHeader = req.headers.authorization; const token = authHeader && authHeader.split(' ')[1]; if (!token) { return next(new AppError(HttpStatusCode.UNAUTHORIZED, 'error', 'No token')); } verify(token, process.env.ACCESS_TOKEN_SECRET, (error, user) => { if (error) { console.error(error); next(new AppError(HttpStatusCode.FORBIDDEN, 'error', 'Invalid or expired token')); } req.body.user = user?.user as IJwtUserPayload; next(); }); }; };
I don't think it's possible with BaseMiddleware
, so that you have access to http context, right?
from inversify-express-utils.
Related Issues (20)
- Error trying to instantiate controller in a test (Error: No matching bindings found for serviceIdentifier: Symbol(HttpContext)) HOT 1
- inversify-express-utils, support of express 4.17.1 HOT 1
- [Inversify Express Utils] Controller is Reconstructed on Every Request HOT 1
- Inversify-Express-Untils Cannot Build Multiple Servers HOT 1
- Is inversify-express-utils no longer under maintenance? HOT 1
- integrating inversify-express-utils with inversify-socket-utils
- inversify-express-utils: BaseHttpController's conflict should take an argument
- [inversify-express-utils] AuthProvider Unhandled Promise Rejection HOT 1
- [inversify-express-utils] allow to inject dependencies into middleware constructor HOT 2
- inversify-socket-io-utils - new project submission HOT 1
- Contextual injection not working with express utils decorators (inversify-express-utils) HOT 1
- [inversify-express-utils] Nesting controllers and maintaining route context HOT 2
- Handling singletons in creation of large plans HOT 3
- [inversify-express-utils]: Auth provider cannot rely on server-level middleware HOT 3
- [Feature Request] Add request, response, next to BaseHttpController
- Adding route config while creating server doesn't assign mount path to baseurl property of express request object
- JsonContent doesn't send correctly HOT 7
- Can not inject dependency in my CustomAuthProvider HOT 1
- Breaking changes from TypeScript update HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inversify-express-utils.