Laravel 5 auth总是假的吗?

时间:2016-02-14 19:04:16

标签: laravel laravel-5

这是我的登录控制器。注册控制器运行良好,它创建了一个用户到数据库,但当我尝试登录失败时,结果总是错误请帮助。
      

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;

class loginController extends Controller
{
    public function login(Request $request)

    {
        $_email = $request->input('emaillogin');
        $_password = $request->input('passwordlogin');

        if (Auth::attempt(['email' => $_email, 'password' => $_password])) {

            Auth::login(Auth::user());
            return redirect()->intended('/dashboard');
        }
        else {
            return redirect()->intended('/');
        }    
    }
}

2 个答案:

答案 0 :(得分:2)

如果通过,

Auth::attempt会将用户登录到您的应用程序中,无需进行第二次身份验证

 if (Auth::attempt(['email' => $_email, 'password' => $_password])) { 

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

 } else { 

     return redirect()->guest('/login'); 

 }

答案 1 :(得分:0)

正如我在评论中说的那样,我刚刚在我的本地电脑上测试过,也许这对你来说不对,但对我来说,如果我使用普通密码保存在数据库中,这是不行的,这是我的寄存器文件

    protected function create(array $data)
{
    $activation_code = str_random(60);
    $user = User::create([
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'activation_code' => $activation_code
    ]);

    if ($user->save()) {
        $data = array(
        'name' => $user->username,
        'code' => $activation_code,
        );
        \Mail::queue('emails.activate', $data, function($message) use ($user) {
        $message->to($user->email)->subject('Thank you for registering. Please activate your account!');
        });
    }

    return $user;
}
你知道我有加密密码

'password' => bcrypt($data['password']),

如果我只用以下方法测试:

'password' =>$data['password'],

注册有效但身份验证失败,因为必须输入密码,您还应删除不必要的Auth::login(Auth::user());

 if (Auth::attempt(['email' => $_email, 'password' => $_password])) { 

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

}

当我在注册控制器中不使用bcrypt密码并尝试稍后登录时,会在我的网站上发生这种情况

Whoops! There were some problems with your input.

These credentials do not match our records.

我在登录表单输入中看到的不是emailloginpasswordlogin它们只是emailpassword