TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published


NPM version Build Status

Complete, compliant and well tested module for implementing an OAuth2 server with Nest in Node.js.

This is the Nest module wrapper for oauth2-server.


To begin using it, we first install the required dependencies.

$ npm install --save nest-oauth2-server oauth2-server
$ npm install --save-dev @types/oauth2-server

Getting started

Once the installation process is complete, we can import the OAuth2ServerModule into the root AppModule.

import { Module } from '@nestjs/common';
import { OAuth2ServerModule } from 'nest-oauth2-server';
import { model } from './model';

  imports: [
      model: model
export class AppModule {}

The forRoot() method accepts the same configuration object to create a new OAuth2Server instance.

Note that OAuth2Server requires a model object through which some aspects or storage, retrieval and custom validation are abstracted. Therefore, in most cases you will need to use async configuration to import your repository module for the model implementation.

The model specification see documentation for details.


The module provides decorators to help you create OAuth2Server handlers (endpoints).

Decorator OAuth2Server handler
@OAuth2ServerAuthenticate(options?: AuthenticateOptions) OAuth2Server#authenticate()
@OAuth2ServerAuthorize(options?: AuthorizeOptions) OAuth2Server#authorize()
@OAuth2ServerToken(options?: TokenOptions) OAuth2Server#token()

Any valid option for @OAuth2ServerAuthenticate(), @OAuth2ServerAuthorize() and @OAuth2ServerToken() can be passed to the OAuth2ServerModule.forRoot() method as well. The supplied options will be used as default for the other methods.

In addition, we provide the @OAuth2ServerOAuth() decorator lets you retrieve oauth information from the res.locals.oauth property.

The following is an example controller for oauth2 server endpoints:

import { Controller, Get, Post } from '@nestjs/common';
import { OAuth2ServerAuthenticate, OAuth2ServerAuthorize, OAuth2ServerToken, OAuth2ServerOAuth, OAuth } from 'nest-oauth2-server';

export class OAuthController {
  user(@OAuth2ServerOAuth() oauth: OAuth) {
    return oauth.token.user;

  authorize() {}

  token() {}

Async configuration

When you need to pass module options asynchronously instead of statically, use the forRootAsync() method. As with most dynamic modules, Nest provides several techniques to deal with async configuration.

One technique is to use a factory function:

  useFactory: () => ({
    model: model,

Like other factory providers, our factory function can be async and can inject dependencies through inject.

  imports: [OAuthModule],
  useFactory: async (model: OAuth2ServerModel) => ({
    model: model
  inject: [OAuth2ServerModel],

Alternatively, you can configure the OAuth2ServerModule using a class instead of a factory, as shown below.

  useClass: OAuth2ServerConfigService,

The construction above instantiates OAuth2ServerConfigService inside OAuth2ServerModule, using it to create an options object. Note that in this example, the OAuth2ServerConfigService has to implement OAuth2ServerOptionsFactory interface as shown below. The OAuth2ServerModule will call the createOAuth2ServerOptions() method on the instantiated object of the supplied class.

class OAuth2ServerConfigService implements OAuth2ServerOptionsFactory {
  constructor(private readonly model: OAuth2ServerModel) {}

  createOAuth2ServerOptions(): OAuth2ServerModuleOptions {
    return {
      model: this.model,

If you want to reuse an existing options provider instead of creating a private copy inside the OAuth2ServerModule, use the useExisting syntax.

  imports: [ConfigModule],
  useExisting: OAuth2ServerConfigService,


A working example is available in test directory.



Package Sidebar


Weekly Downloads






Unpacked Size

133 kB

Total Files


Last publish


  • chunkai1312