danyal14's avatar

Authentication Middleware & custom responses

Hi Guys,

I am referring to my previous question, that is not actually solved but marked solved by mistaken. https://laracasts.com/discuss/channels/lumen/authentication-middleware

Since Lumen has Authenticate middleware and implements check on handle method.

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

        return $next($request);
    }

That's mean every routes lies under auth middleware, if unauthenticated will get same response message "Unauthorized", where as I want to return more details depending on which route triggered this error.

Routes:

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

$router->post('auth/login', 'User\AuthController@login');

What could be the possible solution?

0 likes
4 replies
martinbean's avatar

@danyal14 The route that triggers the error will be the route that you requested? Or am I missing something?

danyal14's avatar

@MARTINBEAN - suppose I am trying to hit auth/verify with invalid api_token, Authenticate middleware with react and respond with "Unauthorized", and same goes with logout and profile endpoint.

This is when I want to respond with different messages.

martinbean's avatar
Level 80

@DANYAL14 - Right. So if you hit auth/verify, and it responds with Unauthorized, it’s the `auth/veri route you’re not allowed to receive.

This is how HTTP works: you send a request, you get a response.

danyal14's avatar

@MARTINBEAN - Of course, I am off today :) that's correct request is not going any further since middleware is checking api_token.

Thanks

Please or to participate in this conversation.