-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #506 from project-violet/discrod-auth
Discord oauth2 login
- Loading branch information
Showing
13 changed files
with
258 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,28 @@ | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { Profile, Strategy } from 'passport-discord'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class DiscordStrategy extends PassportStrategy(Strategy, 'discord') { | ||
constructor(private readonly configService: ConfigService) { | ||
super({ | ||
clientID: configService.get<string>('DISCORD_CLIENT_ID'), | ||
clientSecret: configService.get<string>('DISCORD_CLIENT_SECRET'), | ||
callbackURL: 'http://localhost:3000/api/v2/auth/discord/redirect', | ||
scope: ['identify'], | ||
}); | ||
} | ||
|
||
async validate(accessToken: string, refreshToken: string, profile: Profile) { | ||
try { | ||
const { id: discordId, avatar } = profile; | ||
return { | ||
discordId: discordId, | ||
avatar: avatar, | ||
}; | ||
} catch (error) { | ||
throw new UnauthorizedException(error); | ||
} | ||
} | ||
} |
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,26 @@ | ||
import { | ||
ExecutionContext, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class DiscordAuthGuard extends AuthGuard('discord') { | ||
constructor() { | ||
super({ | ||
property: 'discord', | ||
}); | ||
} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
return (await super.canActivate(context)) as boolean; | ||
} | ||
|
||
handleRequest(err: any, user: any) { | ||
if (err || !user) { | ||
throw err || new UnauthorizedException('Retry login'); | ||
} | ||
return 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export class JwtPayload { | ||
userAppId: string; | ||
discordId?: string; | ||
id: string; | ||
refreshToken?: string; | ||
} |
Oops, something went wrong.