Laravel Auth :: attempt()始终为假? [英] Laravel Auth::attempt() always false?

查看:178
本文介绍了Laravel Auth :: attempt()始终为假?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习Laravel(PHP MVC框架),并且在使用Auth :: attempt($ credentials)对用户进行身份验证时遇到了很多问题.

I recently started learning Laravel (PHP MVC framework) and I've been having a lot of issues with Authenticating a user with: Auth::attempt($credentials).

我将代码片段放在下面.在我的mySQL数据库中,我有一条包含以下key => values的记录: id => 1,用户名=>'admin',密码=>'admin',created_at => 0000-00-00 00:00:00,updated_at => 0000-00-00 00:00:00

I'll put my code snippets below. In my mySQL database I have a record with the following key=>values: id=>1, username=>'admin', password=>'admin', created_at=>0000-00-00 00:00:00, updated_at=>0000-00-00 00:00:00

验证程序通过,但是Auth:attempt总是失败.

The Validator passes, but the Auth:attempt always fails.

如果下面的代码和我的一点解释还不够,请告诉我! :-)

If the code below and my little explanation isn't enough, please let me know! :-)

Routes.php:

Routes.php:

Route::get('login', 'AuthController@showLogin');
Route::post('login', 'AuthController@postLogin');

AuthController:

AuthController:

public function showLogin(){

    if(Auth::check()){
        //Redirect to their home page
    }
    //Not logged in
    return View::make('auth/login');
}

public function postLogin(){

    $userData = array(
        'username' => Input::get('username'),
        'password' => Input::get('password')
    );

    $reqs = array(
        'username' => 'Required',
        'password' => 'Required'
    );

    $validator = Validator::make($userData, $reqs);

    if($validator->passes() && Auth::attempt($userData)){
        return Redirect::to('home');
    }
    return Redirect::to('login')->withInput(Input::except('password'));
}

推荐答案

我相信try()方法会对输入的密码进行哈希处理.如果您的数据库条目的密码为纯文本格式的"admin",那将失败.用Hash :: make($ password);保存您的密码.而且我相信您的问题应该得到解决.

I believe the attempt() method will hash the password that is sent in. Seeing as your DB entry's password is 'admin' in plaintext, that would fail. Save your password with Hash::make($password); and i believe your problem should be solved.

这篇关于Laravel Auth :: attempt()始终为假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆