[英]Laravel 5.1: Auth::login() not persisting
我正在使用Laravel Socialite从Facebook验证用户身份。 我正在获取用户数据并将其存储在数据库中。 但是,当我尝试在视图中使用Auth :: check时,它返回false。 尽管在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;
}
}
但鉴于:
`
<?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"> </span>
<span class="user-name mobile-remove">Saurabh</span><span class="mobile-remove"> </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
希望对您有帮助。.这个技巧对我来说是完美的.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.