Auth::guest() always returns true | Laravel 5.4

Asked

Viewed 629 times

1

I’m using the features offered by Standard for authentication, but even logged in the Auth::guest() command returns true, as if you were a guest.

Follow the code of mine middleware

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class Autorizacao
{
     /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(!$request->is('/') && !$request->is('home') && !$request->is('login') && !$request->is('logout') && !$request->is('register') && Auth::guest()){
            if(Auth::guest())
                echo 'true';
            else
                echo 'false';
            //return redirect('/login');
        }
        return $next($request);
    }
}

I didn’t use dd() because it closes the process to display the result, and may not be able to log in.

In this case I would like the login screen to be displayed if the user was not logged in, but could access the login/logout or home routes, but if logged in would have access to all screens.

  • what dd(Auth::user()); returns?

  • There’s redundancy in the code, it’s calling Auth::guest() twice

  • @Guilhermenascimento returns null

  • Even when logged in?

  • @Guillhermenascimento yes

1 answer

0

I managed to solve otherwise, I stopped using the middleware and added to my constructor controllers being:

public function __construct(){
    $this->middleware('auth');
}

In each controller.

Thank you for your cooperation.

  • 1

    Instead of declaring on each controller, you can use a group of routes and declare middleware on Routeserviceprovider.php

  • @Tiger could give an example of how it would look this way?

  • Well explained in this Issue in the group of Larable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.