@universal-packages/express-controllers-session
TypeScript icon, indicating that this package has built-in type declarations

1.2.6 • Public • Published

Express controllers session

npm version Testing codecov

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.authenticatableId)

    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.

License

MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i @universal-packages/express-controllers-session

Weekly Downloads

70

Version

1.2.6

License

MIT

Unpacked Size

13.1 kB

Total Files

15

Last publish

Collaborators

  • omarandstuff