Created
May 18, 2023 09:14
-
-
Save System3-2/d7d461e58e6c1f3d7dbf98cbad46b7de to your computer and use it in GitHub Desktop.
Get authenticated user strategy
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
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