首页 文章

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

提问于
浏览
0

我使用solcialite通过Gihub登录Laravel,但是登录时有问题 . 我收到一个错误:Symfony \ Component \ Debug \ Exception \ FatalThrowableError(E_RECOVERABLE_ERROR)类型错误:传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须实现接口Illuminate \ Contracts \ Auth \ Authenticatable,null给定,调用第292行/var/www/html/testio/vendor/laravel/framework/src/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

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

相关问题