gpt4 book ai didi

php - Laravel 身份验证问题

转载 作者:行者123 更新时间:2023-11-30 00:27:40 25 4
gpt4 key购买 nike

首先,我对 Laravel 相当陌生。

所以我在每个页面的标题中都有一个表单,它指向 POST 请求。我知道我的 mySQL 数据库中有正确的用户电子邮件和密码,但是当我尝试登录“if(Auth::attempt($userdata))”时总是失败。

表格:

@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() }}

发布:

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';
}
});

我还在我的 User.php 模型中包含了 $fillable:

protected $fillable = array('firstname', 'lastname', 'password', 'email', 'code', 'active');

注册方式:

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');
}
}

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?');
}

查看:

<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>

谢谢

最佳答案

从 Dropbox 中的文件中,UsersController 中的 postSignIn 方法具有 'password' => 'required'

关于php - Laravel 身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22751161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com