laravel-5 - Laravel 5.7 + socialite 类型错误:参数 1 传递给 Illuminate\Auth\SessionGuard::login()
问题描述
我正在使用 solcialite 通过 Gihub 登录 Laravel,但登录时出现问题。我收到一个错误:Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) 类型错误:传递给 Illuminate\Auth\SessionGuard::login() 的参数 1 必须实现接口 Illuminate\Contracts\Auth\Authenticatable,给定 null,调用/var/www/html/testio/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php 在第 292 行
我的登录控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
use App\User;
use Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Redirect the user to the GitHub authentication page.
*
* @return \Illuminate\Http\Response
*/
public function redirectToProvider()
{
return Socialite::driver('github')->redirect();
}
/**
* Obtain the user information from GitHub.
*
* @return \Illuminate\Http\Response
*/
public function handleProviderCallback()
{
$github_user = Socialite::driver('github')->user();
$user = $this->userFindorCreate($github_user);
Auth::login($user, true);
return redirect('/home');
// $user->token;
}
public function userFindorCreate($github_user){
$user = User::where('provider_id', $github_user->id)->first();
if(!$user){
$user = new User;
$user->name = $github_user->getName();
$user->email = $github_user->getEmail();
$user->provider_id = $github_user->getid();
$user->save();
}
}
}
我的 User.php 模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Foundation\Auth\User as AuthUser;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
创建用户表:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('password')->nullable();
$table->string('provider_id');
$table->rememberToken();
$table->timestamps();
});
}
该死的我做错了什么?
解决方案
我错过了 return $users; 最后一个功能:
public function userFindorCreate($github_user){
$user = User::where('provider_id', $github_user->id)->first();
if(!$user){
$user = new User;
$user->name = 'jojo';
$user->email = $github_user->getEmail();
$user->provider_id = $github_user->getid();
$user->save();
}
return $user;
}
推荐阅读
- javascript - Brain js 字符串分类速度慢
- zsh - 如何为执行另一个命令的函数创建别名?
- angular - 如何使赛普拉斯的 toMatchSnapshot() 在 Angular 上工作?
- json - 使用 Postgres JSON 数据类型的 Grafana 绘图不起作用
- javascript - 如何在 JavaScript Web Socket 上缓存错误 net::ERR_CONNECTION_REFUSED
- c# - 使用 C# 根据 2 个变量中的值创建表格格式数据
- ruby-on-rails - Rails 默认错误页面不显示在开发中
- c++ - 添加 DIV/0 异常处理。我做错了什么,但无法指出在哪里
- corda - 检索合同附带的法律文书
- angular - 如何在 Apollo Angular 中通过 refetchQueries 使用optimisticResponse