8

I'm using the basic HTTP authentication provided in Laravel to log in to my website. However, when I call Auth::Check() I always get false as the response even though I am logged in.

Does Auth::Check() not work with the basic authentication model and if not, is there any way to check the basic authentication to see if a user is logged in?

This is my user class:

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }
}

This is the segment of code where I set the authentication filter to use

$this->middleware('auth.basic', ['only' => ['create', 'store', 'edit', 'update', 'destroy']]);

And this is my Auth::Check() call (Always prints 0):

public function show($id)
{
    echo \Auth::check() ? '1' : '0';
    die();
    #.......
}

My routes: Routes

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Liam Potter
  • 1,732
  • 8
  • 24
  • 47
  • Did you follow the steps from https://laravel.com/docs/5.1/authentication#http-basic-authentication ? – Mahmoud Tantawy Jan 17 '16 at 21:02
  • Auth::check is exactly what is used, could you post your code? – UX Labs Jan 17 '16 at 21:20
  • @LiamPotter Looks like you already check for authentication using middleware. Why are you checking it again in the controller? If you're not authenticated, Laravel won't let you inside the controller method. – parrker9 Jan 18 '16 at 11:13
  • @parrker9 that's just for checking that a value is being returned when the method is called. What I want to use Auth::Check for is to output certain HTML elements if the check passes in my view code. – Liam Potter Jan 18 '16 at 11:50
  • can you also add the routes? so far your code seems right – UX Labs Jan 18 '16 at 19:44
  • @Yousef I've added a picture of the output from 'php artisan route:list'. – Liam Potter Jan 18 '16 at 20:36

1 Answers1

3

It changed in 5.2 version.

If you will use session, csrf, cookie ext. you should to use "web" middleware like this in your routes:

Route::group(['middleware' => ['web']], function () {
//
});

And you can see in your project the new kernel.php file is like this:

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
],

'api' => [
    'throttle:60,1',
],
];

More info: https://laravel.com/docs/5.2/releases

Erkan Özkök
  • 895
  • 12
  • 25
  • When I route a controller and view via web middleware it does not hit at all Authenticate.php and it allows to open the controller without user being logged in and authenticated. I use database sessions, but I've also tried files with same results. – mal Mar 23 '16 at 13:08
  • It did not work for me until I've placed a route with "auth" middleware inside "web" route group as: Route::group(['middleware' => ['web']], function () { Route::get('/dashboard', ['middleware' => 'auth', 'uses' => 'dashboard@index']); }); – mal Mar 23 '16 at 13:26