如何在laravel 5中验证登录功能的凭据


How to validate credentials in laravel 5 for logging in features?

我是Laravel的新手,正在使用web应用程序教程。教程使用Laravel 4,而我使用的是Laravel 5。我解决了注册问题,但当我开始测试登录时,出现了以下错误:

EloquentUserProvider.php第111行中的ErrorException:传递给Illuminate''Auth''EloquetUserProvider::validateCredentials()的参数1必须是Illuminate''Comments''Auth''Authenticable的实例,即给定的App''user的实例,在第390行的C:''Users''Pujan''Desktop''projectlaravel''vendor''laravel''framework''src''Illuminates''Auth''Guard.php中调用并定义。

我搞不清楚这里的问题。凭证的实际含义。我知道Laravel 5有内置的登录功能,但无法使用,所以我尝试了不同的方法,但这个问题超出了我的关注范围。我的用户控制器是:

<?php
namespace App'Http'Controllers;
use Illuminate'Http'Request;
use App'Http'Requests;
use App'Http'Controllers'Controller;
use View;
use App'user;
use Input;
use App'Http'Requests'YourFormRequest;
use Auth;
//use Illuminate'Support'Facades'Auth;
class UserController extends Controller
{
    public $restful = true;
    public function index()
    {
        return View::make('users.new')->with('title', 'Make it snappy Q&A - Register');
    }

    public function create(YourFormRequest $request)
    {
        User::create($request->all());
        return redirect('/')->with('message','Thanks for registering!');
    }
    public function getlogin()
    {
       // return 'Auth::user();
        return View::make('users.login')->with('title','Make it snappy Q&A - Login ');
    }
    public function createlogin()
    {
        $user = array(
            'username'=>Input::get('username'),
            'password'=>Input::get('password')
            );
        if (Auth::attempt($user))
        {
            return redirect('/')->with('message','You are logged in:');
        }else{
            return redirect('login')
            ->with('message','Your username or password are incorrect pls chk it out')
            ->with_input();
        }
    }

我有我的路线:

Route::get('/','Questions@index');
Route::get('register','UserController@index');
Route::get('login','UserController@getlogin');
Route::post('register','UserController@create');
Route::post('login','UserController@createlogin');

我的登录布局工作正常,但当我尝试登录时,上面的错误出现了。我认为这个错误属于内置的Laravel 5功能,但我无法匹配内置的设置和我创建的登录功能。

您看到的错误与登录代码无关。您的User实体应该实现Illuminate'Contracts'Auth'Authenticatable接口。接口是一个约定,它列出了类必须具有的方法/函数。类定义应该如下所示:

class User extends Model implements Authenticatable
{

快速浏览一下你的代码,你可以做其他事情来清理它:

public function createlogin()
{
    if (Auth::attempt(Input::only('username', 'password'))) {
        return redirect('/')->with('message','You are logged in:');
    }
    return redirect('login')
        ->with('message','Your username or password are incorrect pls chk it out')
        ->with_input();
}

最后,我要停止使用您正在学习的教程。Laravel的最新文档有一个涵盖身份验证的快速入门指南,有两个版本,beginnner和intermediate。

@Logan的建议是正确的,您应该遵循这一点。

我只是去清理一下你的代码。

无论何时处理验证,都应该将其视为一个单独的东西,为此需要创建FormRequest对象。

请参阅本文档,了解如何创建表单请求。

然后用以下内容更新authorizerules方法:

/**
 * Authorize the request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}
/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'username' => 'required|exists:users|alpha_dash|min:4',
        'password' => 'required'
    ];
}

现在在你的createLogin方法

/**
 * Log in the user.
 *
 * @param 'App'Http'Requests'YourFileRequest $request
 * @return 'Illuminate'Http'RedirectResponse
 */
public function createlogin(YourFileRequest $request)
{
    $credentials = $request->only('username', 'password');
    if (Auth::attempt($credentials)) {
        return redirect('/')->with('message','You are logged in:');
    }
    return redirect('login')
        ->with('message', 'Invalid credentials')
        ->with_input();
}

现在,当您在6个月后访问时,您看到您的代码是如何简单易读的?必须是这样。Validation逻辑在单独的文件中,Login逻辑在控制器中。据我说,当你通过phpUnit、phpSpec或任何其他测试工具进行测试时,这会有很大帮助。

我和Logan也替换了你的if语句块。这是因为,当您从ifelse块返回某些内容时,应该尽可能避免使用else块。

只是附带说明:

  1. 在编程/编码时,您应该始终遵循3个原则:SOLID、KISS和DRY
  2. 当你完成代码的那一部分时,请记录你的代码。这将再次帮助未来的程序员,也可供未来参考,以了解这段代码在做什么

希望这能帮到你。干杯

不确定你是否已经解决了这个问题,但我把我的经验放在这里,也许它可以帮助其他人寻找这个问题的解决方案,而这个问题恰好发生在我身上。

你的应用程序/用户模型类应该扩展Illuminate'Foundation'Auth'User,而不是Eloquent模型。

因此,将您的用户模型更改为:

use Illuminate'Foundation'Auth'User as Authenticatable;
class User extends Authenticatable

代替

class User extends Model

它确实为我解决了同样的错误。希望这能有所帮助。