Laravel Auth“在null上调用成员函数getReminderToken()”

时间:2017-09-24 03:46:54

标签: laravel authentication laravel-5 laravel-5.5

不知道我为什么会收到这个错误...

Call to a member function getRememberToken() on null (View: /home/vagrant/temptools/resources/views/layouts/main.blade.php) (View: /home/vagrant/temptools/resources/views/layouts/main.blade.php)

我在该刀片页面上有一个Auth :: check()

我正在使用https://github.com/invisnik/laravel-steam-auth

路线:

Route::get('login', 'AuthController@redirectToSteam')->name('login');
Route::get('login/handle', 'AuthController@handle')->name('login.handle');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

AuthController:

namespace App\Http\Controllers;

use Invisnik\LaravelSteamAuth\SteamAuth;
use App\User;
use Auth;

class AuthController extends Controller
{
    /**
     * The SteamAuth instance.
     *
     * @var SteamAuth
     */
    protected $steam;

    /**
     * The redirect URL.
     *
     * @var string
     */
    protected $redirectURL = '/';

    /**
     * AuthController constructor.
     * 
     * @param SteamAuth $steam
     */
    public function __construct(SteamAuth $steam)
    {
        $this->steam = $steam;
    }

    /**
     * Redirect the user to the authentication page
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function redirectToSteam()
    {
        return $this->steam->redirect();
    }

    /**
     * Get user info and log in
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function handle()
    {
        if ($this->steam->validate()) {
            $info = $this->steam->getUserInfo();

            if (!is_null($info)) {
                $user = $this->findOrNewUser($info);

                Auth::login($user, true);

                return redirect($this->redirectURL); // redirect to site
            }
        }
        return $this->redirectToSteam();
    }

    /**
     * Getting user by info or created if not exists
     *
     * @param $info
     * @return User
     */
    protected function findOrNewUser($info)
    {
        $user = User::where('id', $info->steamID64)->first();

        if (!is_null($user)) {
            return $user;
        }

        return User::create([
            'name' => $info->personaname,
            'avatar' => $info->avatarfull,
            'id' => $info->steamID64
        ]);
    }
}

应用程序/用户:

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'id', 'name', 'avatar',
    ];

    protected $hidden = [
        'remember_token',
    ];
}

create_users_table migration:

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->bigInteger('id')->unsigned();
            $table->string('name');
            $table->string('avatar');
            $table->rememberToken();
            $table->timestamps();

            $table->primary('id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

已删除密码恢复迁移。

如果我点击退出路径,我也会收到错误:

Call to a member function getRememberToken() on null

======

查看错误,错误似乎在laravel / framework / sec / Illuminate / Auth / EloquentUserProvider.php第67行

public function retrieveByToken($identifier, $token)
{
        $model = $this->createModel();
        $model = $model->where($model->getAuthIdentifierName(), $identifier)->first();
        $rememberToken = $model->getRememberToken();
        return $model && $rememberToken && hash_equals($rememberToken, $token) ? $model : null;
    }

仍然不知道如何修复它

2 个答案:

答案 0 :(得分:0)

看起来这个软件包使用Laravel Auth ...如何为用户设置唯一ID?通常你应该自动递增它们......别忘了Laravel的auth依赖于许多约定......看看AuthenticatesUsers的特性是为了窥视...但是所需的字段之一是电子邮件.. 。

trait AuthenticatesUsers
{
     /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'email';
    }
}

浏览Auth结构,查看默认的' web' guard(查看config目录中的auth.php作为起点......)。

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();   //Unique ID for login in laravel? The AuthenticatesUser trait uses email as username... 
            $table->string('password');
            $table->string('avatar');
            $table->rememberToken();
            $table->timestamps();
        });
    }

希望这会有所帮助......

答案 1 :(得分:0)

进入这个函数parent:

public function retrieveByToken($identifier, $token){
    $model = $this->createModel();
    $model = $model->where($model->getAuthIdentifierName(), $identifier)->first();
    $rememberToken = $model->getRememberToken();
    return $model && $rememberToken && hash_equals($rememberToken, $token) ? $model : null;
}

转储模型和$ identifier,

使用该标识符创建一个模型行,它将被修复!