danyal14's avatar

Authentication Middleware

Hi Guys,

I am running Luman first time to test framework, I made login action that return api_token on success.

Then api_token is passed for further requests to endpoint under auth middleware. I would like to return message of my choice upon 401, whereas authentication middleware from handle method return Unauthorized for any Unauthorized request.

How can I return custom message?

Thanks in advanced.

Still not solved

Regards.

0 likes
3 replies
munazzil's avatar

Use as like below in your controller.

public function __construct()
 {

 $this->middleware('auth');  

 }
danyal14's avatar

@MUNAZZIL - Thanks for the reply, implementing just constructor had no effect unless I remove following commented code from the Authentication middleware->handle

    public function handle($request, Closure $next, $guard = null)
    {
        // if ($this->auth->guard($guard)->guest()) {
        //    return response('Unauthorized.', 401);
        // }

        return $next($request);
    }
danyal14's avatar
danyal14
OP
Best Answer
Level 4

I still have a issue, even I add to controller constructor, then nothing happens.

According to my understanding

$this->middleware('auth');  

or

      // Routes requires auth
    $router->group(['middleware' => 'auth'], function () use ($router) {
        $router->get('auth/logout', 'User\AuthController@logout');

        $router->get('user/profile', 'User\UserController@profile');
    });

Both are equal and problem remain same, how to return custom unauthenticated messages.

Please or to participate in this conversation.