mardi 7 février 2017

Auth::check() return false after refresh or redirect page

here is my Route

Route::group(['middleware' => ['web']], function(){
    Route::get('/', 'IndexController@index');
    Route::get('/play/{game_id}', 'PlayController@play');
});

here is my kernel.php

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

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

Here is my play method that i am checking Authentication if not Login yet, it will create or fetch user from database and call Auth::login()

public function play($game_id)
    {
        $this->game_id_ = $game_id;

        if(!Auth::check())
        {
            echo "Not login yet.";
            $user = "username";
            $name = "password";
            $id = "1234555";
            $email = "email@email.com";
            $avatar = "pic.png";
            // echo $avatar;
            (new SocialAuthController)->facebookLoginAndUpdateData($name, $id, $email, $avatar);
        }
    }

It's calling Auth::login($user, true) in facebookLoginAndUpdateData()

public function facebookLoginAndUpdateData($name, $email, $facebook_id, $avatar)
    {
        $authUser = $this->findOrCreateUser($name, $email, $facebook_id, $avatar);
        Auth::login($authUser, true);
        return $authUser;
    }

    private function findOrCreateUser($name, $email, $facebook_id, $avatar)
    {
        $authUser = User::where('facebook_id', $facebook_id)->first();

        if ($authUser){
            return $authUser;
    }

    return User::create([
        'name' => $name,
        'email' => $email,
        'facebook_id' => $facebook_id,
        'avatar' => $avatar
    ]);
  }

The problem is, for first time that call play() it create new row in database.

After that , i have refresh this page but Auth::check() still return false although i have call Auth::login($user ,true) on first time,

It should remember that i already logged in and Auth::check() should return true, I have google it for 4 hours but i still can't fix it.

Anyone have the same problem, please help . Thank you.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jYLbvq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire