-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created github authentication mechanisms using Passport.js. References #17.
- Loading branch information
1 parent
2d2e260
commit bb050e5
Showing
6 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class AuthGithubGuard extends AuthGuard('github') { | ||
async canActivate(context: ExecutionContext) { | ||
const canActivate = (await super.canActivate(context)) as boolean; | ||
const request = context.switchToHttp().getRequest(); | ||
|
||
await super.logIn(request); | ||
return canActivate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Profile, Strategy } from 'passport-github2'; | ||
|
||
import { AuthService } from '../services/auth.service'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { UsernameUtil } from '../utils/auth.username.util'; | ||
import { configObject } from 'src/configuration'; | ||
|
||
@Injectable() | ||
export class AuthGithubStrategy extends PassportStrategy(Strategy) { | ||
constructor( | ||
private readonly authService: AuthService, | ||
private readonly usernameUtil: UsernameUtil, | ||
) { | ||
super(configObject.github); | ||
} | ||
|
||
validate( | ||
accessToken: string, | ||
refreshToken: string, | ||
profile: Profile, | ||
done: any, | ||
) { | ||
const { | ||
username: githubUsername, | ||
id: githubId, | ||
displayName: name, | ||
photos, | ||
} = profile; | ||
|
||
// As github usernames are not ensured to be unique, random characters are added. | ||
const username = this.usernameUtil.addRandomSequence(githubUsername); | ||
|
||
const avatar = photos[0].value; | ||
const userDetails = { username, githubId, name, avatar }; | ||
|
||
const user = this.authService.validateUser(userDetails); | ||
done(null, user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters