question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom messages from guards

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
kamilmysliwieccommented, Oct 8, 2017

@nmabhinandan of course! I see there’s no example in the docs. Good point, maybe I should create one 🙂

3reactions
kamilmysliwieccommented, Oct 8, 2017

Hi @nmabhinandan, It’s easy. Instead of returning false, just throw a HttpException.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found