Laravel:将数据传递给session和auth :: attempt

时间:2016-05-24 15:04:49

标签: php laravel laravel-5.2

我正在处理身份验证模块,我有以下代码,if条件是检查表单字段不起作用且条件为false。

然后else消息也没有在那里显示。

if (Auth::attempt(['email' => Input::get("email"), 'password' => Input::get("password")], true)) {

  return redirect()->intended('dashboard');

} else {
  return redirect()
        ->back()
        ->with('message', 'Incorrect email or password.')
        ->with('form', 'login')
        ->withInput(Input::except('password'));
}

在登录视图中,我有一大堆错误代码:

<div class="alert alert-danger {{{ (Session::has('message') && Session::get('form', 'login') == 'login') ? '' : 'display-hide' }}}">
    <button class="close" data-close="alert"></button>
    <span>
        {!! Session::has('message') ? Session::get('message') : 'Please correct your fields.' !!}
    </span>

</div>

我使用的是ubuntu,权限是:

drwxrwxrwx 10 programmer programmer   4096 May 24 23:56 app
-rwxrwxrwx  1 programmer programmer   1646 Apr 27 18:01 artisan
drwxrwxrwx  3 programmer programmer   4096 Apr 27 18:01 bootstrap
-rwxrwxrwx  1 programmer programmer   1351 May 25 00:20 composer.json
-rwxrwxrwx  1 programmer programmer 124821 May 25 00:20 composer.lock
drwxrwxrwx  2 programmer programmer   4096 May 24 23:56 config
drwxrwxrwx  5 programmer programmer   4096 May 24 23:56 database
-rwxrwxrwx  1 programmer programmer    503 Apr 27 18:01 gulpfile.js
-rwxrwxrwx  1 programmer programmer    212 Apr 27 18:01 package.json
-rwxrwxrwx  1 programmer programmer   1026 Apr 27 18:01 phpunit.xml
drwxrwxrwx  6 programmer programmer   4096 May 24 23:57 public
-rwxrwxrwx  1 programmer programmer   1918 Apr 27 18:01 readme.md
drwxrwxrwx  5 programmer programmer   4096 Apr 27 18:01 resources
-rwxrwxrwx  1 programmer programmer    567 Apr 27 18:01 server.php
drwxrwxrwx  5 programmer programmer   4096 Apr 27 18:01 storage
drwxrwxrwx  2 programmer programmer   4096 Apr 27 18:01 tests
drwxrwxrwx 31 programmer programmer   4096 May 25 00:20 vendor

路线列表:

+--------+----------+----------------------------+---------------------------------+-------------------------------------------------------------+-------------------------+
| Domain | Method   | URI                        | Name                            | Action                                                      | Middleware              |
+--------+----------+----------------------------+---------------------------------+-------------------------------------------------------------+-------------------------+
|        | GET|HEAD | /                          |                                 | App\Http\Controllers\CommonController@index                 | web,web,auth            |
|        | GET|HEAD | dashboard                  | dashboard                       | App\Http\Controllers\CommonController@routeDashboard        | web,web,auth            |
|        | GET|HEAD | login                      |                                 | App\Http\Controllers\CommonController@showLogin             | web,web,guest           |
|        | POST     | login                      |                                 | App\Http\Controllers\CommonController@authenticate          | web,web,guest           |
|        | GET|HEAD | logout                     | logout                          | App\Http\Controllers\CommonController@logout                | web,web,auth            |
|        | POST     | password/reset             |                                 | App\Http\Controllers\Auth\PasswordController@postReset      | web,web,guest,guest     |
|        | GET|HEAD | password/reset/{token}     |                                 | App\Http\Controllers\CommonController@showReset             | web,web,guest           |
|        | POST     | reset                      |                                 | App\Http\Controllers\Auth\PasswordController@postEmail      | web,web,guest,guest     |
|        | GET|HEAD | users                      | admin::users::manage            | App\Http\Controllers\AdminController@showManageUsers        | web,web,auth,role:admin |
|        | POST     | users/edit-basic/{id}      | admin::users::                  | App\Http\Controllers\AdminController@editUserBasicSave      | web,web,auth,role:admin |
|        | GET|HEAD | users/edit-basic/{id}      | admin::users::edit_basic        | App\Http\Controllers\AdminController@editUserBasic          | web,web,auth,role:admin |
|        | POST     | users/edit-historical/{id} | admin::users::                  | App\Http\Controllers\AdminController@editUserHistoricalSave | web,web,auth,role:admin |
|        | GET|HEAD | users/edit-historical/{id} | admin::users::edit_historical   | App\Http\Controllers\AdminController@editUserHistorical     | web,web,auth,role:admin |
|        | POST     | users/handle               | admin::users::handle            | App\Http\Controllers\AdminController@handleUsers            | web,web,auth,role:admin |
|        | POST     | users/new                  | admin::users::                  | App\Http\Controllers\AdminController@newUserSave            | web,web,auth,role:admin |
|        | GET|HEAD | users/new                  | admin::users::new               | App\Http\Controllers\AdminController@newUser                | web,web,auth,role:admin |
|        | POST     | users/validate-email/{id?} | admin::users::validate_email    | App\Http\Controllers\AdminController@validateEmail          | web,web,auth,role:admin |
|        | POST     | users/validate-username    | admin::users::validate_username | App\Http\Controllers\AdminController@validateUsername       | web,web,auth,role:admin |
+--------+----------+----------------------------+---------------------------------+-------------------------------------------------------------+-------------------------+

1 个答案:

答案 0 :(得分:1)

这不起作用,因为auth :: attempt使用bcrypt将密码转换为hash,并在users表中查找该hash以匹配。

简而言之,密码应该是存储在数据库表中的哈希值,用于auth ::尝试工作。

这就是你的if()条件失败的原因。

以下是来自laravel 5.2 docs

laravel.com/docs/5.2/authentication#authenticating-users

  

attempt方法接受一个键/值对数组作为其第一个   论点。数组中的值将用于查找用户   你的数据库表。因此,在上面的示例中,用户将是   通过电子邮件列的值检索。如果找到用户,则   存储在数据库中的哈希密码将与之进行比较   散列密码值通过数组传递给方法。如果是两个   将启动散列密码匹配经过身份验证的会话   用户。

     

如果身份验证成功,则尝试方法将返回true。   否则,将返回false。