PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 28, 2022

[FIXED] Laravel 8 bearer token exception handling if bearer token not found

 February 28, 2022     api, bearer-token, laravel, php     No comments   

Issue

Hello I can't understand how to set error message when user put some wrong bearer token currently I am not getting any exceptional error message if I try wrong bearer token in postman.

public function general_information(Request $request) {
    try {
        return $token = $request->bearerToken();
    } catch(Exception $e) {
        return $e->message();
    }
}

Solution

in app/Exceptions/Handler.php add this function and change the message as you want

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'haha'], 401);
    }

    return redirect()->guest('login');
}

here return haha message

note define this before write the function
Illuminate\Auth\AuthenticationException;



Answered By - Ossama Abd
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing