简体   繁体   中英

Has authentication changed in Laravel 5.1?

Consider the following:

namespace App\ART\Domain\Services;

use App\ART\Domain\Entities\User;
use Illuminate\Support\Facades\Auth;

/**
 * Handle Sessions.
 */
class SessionService {

    /**
     * Create a new session based on user id.
     *
     * @param User $user
     */
    public function createSession(User $user) {
        Auth::loginUsingId($user->id);
    }

    /**
     * Destroy a session if it exists.
     */
    public function destroySession() {
        if (Auth::check()) {
            Auth::logout();
        }
    }
}

Pretty basic, nothing fancy going on here. Create a new session based on the specific user id.

How ever now I get:

ErrorException in Guard.php line 430:
Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\ART\Domain\Models\User given, called in /var/www/AppResponseTracker/vendor/laravel/framework/src/Illuminate/Auth/Guard.php on line 489 and defined

A bit of the stack trace:

in Guard.php line 430
at HandleExceptions->handleError('4096', 'Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\ART\Domain\Models\User given, called in /var/www/AppResponseTracker/vendor/laravel/framework/src/Illuminate/Auth/Guard.php on line 489 and defined', '/var/www/AppResponseTracker/vendor/laravel/framework/src/Illuminate/Auth/Guard.php', '430', array()) in Guard.php line 430
at Guard->login(object(User), false) in Guard.php line 489
at Guard->loginUsingId('1')
at call_user_func_array(array(object(Guard), 'loginUsingId'), array('1')) in Manager.php line 137
at Manager->__call('loginUsingId', array('1')) in Facade.php line 210
at AuthManager->loginUsingId('1') in Facade.php line 210
at Facade::__callStatic('loginUsingId', array('1')) in SessionService.php line 19
at Auth::loginUsingId('1') in SessionService.php line 19

The last line: at Auth::loginUsingId('1') in SessionService.php line 19

Did they change something that prevents me from passing in an ID?

Update!!

I have code else where that states if the user is logged in Auth::check() then allow access to a route and if not don't. After this error is thrown access is granted ...

This doesn't seem right ...

Sorry for the previous comment , i miss-understood your question.

as it turns out that the loginUsingId() function is just setting the sessions and then calls login() function.

public function loginUsingId($id, $remember = false){

 $this->session->set($this->getName(), $id); $this->login($user = $this->provider->retrieveById($id), $remember); return $user; }

and the login() function takes a user object of type Authenticatable.

public function login(Authenticatable $user, $remember = false);

so your model should be implementing AuthenticatableContract.

class User extends Model implements AuthenticatableContract{//Code goes here}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM