我正在尝试Laravel的Auth类,但每当我尝试登录用户时,该方法返回false.这是我的代码:
Route::get('new-user',function() { return View::make('register'); }); Route::post('new-user',function() { $name = Input::get('name'); $email = Input::get('email'); $password = Hash::make(Input::get('password')); $user = new User; $user->name = $name; $user->email = $email; $user->password = $password; $user->save(); }); Route::get('login',function() { return View::make('login'); }); Route::post('login',function() { $user = array( 'email' => Input::get('email'),'password' => Hash::make(Input::get('password')) ); if (Auth::attempt($user)) { //return Redirect::intended('dashboard'); return "ok."; } else { return "Wrong."; } });
意见/ login.blade.PHP
{{ Form::open(array('url' => 'login','method' => 'post')) }} <h1>Login:</h1> <p> {{ Form::label('email','Email: ') }} {{ Form::text('email') }}<br /> {{ Form::label('password','Password: ') }} {{ Form::password('password') }}<br /> </p> <p> {{ Form::submit('Login') }} </p> {{ Form::close() }}
配置/ auth.PHP
return array( 'driver' => 'eloquent','model' => 'User','table' => 'users','reminder' => array( 'email' => 'emails.auth.reminder','table' => 'password_reminders',),);
该数据库有电子邮件&密码字段,密码字段是varchar(60).
每当我将登录信息发送到/ login时,它都会返回“Wrong”.
我真的看不出这里有什么不对吗?
你的代码是错误的,因为你将错误的变量传递给Auth :: attempt().该方法需要一个带有密钥用户名,密码的数组,并可选择记住.鉴于此,您的上述代码应为:
原文链接:https://www.f2er.com/laravel/137974.htmlRoute::post('login',function() { $credentials = [ 'username' => Input::get('email'),'password' => Input::get('password') ]; dd(Auth::attempt($credentials)); });
希望有所帮助.
此外,我将为您提供额外代码的片段,以改善您的工作流程.存储新用户的路线:
Route::post('register',function() { $input = Input::only(['username','email','password']); // validate data Eloquent::unguard(); $user = User::create($input); Auth::loginUsingId($user->id); return Redirect::to('dashboard'); });
public function setPasswordAttribute() { $this->password = Hash::make($this->password); }
这样,密码将在每次设置时自动散列