Как сделать форму смены пароля для пользователя, который вошел в Google API и не имеет пароля? Laravel - PullRequest
0 голосов
/ 27 февраля 2020

У меня есть простая форма для изменения пароля пользователя, но в моей системе входа в систему есть возможность войти в систему с помощью Google API. Если пользователь вошел в систему с помощью Google, этому паролю не присвоено значение пароля.

Как я могу правильно определить свой контроллер и форму, чтобы, если у пользователя нет пароля, он должен его создать, если Пользователь теперь может войти в систему двумя способами через. Google API или Email с паролем.

Функция My 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>

1 Ответ

0 голосов
/ 27 февраля 2020

У вас есть тип регистрации? Например, при регистрации в Google вы сохраняете, что он зарегистрирован в Google. например

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