在我的 laravel 9 项目中,我使用 laravel/ui 依赖项进行身份验证过程。所有使用 localhost:8000/register 注册的用户都成功登录到应用程序。但是我直接在phpMyAdmin中插入的那些用户无法登录。错误总是说 “这些凭据与我们的记录不匹配。” 尽管数据位于用户表中。
你对这个问题有什么想法吗?
Maulik 是对的,但是它并没有回答你的问题,因为你正在直接插入记录到 PhpMyAdmin。
如果您使用了 Laravel 附带的
AuthController
,那么密码将使用 Bcrypt 进行哈希处理。看看这个生成器:Bcrypt gen。加密您的字符串,将其插入 PhpMyAdmin 并尝试使用您在加密之前提供的密码登录。应该可以工作。
您需要创建哈希密码才能正常工作!
$password = Hash::make('12345678');
上面的行将创建哈希密码,将其放入密码字段
希望能成功
在查看文件(注册)中
<form id="register_form" action="#" method="post">
<div>
<input type="text" class="ggg" name="name" id="name" placeholder="NAME" required="">
<span id="error-name" class="text-danger"></span>
</div>
<div>
<input type="email" class="ggg" name="email" id="email" placeholder="E-MAIL" required="">
<span id="error-email" class="text-danger"></span>
</div>
<div>
<input type="password" class="ggg" name="password" id="password" placeholder="PASSWORD" required="">
<span id="error-password" class="text-danger"></span>
</div>
<div>
<input type="password" class="ggg" name="password_confirmation" id="password_confirmation" placeholder="Confirm Password" required="">
<span id="error-password_confirmation" class="text-danger"></span>
</div>
<div class="clearfix"></div>
<input type="button" id="register_btn" value="submit" name="register_btn">
</form>
在Js中进行注册
$(document).ready(function(){
console.log('doc ready');
$('#register_btn').on('click', function(){
register();
});
});
function validate(){
var Valid = true;
if($('#name').val().trim() === ""){
Valid = false;
}
if($('#email').val().trim() === ""){
Valid = false;
}
if($('#password').val().trim() === ""){
Valid = false;
}
if($('#password_confirmation').val().trim() === ""){
Valid = false;
}
if($('#password_confirmation').val() != $('#password').val()){
Valid = false;
}
return Valid;
}
function register(){
var input_array = ['name','email','phone','password'];
removeErrorMessage(input_array);
if (validate()) {
console.log('validated');
console.log('base_url', base_url);
var formData = $('#register_form').serialize();
$.ajax({
data: formData,
type: 'POST',
dataType: 'JSON',
url: base_url + '/signup',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success: function(response){
if(response.status){
window.location.href = response.redirect_url;
}else{
alert(response.message);
}
},
error: function(error, xhr, status){
console.log('xhr', xhr);
console.log('error', error);
console.log('status', status);
var response_error = error.responseJSON.errors;
console.log(response_error);
displayError(response_error);
}
});
console.log('form', formData);
} else {
console.log('not validated');
}
}
function displayError(response_error){
$.each(response_error, function(input_id, error_message){
$('#error-' + input_id).html(error_message);
});
}
function removeErrorMessage(input_array){
$.each(input_array, function(key, input_id){
$('#error-' + input_id).html('');
});
}
在寄存器控制器中
public function signup(Request $request){
$rules = [
'name' => 'required|string|max:255',
'email' => 'unique:customers|required|email',
'password' => 'required|min:3|string',
];
$validators = Validator::make($request->all(),$rules);
if ($validators->fails()) {
return response()->json(['status'=>false,'errors'=>$validators->errors()],422);
}else{
$return_array = ['status'=>false,'message'=>''];
$name = $request->input('name');
$email = $request->input('email');
$password = $request->input('password');
$confirm = $request->input('password_confirmation');
$checkexist = Customers::where([['email',$email]])->count();
if ($checkexist) {
$return_array['message'] = 'Email Already Exist';
}else{
if ($password == $confirm) {
$hashpassword = Hash::make($password);
$customer = new Customers();
$customer->name = $name;
$customer->email = $email;
$customer->password = $hashpassword;
$is_created = $customer->save();
if ($is_created) {
$return_array['status'] = true;
$return_array['message'] ='Registered Successfully';
$return_array['redirect_url'] = url('/dashboard');
$request->session()->put('customers',$customer->toArray());
}else{
$return_array['message'] = 'Failed registration';
}
}else{
$return_array['message'] = 'Password does not match';
}
}
}
return response()->json($return_array);
}