[英]Laravel Authentication Problems
First off I am fairly new to Laravel. 首先,我对Laravel并不陌生。
So I have a form in my header on each page and that is pointing to a POST request. 所以我在每个页面的标题中都有一个表单,它指向一个POST请求。 I know that I have the proper user email and password in my mySQL database but when I try to login the 'if(Auth::attempt($userdata))' always fails.
我知道我的mySQL数据库中有正确的用户电子邮件和密码,但是当我尝试登录'if(Auth :: attempt($ userdata))'时总是失败。
Form: 形成:
@if(Auth::check())
<li><a href="{{ URL::to('/') }}">Home</a></li>
<li><a href="{{ URL::to('profile') }}">Profile</a></li>
<li><a href="{{ URL::to('logout') }}">Logout</a>
@else
<div class="navbar-form navbar-left">
{{ Form::open(array('url' => '/account/sign-in')) }}
<div class="form-group">
<input type="email" name="email" class="form-control input-sm" placeholder="Email">
<input type="password" name="password" class="form-control input-sm" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-default btn-sm">Login</button>
{{ Form::close() }}
POST: 开机自检:
Route::post('/account/sign-in', function(){
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if(Auth::attempt($userdata)){
return 'Success';
}else{
return 'Fail';
}
});
I also have included the $fillable in my User.php model: 我的User.php模型中还包含了$ fillable:
protected $fillable = array('firstname', 'lastname', 'password', 'email', 'code', 'active');
Registration method: 报名方法:
public function postCreate(){
$validator = Validator::make(Input::all(),
array(
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|max:50|email', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3', // password can only be alphanumeric and has to be greater than 3 characters
'password_confirmation' => 'required|same:password'
)
);
if($validator->fails()){
return Redirect::route('account-create')->withErrors($validator)->withInput();
}else{
//Activation Code
$code = str_random(60);
$user = new User;
$pass1 = Input::get('password');
$pass2 = Input::get('password_confirmation');
$firstname = Input::get('firstname');
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->email = Input::get('email');
$user->code = $code;
$user->active = 0;
if($pass1 == $pass2){
$user->password = Hash::make(Input::get('pass1'));
}else{
return Redirect::to('register/');
}
$user->save();
}
if($user){
//send email
Mail::send('emails.auth.activate', array('link' =>URL::route('account-activate', $code), 'firstname' => $firstname), function($message) use($user){
$message->to($user->email, $user->firstname)->subject('Activate your Account');
});
return Redirect::to('/')->with('global', 'Your account has been create! We have sent you an email to activate your account');
}
}
Sign in method in UsersController: 在UsersController中登录方法:
public function postSignIn(){
$validator = Validator::make(Input::all(),
array(
'email' => 'required',
'pasword' => 'required'
)
);
if($validator->fails()){
return Redirect::to('/')->withErrors($validator)->withInput();
}else{
$user = array(
'active' => 1,
'email' => Input::get('email'),
'password' => Input::get('password')
);
if(Auth::attempt($user)){
//Redirect to intended page
return Redirect::intended('/')->with('global', 'Success!!');
}else{
/*return Redirect::to('/')->with('global', 'Failed to login');*/
return Input::all();
}
}
return Redirect::to('/')->with('global', 'There was a problem signing in. Did you activate?');
}
View: 视图:
<ul class="nav navbar-nav navbar-right">
@if(Auth::check())
<li><a href="{{ URL::to('/') }}">Home</a></li>
<li><a href="{{ URL::to('profile') }}">Profile</a></li>
<li><a href="{{ URL::to('logout') }}">Logout</a></li>
@else
<div class="navbar-form navbar-left">
{{ Form::open(array('url' => '/account/sign-in')) }}
<div class="form-group">
<input type="email" name="email" class="form-control input-sm" placeholder="Email">
<input type="password" name="password" class="form-control input-sm" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-default btn-sm">Login</button>
{{ Form::close() }}
</div>
<li><a href="{{ URL::to('account/create') }}">Register</a></li>
@endif
</ul>
Thanks 谢谢
从Dropbox的文件中, UsersController
的postSignIn
方法具有'pasword' => 'required'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.