Laravel 5.7 + socialite Ошибка типа: Аргумент 1 передан в Illuminate \ Auth \ SessionGuard :: login () - PullRequest
0 голосов
/ 30 августа 2018

Я использую solcialite для входа в Laravel через Gihub, но есть проблема при входе. Я получаю сообщение об ошибке: Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) Ошибка типа: Аргумент 1, передаваемый в Illuminate \ Auth \ SessionGuard :: login (), должен реализовывать интерфейс Illuminate \ Contracts \ Auth \ Authenticatable, дано с нулевым значением, вызывается в / var / www / html / testio / vendor / laravel / framework / src / Осветить / 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',
    ];
}

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 голосов
/ 30 августа 2018

Я пропустил возврат $ пользователей; по последней функции:

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;
    }
...