2
votes

Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Database\User\User given, called in C:\Users\admin\workspace\projects\psz\document_root\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 292

<?php

namespace App\Database\User;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'game_users';
    public $timestamps = true;
    protected $guarded = ['id'];
}

RegisterController

public function onPost(Request $request)
{
    $validator = Validator::make($request->all(), [
        'username' => 'required|unique:game_users,username|between:3,12',
        'password' => 'required|confirmed|min:8',
        'gender' => 'required',
        'g-recaptcha-response' => 'required',
        'terms_of_service' => 'required',
    ]);

    if ($validator->fails()) {
        return Redirect::back()->withErrors($validator)->withInput();
    }
    else {
        $curl = curl_init();

        curl_setopt_array($curl, [
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => [
                'secret' => '************-**************-****',
                'response' => $request->input('g-recaptcha-response'),
                'remoteip' => ''
            ]
        ]);

        if (json_decode(curl_exec($curl))->success) {
            $user = User::create([
                'username' => $request->input('username'),
                'password' => Hash::make($request->input('password')),
                'gender' => $request->input('gender') == '1' ? 'M' : 'F'
            ]);

            Auth::login($user);

            return redirect()->route('frontend.user.home');
        }
        else {
            return Redirect::back()->with('You failed to complete the captcha.')->withInput();
        }
    }
}
2

2 Answers

0
votes

User model must extend Authenticatable if you want to use login() method:

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
0
votes

User model must extend Authenticatable and the AuthenticableTrait must be used in User class.

Modify your Model/User.php as per the following code:

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;

class User extends Eloquent  implements Authenticatable
{
    use AuthenticableTrait;