Express session for universal controllers.
npm install @universal-packages/express-controllers-session
npm install express
npm install @universal-packages/express-controllers
By installing this package a global middleware is going to be loaded automatically by the ExpressControllers
.
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()
Same options as express-session.
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
}
}
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
}
}
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)
}
This library is developed in TypeScript and shipped fully typed.
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.