Laravel 5.1: Auth::login() not persisting



我使用Laravel Socialite从Facebook认证用户。我正在获取用户数据,并能够将它们存储在我的数据库中。但是当我尝试在视图中使用Auth::check时,它返回false。虽然在Authenticate模块中它返回true。

use IlluminateContractsAuthGuard;
use LaravelSocialiteContractsFactory as Socialite;
use Auth;

class  Authenticate {
    /**
     * @var DbUserRepository
     */
    private $userRepo;
    /**
     * @var Socialite
     */
    private $socialite;
    /**
     * @var Guard
     */
    private $auth;

    /**
     * Constructor method for the class
     *
     * @param DbUserRepository $userRepo
     * @param Socialite $socialite
     * @param Guard $auth
     */
    function __construct(DbUserRepository $userRepo, Socialite $socialite, Guard $auth)
    {
        $this->userRepo = $userRepo;
        $this->socialite = $socialite;
        $this->auth = $auth;
    }
    /**
     * Method to authenticate
     *
     * @param $driver
     * @param $hasCode
     * @param AuthenticateUserListener $listener
     * @return SymfonyComponentHttpFoundationRedirectResponse
     */
    public function execute($driver, $hasCode, $listener)
    {   
        if( !$hasCode) return $this->getAuthorizationFirst($driver);
        $userData = $this->getSocialUser($driver);
        $user = $this->userRepo->findByEmailOrCreate($userData);
        $this->auth->login($user); 
        dd($this->auth->check()); //this returns true
        return $listener->redirectTo('/', $user);
    }

    /**
     * Authorization method if user does not have
     *
     * @param $driver
     * @return SymfonyComponentHttpFoundationRedirectResponse
     */
    public function getAuthorizationFirst($driver)
    {
        return $this->socialite->driver($driver)->redirect();
    }

    /**
     * Method to register the user using the form
     *
     * @param $userData
     * @param AuthenticateUserListener $listener
     * @return mixed
     */
    public function registerUser($userData,AuthenticateUserListener $listener)
    {
        $user = $this->userRepo->findByEmailOrCreate($userData);
        $this->auth->login($user,true);
        return  $listener->redirectTo('/',null);
    }

    /**
     * Method to logout the user
     *
     * @param AuthenticateUserListener $listener
     * @return mixed
     */
    public function logoutUser(AuthenticateUserListener $listener)
    {
        $this->auth->logout();
        return $listener->redirectTo('/','');
    }

    /**
     * Method to retrive the user data after authorisation
     *
     * @param $driver
     * @return LaravelSocialiteContractsUser
     */
    public function getSocialUser($driver)
    {
        return $this->socialite->driver($driver)->user();
    }
    public function checkUserIfExists($userData)
    {
        $user = $this->userRepo->findUserIfExists($userData);
        return $user;
    }
}

但考虑到:

<?php if(Auth::check()) { //**this returns false** ?>
    <div class="dropdown pull-right btn user-info user_profile_div">
      <button id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <img id="profile-pic" src="<?php echo url('img/saurabh.jpg') ?>"><span class="mobile-remove">&nbsp;</span>
        <span class="user-name mobile-remove">Saurabh</span><span class="mobile-remove">&nbsp;</span>
        <span class="caret"></span>
      </button>
      <ul class="profile_dropdown dropdown-menu profile_dropdown" role="menu" aria-labelledby="dLabel">
        <li><a href="<?php echo route('books.read') ?>">My Books</a></li>
        <li><a href="<?php echo route('users.show') ?>">View Profile</a></li>
        <li><a href="<?php echo route('users.edit') ?>">Edit Profile</a></li>
        <li><a href="<?php echo route('pages.index') ?>">Sign Out</a></li>
      </ul>
    </div>

"

首先查看....用户访问此页面的令牌和路由

  // OAuth One Providers
  $token = $user->token;
  $tokenSecret = $user->tokenSecret;

之后,您可以检查数据库中的主键是什么,并像这样使用主键…

protected $primaryKey = 'ID';

你可以像这样使用…

在BaseModel中扩展Eloquent,然后在我的用户模型中扩展BaseModel。因此无法访问任何受保护的属性,如primaryKey

我希望这对你有帮助…这个技巧对我来说是完美的.....

最新更新