Laravel5:Auth ::尝试不按预期工作

时间:2015-10-14 17:52:17

标签: php authentication laravel-5

我已阅读Laravel 5 Docs about Authentication但无法按照说明设置登录。

我的用户表包含字段,用户名和密码(类型为TEXT),我已存储哈希密码,如$2y$10$XCyEOyvC6Yp/O6HaeemPheO4KV1I8aEMUytZZt77Yjw9hp/j6uivWnope

这是我的代码:

public function validateLogin()
{
    $email = Input::get('email');
    $pass = Input::get('password');
    $pass = Hash::make($pass);

    $credentials = [
            'username' => $email,
            'password' => $pass
    ];
    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return "passed";
    }else{
        return "nope";
    }
}

我尝试过任何事情,Auth::attempt总是返回false :( 完全像文档中描述的那样,任何人都知道错误是什么? 感谢

2 个答案:

答案 0 :(得分:2)

您可能不需要自己进行$pass = Hash::make($password)调用。

尝试删除该行,看看它是否有效。

来自文档:

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Routing\Controller;

class AuthController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
}
  

attempt方法接受一个键/值对数组作为其第一个   论点。数组中的值将用于查找用户   你的数据库表。因此,在上面的示例中,用户将是   通过电子邮件列的值检索。 如果找到用户,   存储在数据库中的散列密码将与   散列密码值通过数组传递给方法。

进一步参考检查:

http://laravel.com/docs/master/authentication#authenticating-users

答案 1 :(得分:0)

你在AuthController中编写了这个函数吗?或者在任何新的/其他控制器中?

如果您在其他控制器中编写该函数,请确保已在顶部添加此代码

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;




public function login()
    {
        $email = Input::get('email');
        $password = Input::get('password');
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
        else{
             return redirect()->intended('admin/login');
        }
    }
相关问题