Custom messages from guards
See original GitHub issueI’ve a guard which returns false if the request doesn’t have correct jwt token or has expired jwt token. But I’m not being able to send the message back to notify why the request is 403 forbidden.
here’s my code
@Guard()
export class AuthGuard implements CanActivate {
canActivate(req: Request, context: ExecutionContext): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
if (req.headers.authorization && req.headers.authorization.toString().split(' ')[0] === 'Bearer') {
const token: string = req.headers.authorization.toString().split(' ')[1];
Database.Connection.then((connection: Connection) => {
const decoded = jwt.verify(token, 'secret');
connection.getRepository(User).findOneById(decoded.data).then((user: User) => {
(req as any).user = user;
resolve(true);
});
}).catch((err) => {
resolve(false);
});
} else {
resolve(false);
}
});
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
World Guard Custom Messages | BuiltByBit (MC-Market)
World Guard Custom Messages ... a file called messages.something and you can edit it there, every plugin's messages is editable i believe.
Read more >Guards | NestJS - A progressive Node.js framework
A guard is a class annotated with the @Injectable() decorator, ... Nest provides the ability to attach custom metadata to route handlers through...
Read more >World edit/guard text | SpigotMC - High Performance Minecraft
I don't think world guard has a way to customize those messages. At least not in the config. #4 CreepersplaysMC, Jul 17, 2017....
Read more >NestJS throw http exepction from guard (export as library ...
I'm making a nestjs library where I include a guard as part of ... new ForbiddenException('Invalid url ,with custom error message') } }...
Read more >How to customize the messages displayed to the user
To access this option, go to Login Guard module => User Messages. GASP Alert Text. You can customize the alert message displayed to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nmabhinandan of course! I see there’s no example in the docs. Good point, maybe I should create one 🙂
Hi @nmabhinandan, It’s easy. Instead of returning
false
, just throw aHttpException
.