I'm working with nest.js as well as it's passport module and passport-discord to handle discord oauth2 authentication.
However, I want to pass the request object from my login route through the authentication guard for use later down the line where I check some of the data, and then will want to invoke the logOut()
method on the request object in some situations.
Controller:
@Get('login')
@UseGuards(DiscordAuthGuard)
login() {
console.log('hello123');
}
Guard:
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
export class DiscordAuthGuard extends AuthGuard('discord') {
async canActivate(context: ExecutionContext) {
const activate = (await super.canActivate(context)) as boolean;
const request = context.switchToHttp().getRequest();
await super.logIn(request);
return activate;
}
}
Discord Strategy:
import { Inject } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Profile, Strategy } from 'passport-discord';
import { IAuthService } from '../interfaces/auth';
export class DiscordStrategy extends PassportStrategy(Strategy) {
constructor(
@Inject('AUTH_SERVICE') private readonly authService: IAuthService,
) {
super({
clientID: 'REDACTED',
clientSecret: 'REDACTED',
callbackURL: 'http://localhost:4000/api/auth/redirect',
scope: ['identify'],
});
}
async validate(accessToken: string, refreshToken: string, profile: Profile) {
console.log('DiscordStrategy Validate Method');
console.log(profile);
// I WANT TO BE ABLE TO GET THE REQUEST OBJECT TO HERE, SO THAT I CAN PASS IT INTO MY AUTH SERVICE
return this.authService.validateUser({ discordId: profile.id });
}
}
Any idea how I would achieve this?
Try passing passReqToCallback
parameter to super in constructor:
constructor(
@Inject('AUTH_SERVICE') private readonly authService: IAuthService,
) {
super({
clientID: 'REDACTED',
clientSecret: 'REDACTED',
callbackURL: 'http://localhost:4000/api/auth/redirect',
scope: ['identify'],
passReqToCallback: true,
});
}
and modify the validate function to have signature
async validate(req: Request, accessToken: string, refreshToken: string, profile: Profile)