Laravel 5.7 + socialite类型错误:参数1传递给Illuminate \ Auth \ SessionGuard :: login()

时间:2018-08-30 16:17:24

标签: laravel-5 laravel-socialite

我正在使用Solcialite通过Gihub登录Laravel,但登录时遇到问题。我收到一个错误:Symfony \组件\调试\异常\ FatalThrowableError(E_RECOVERABLE_ERROR) 类型错误:传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须实现Illuminate \ Contracts \ Auth \ Authenticatable接口,给定为null,在/ var / www / html / testio / vendor / laravel / framework / src /中调用第292行的Illuminate / Auth / AuthManager.php

我的LoginController:

<?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',
    ];
}

create_users_table:

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();
        });
    }

该死我做错了吗?

1 个答案:

答案 0 :(得分:0)

我错过了$ 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;
    }