Skip to content

Instantly share code, notes, and snippets.

@System3-2
Created May 18, 2023 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save System3-2/d7d461e58e6c1f3d7dbf98cbad46b7de to your computer and use it in GitHub Desktop.
Save System3-2/d7d461e58e6c1f3d7dbf98cbad46b7de to your computer and use it in GitHub Desktop.
Get authenticated user strategy
import { ExecutionContext } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class CustomAuthGuard extends AuthGuard('strategyName') {
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
// Access the authentication strategy name or identifier
const strategy = super.getActiveStrategy(request);
// You can store the strategy name in the request object or use it directly here
request.strategy = strategy;
return super.canActivate(context);
}
}
@Controller('example')
export class ExampleController {
@UseGuards(CustomAuthGuard)
@Get('protected')
protectedRoute() {
// Access the strategy name from the request object
console.log(request.strategy);
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment