
Security News
CISA Extends MITRE Contract as Crisis Accelerates Alternative CVE Coordination Efforts
CISA extended MITRE’s CVE contract by 11 months, avoiding a shutdown but leaving long-term governance and coordination issues unresolved.
@nestjs-hybrid-auth/github
Advanced tools
Implement github authentication in your NestJS application.
npm install @nestjs-hybrid-auth/github --save
OR
yarn add @nestjs-hybrid-auth/github
The package exports mainly a dynamic module and guard. The module should be imported in your app.module.ts and guards should be used on the route handlers of any controller.
Want to jump directly to the available options?
If you just want to provide the static values or have them handy, pass them as options to the forRoot
static method like below. The options object is type of GithubAuthModuleOptions
.
import { GithubAuthModule } from '@nestjs-hybrid-auth/github';
@Module({
imports: [
GithubAuthModule.forRoot({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
useFactory
to get the ConfigService injected.If you want to make use of nest's ConfigModule to get the auth configuration for a provider from .env
config files, use forRootAsync
static method. The options to this method are typeof GithubAuthModuleAsyncOptions
which accepts a useFactory
property. useFactory
is a function which gets the instances injected whatever has been provided in inject
array. You can use those instances to prepare and return the actual GithubAuthModuleOptions
object. ConfigService can be one of them as per your choice.
import { GithubAuthModule } from '@nestjs-hybrid-auth/github';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
expandVariables: true,
}),
GithubAuthModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
clientID: configService.get('GITHUB_CLIENT_ID'),
clientSecret: configService.get('GITHUB_CLIENT_SECRET'),
callbackURL: configService.get('GITHUB_CALLBACK_URL'),
}),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
useClass
to get your auth config from a classIf the useFactory
makes your app module bloated with a lot of boilerplate code, you can useClass
to provide an existing config provider class. The class must implement GithubAuthModuleOptionsFactory
interface and createModuleOptions
method. This method should return GithubAuthModuleOptions
object. Similar to useFactory
, whatever you provide in inject
array, it will get injected in the constructor of your class. Follow the example:
hybrid-auth.config.ts
import { ConfigService } from '@nestjs/config';
import {
GithubAuthModuleOptions,
GithubAuthModuleOptionsFactory,
} from '@nestjs-hybrid-auth/github';
@Injectable()
class HybridAuthConfig implements GithubAuthModuleOptionsFactory {
constructor(private configService: ConfigService) {}
createModuleOptions(): GithubAuthModuleOptions {
return {
clientKey: this.configService.get('GITHUB_CLIENT_ID'),
clientSecret: this.configService.get('GITHUB_CLIENT_SECRET'),
callbackURL: this.configService.get('GITHUB_CALLBACK_URL'),
};
}
}
app.module.ts
import { GithubAuthModule } from '@nestjs-hybrid-auth/github';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
expandVariables: true,
}),
GithubAuthModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useClass: HybridAuthConfig,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Once you have setup the module properly in module file, its time to configure your route handlers to make the user properly redirected to appropriate identity provider's login page. @nestjs-hybrid-auth/github
provides a guard and result interface to make it enabled.
Each route will have two variants. One is to redirect to social login page and the other is to collect the response such as access/refresh tokens and user profile etc. The result will be attached to Request
object's hybridAuthResult
property as shown in the example below.
import { UseGithubAuth, GithubAuthResult } from '@nestjs-hybrid-auth/github';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@UseGithubAuth()
@Get('auth/github')
loginWithGithub() {
return 'Login with Github';
}
@UseGithubAuth()
@Get('auth/github-login/callback')
githubCallback(@Request() req): Partial<GithubAuthResult> {
const result: GithubAuthResult = req.hybridAuthResult;
return {
accessToken: result.accessToken,
refreshToken: result.refreshToken,
profile: result.profile,
};
}
}
@nestjs-hybrid-auth/github
exports various decorators, interfaces and methods.
UseGithubAuth
is NestJS Guard
which hijacks your nest request and redirects users to the appropriate login page of your configured identity provider (github in this case). The same guard can be used on callback
route also as shown in the example above. In the callback route handler, the req: Request
object will have a property hybridAuthResult
which is an object of type GithubAuthResult
.
@UseGithubAuth(options: GithubAuthGuardOptions)
@Get('auth/github')
loginWithGithub() {
return 'Login with Github';
}
This is a simple object to be passed into UseGithubAuth
guard as shown in example above if you want to pass some extra parameters to query the github result. It can be left empty for default result.
This is the dynamic module which must be imported in your app's main module with forRoot
or forRootAsync
static methods whichever suits your need. Both will return a NestJS dynamic module.
interface GithubAuthModule {
forRoot(options: GithubAuthModuleOptions): DynamicModule;
forRootAsync(options: GithubAuthModuleAsyncOptions): DynamicModule;
}
If you are configuring your module with forRoot
static method, pass in the module options given below. They can be called the github passport strategy options also.
interface GithubAuthModuleOptions {
authorizationURL?: string | undefined;
tokenURL?: string | undefined;
clientID: string;
clientSecret: string;
callbackURL?: string | undefined;
customHeaders?: OutgoingHttpHeaders | undefined;
scope?: string | string[] | undefined;
scopeSeparator?: string | undefined;
sessionKey?: string | undefined;
store?: oauth2.StateStore | undefined;
state?: string | undefined;
userAgent?: string | undefined;
userProfileURL?: string | undefined;
}
If you want to configure the GithubAuthModule
dynamically having the config or other services injected, pass in async options in the forRootAsync
static method. Please refer to the example above for useFactory
and useClass
properties.
interface GithubAuthModuleAsyncOptions {
useExisting?: Type<GithubAuthModuleOptionsFactory>;
useClass?: Type<GithubAuthModuleOptionsFactory>;
useFactory?: (
...args: any[]
) => Promise<GithubAuthModuleOptions> | GithubAuthModuleOptions;
inject?: any[];
}
interface GithubAuthModuleOptionsFactory {
createModuleOptions():
| Promise<GithubAuthModuleOptions>
| GithubAuthModuleOptions;
}
If you still have trouble setting up the workflow properly, please file an issue at Issues page.
FAQs
NestJS github authentication using passport
The npm package @nestjs-hybrid-auth/github receives a total of 7 weekly downloads. As such, @nestjs-hybrid-auth/github popularity was classified as not popular.
We found that @nestjs-hybrid-auth/github demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA extended MITRE’s CVE contract by 11 months, avoiding a shutdown but leaving long-term governance and coordination issues unresolved.
Product
Socket's Rubygems ecosystem support is moving from beta to GA, featuring enhanced security scanning to detect supply chain threats beyond traditional CVEs in your Ruby dependencies.
Research
The Socket Research Team investigates a malicious npm package that appears to be an Advcash integration but triggers a reverse shell during payment success, targeting servers handling transactions.