@universal-packages/express-controllers-session
v1.3.0
Published
Express session for universal controllers
Downloads
796
Readme
Express controllers session
Express session for universal controllers.
Install
npm install @universal-packages/express-controllers-session
npm install express
npm install @universal-packages/express-controllers
Global middleware
By installing this package a global middleware is going to be loaded automatically by the ExpressControllers
.
Global methods
initialize([options: ExpressSessionOptions])
Set up the global middleware session options before loading the ExpressControllers
.
import { initialize } from '@universal-packages/express-controllers-session'
import { RedisEngine } from '@universal-packages/universal-token-registry-redis'
initialize({ engine: 'redis', engineOptions: { host: 'localhost' } })
await expressControllers.prepare()
Options
Same options as express-session.
Decorators
@AuthenticateAction()
You can use @AuthenticateAction to quickly end request when no session is present at action level.
import { BaseController, Controller, Get, Post } from '@universal-packages/express-controllers'
import { AuthenticateAction } from '@universal-packages/express-controllers-session'
@Controller('good')
export default class GoodController extends BaseController {
@Get()
@AuthenticateAction()
async action() {
this.request.parameters
}
}
@AuthenticateController()
You can use @AuthenticateController to quickly end request when no session is present for every action in a controller.
import { BaseController, Controller, Get, Post } from '@universal-packages/express-controllers'
import { AuthenticateController } from '@universal-packages/express-controllers-session'
@Controller('good')
@AuthenticateController()
export default class GoodController extends BaseController {
@Get()
async action() {
this.request.parameters
}
}
Create your own
Creating your own decorator enables you to not only end the request but also make the request richer with additional data.
import { ActionUse, ControllerUse } from '@universal-packages/express-controllers'
async function authenticateRequest(request, response, next) {
if (request.session.authenticated) {
request.currentUser = await User.findById(request.session.userId)
next()
} else {
response.status(401).end()
}
}
export function AuthenticateAction() {
return ActionUse(authenticateRequest)
}
export function AuthenticateController() {
return ControllerUse(authenticateRequest)
}
Typescript
This library is developed in TypeScript and shipped fully typed.
Contributing
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.