Can't authenticate user in laravel

不羁的心 提交于 2019-12-12 08:01:14

问题


        $userdata = array(
            'email' => Input::get('email'),
            'password'  => Input::get('password')
            );



        if (Auth::attempt($userdata)) {

            echo 'SUCCESS!';

        } else {        

            return Redirect::to('login');

        }

when i run the application from browser i got this error :

Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Auth\UserInterface, instance of User given, called in /home/srtpl17/Sites/yatin.sr/vendor/laravel/framework/src/Illuminate/Auth/Guard.php on line 316 and defined

What have I done incorrectly?


回答1:


It sounds like from your error that your User model does not implement UserInterface (the default User Model that comes with Laravel does this properly, so I'm guessing you made a custom one)

http://github.com/laravel/laravel/blob/master/app/models/User.php

Make sure that the User model looks like this:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {


/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

// Add your other methods here

}



回答2:


You don't show what userdata is defined as, but from the error message it seems like it's an instance of your User model class.

The Auth::attempt() method is looking for an array of the credentials not a model.




回答3:


This error might also appear when you use a different model for users (example :member) if it's the case you need to update Authentication Model and Authentication Table in app/config/auth.php with the appropriate model / database table.



来源:https://stackoverflow.com/questions/21723419/cant-authenticate-user-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!