0.0.7 • Published 2 years ago

nestjs-oauthclient v0.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

NestJS OAuthClient

About

Collection of NestJS modules for OAuth 2.0

Features

  • Simple. Flexible. Easy to use.
  • Perform OAuth 2.0 actions like
    • Grant access token
    • Verify access token
    • Refresh access token
    • Revoke tokens

Packages

PackageDescriptionVersion
nestjs-oauthclientCollection of OAuth 2.0 modulesversion

Installation

Install via npm/yarn/pnpm

npm i nestjs-oauthclient

Usage

  • Register OauthModule for your provider, e.g. DiscordOauthModule
import { DiscordOauthModule } from 'nestjs-oauthclient';

@Module({
  imports: [
    DiscordOauthModule.register({
      isGlobal: true,
      client: {
        id: 'your-client-id',
        secret: 'your-client-secret',
      },
    }),
  ],
})
export class AppModule {}

.registerAsync is also available for async configuration

  • Use OauthService from your OAuth provider, e.g. DiscordOauthService
import { DiscordOauthService } from 'nestjs-oauthclient';

@Controller('/oauth/discord')
export class AppController {
  constructor(private readonly discordOauth: DiscordOauthService) {}

  @Get('/')
  getUrl() {
    return this.discordOauth.authorizeURL({
      redirectUri: 'http://localhost:3000/oauth/discord/callback', //required
      state: 'some-random-state', // optional,
      scopes: ['identify', 'guilds'], // optional
    });
  }

  @Get('/callback')
  getToken(@Param('code') code: string, @Param('state') state?: string) {
    // Custom logic to verify state

    // Generate Token
    return this.discordOauth.getToken({
      code,
      scope: ['identify', 'guilds'],
      redirectUri: 'http://localhost:3000/oauth/discord/callback',
    });
  }
}

Contribute

Contributions welcome! Read the contribution guidelines first.

License

MIT License

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago