简体   繁体   English

Laravel 5.1:Auth :: login()不持久

[英]Laravel 5.1: Auth::login() not persisting

I am using Laravel Socialite to authenticate user from Facebook. 我正在使用Laravel Socialite从Facebook验证用户身份。 I am getting user data and able to store them in my database. 我正在获取用户数据并将其存储在数据库中。 But when I try to use Auth::check in my view it returns false. 但是,当我尝试在视图中使用Auth :: check时,它返回false。 Though in Authenticate module it's returning true. 尽管在Authenticate模块中,它返回true。

use Illuminate\Contracts\Auth\Guard;
use Laravel\Socialite\Contracts\Factory 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 \Symfony\Component\HttpFoundation\RedirectResponse
     */
    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 \Symfony\Component\HttpFoundation\RedirectResponse
     */
    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 \Laravel\Socialite\Contracts\User
     */
    public function getSocialUser($driver)
    {
        return $this->socialite->driver($driver)->user();
    }

    public function checkUserIfExists($userData)
    {
        $user = $this->userRepo->findUserIfExists($userData);

        return $user;
    }
}

But in view: 但鉴于:

` `

<?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>

` `

First of all you check ....token of user come on this page And Route 首先您检查..用户令牌在此页面上并路由

  // OAuth One Providers

  $token = $user->token;
  $tokenSecret = $user->tokenSecret;

After that you can check what is primary key in your databse and use the primary key like that.. 之后,您可以检查数据库中的主键,然后像这样使用主键。

protected $primaryKey = 'ID';

You can use this like that.. 您可以这样使用。

Extending Eloquent in BaseModel and then extening BaseModel in my user model. 在BaseModel中扩展Eloquent,然后在我的用户模型中扩展BaseModel。 so was not able to Access any protected properties like primaryKey 因此无法访问任何受保护的属性,例如primaryKey

I hope this is help you..this trick is working perfectly for me..... 希望对您有帮助。.这个技巧对我来说是完美的.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM