繁体   English   中英

Auth::login() 无法正常工作 laravel 5.7

[英]Auth::login() not working properly laravel 5.7

我正在尝试使用 laravel socialite 设置“使用 facebook 登录”。 当我尝试登录时,它从 facebook 获得成功回调,我将获取的数据存储到数据库中并尝试重定向到主页。 这样做时,我被重定向回登录页面,再也没有到达主页。

在调试错误时,我发现我的 Auth::login($user) 无法正常工作。

这是代码-

授权控制器.php

use App\Http\Controllers\Controller;
use Laravel\Socialite\Two\InvalidStateException;
use Auth;
use Socialite;
use App\User;

public function redirectToProvider($provider)
{
    return Socialite::driver($provider)->redirect();
}

public function handleProviderCallback($provider)
{
    $user = Socialite::driver($provider)->user();
    // dd($user);
    $authUser = $this->findOrCreateUser($user, $provider);
    // dd($authUser);
    if(Auth::login($authUser, true)){            // here is the error
      return redirect($this->redirectTo);
    }
    else{
      return 'Login not done';                 //this prints out to the screen
    }
}

public function findOrCreateUser($user, $provider)
 {
     $authUser = User::where('id', $user->id)->first();
     if ($authUser) {
         return $authUser;
     }
     return User::create([
         'name'     => $user->name,
         'email'    => $user->email,
         'avatar'    => $user->avatar,
         'password'    => bcrypt('password'),
         'provider' => $provider,
         'id' => $user->id
     ]);
 }

让我知道我做错了什么。

这就是我在 dd($authUser) 时得到的

在此处输入图像描述

由于您正在获取用户详细信息并将它们保存到您的数据库中,因此为了登录,您需要使用尝试并传递从 $authUser 变量中获取的详细信息,但是您需要一种获取密码的方法,因为尝试转换密码字符串到哈希以登录用户

$authUser = $this->findOrCreateUser($user, $provider);
  $credentials = $request->only($authUser->email, 'password');

        if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }

最后,我只是通过在我的 session.php 中将domain设为 null 来解决它。 我不知道这是否是正确的答案,但我现在可以在使用谷歌登录后重定向到我的主页。 也许如果你将它部署到生产环境中,你会将域更改为实际的 url。

/*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */

    'domain' => null,

    /*
    |--------------------------------------------------------------------------
    | HTTPS Only Cookies
    |--------------------------------------------------------------------------
    |
    | By setting this option to true, session cookies will only be sent back
    | to the server if the browser has a HTTPS connection. This will keep
    | the cookie from being sent to you if it can not be done securely.
    |
    */

    'secure' => false,

Auth::login()返回无效。 采用

Auth::login($authUser, true);

if(Auth::check()){
  return redirect($this->redirectTo);
}
else{
  return 'Login not done';
}

你应该使用auth()->login($user); 创建用户后将用户添加到身份验证的功能

暂无
暂无

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

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