如何为使用Google API登录但没有密码的用户制作更改密码表单?拉拉维尔



我有一个简单的表单来更改用户密码,但在我的登录系统中,有一个使用Google API登录的选项。如果用户使用Google登录,则不会向该用户分配密码值。

我如何正确定义我的控制器和表单,以便如果用户没有密码,他应该创建一个密码,如果创建了密码,用户现在可以通过两种方式登录。谷歌API或带密码的电子邮件。

我的UserController功能:

public function changePassword(Request $request)
{
if (!(Hash::check($request->get('current-password'), Auth::user()->password))) {
// The passwords matches
return redirect()->back()->with('error', 'Your current password does not matches 
with the password you provided. Please try again.');
}
if (strcmp($request->get('current-password'), $request->get('new-password')) == 0) {
//Current password and new password are same
return redirect()->back()->with('error', 'New Password cannot be same as your 
current password. Please choose a different password.');
}
$validatedData = $request->validate([
'current-password' => 'required',
'new-password' => 'required|string|min:6|confirmed',
]);
//Change Password
$user = Auth::user();
$user->password = bcrypt($request->get('new-password'));
$user->save();
return redirect()->back()->with('success', 'Password changed successfully !');
}

更改密码的HTML表单:

<form class="form-horizontal" method="POST" action="{{ route('account.change-password')}}">
{{ csrf_field() }}
@method('patch')
<div class="form-group{{ $errors->has('current-password') ? ' has- 
error' : '' }}">
<label for="new-password" class="col-md-4 control-label">Current 
Password</label>
<div class="col-md-6">
<input id="current-password" type="password" class="form- 
control" name="current-password" required>
@if ($errors->has('current-password'))
<span class="help-block">
<strong>{{ $errors->first('current-password') }} 
</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('new-password') ? ' has-error' 
: '' }}">
<label for="new-password" class="col-md-4 control-label">New 
Password</label>
<div class="col-md-6">
<input id="new-password" type="password" class="form- 
control" name="new-password" required>
@if ($errors->has('new-password'))
<span class="help-block">
<strong>{{ $errors->first('new-password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="new-password-confirm" class="col-md-4 control- 
label">Confirm New Password</label>
<div class="col-md-6">
<input id="new-password-confirm" type="password" 
class="form-control" name="new-password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Change Password
</button>
</div>
</div>
</form>

您有注册类型吗?就像在谷歌注册时,你会存储他在谷歌注册的信息。例如

@if($user->register_type == 'google')
<div class="form-group{{ $errors->has('current-password') ? ' has- 
error' : '' }}">
<label for="new-password" class="col-md-4 control-label">Current 
Password</label>
<div class="col-md-6">
<input id="current-password" type="password" class="form- 
control" name="current-password" required>
@if ($errors->has('current-password'))
<span class="help-block">
<strong>{{ $errors->first('current-password') }} 
</strong>
</span>
@endif
</div>
</div>
@endif

最新更新