Ad

How To Reset Password Manually Via Answering The Security Questions Without Sending Email (Laravel/Vuejs)

- 1 answer

I am currently developing a simple Bookstore application with a few numbers of users on which sending emails are not needed because it will be implemented in the local system so is there any way to customize laravel-auth for password reset function by adding a few security questions fields where user can reset his/her password without sending reset links via email.

Any kind of help will be highly appreciated.

here I tried the below code but id did not work

Code in web.php

Route::post('/main/checklogin', '[email protected]');

Code in userContoller

 public function chekQuestions(Request $request)
    {
          $request->validate( [
         'email'    => 'required|string|email',
         'answerQuestionOne' => 'required|string|confirmed',
         'answerQuestionTwo'    => 'required|string'
      ] );
    $user = User::first();

        if($user->email == $request->email && $user->answerQuestionOne == $request->answerQuestionOne && $user->answerQuestionTwo == $request->answerQuestionTwo )
         {
            // $userEmail = DB::table( 'password_resets' )->where( 'token', $user->token );
            // return view('auth.password.reset',compact($userEmail));
            return view('auth.password.reset');
         }

           return response()->json( [
         'error'   => true,
         'message' => 'We cannot find a user with that Email Address'
      ], 404 );
    }

Code in reset password.blade

    <div id="register" class="animate form registration_form">
      <section class="login_content">
      <form method="POST" action="{{ url('/main/checklogin') }}" >
                    @csrf
          <h3>د پټ نو بیا راګرځولو لپاره لاندی امنتی پوښتنو ته ځواب ورکړی </h3>
          <div class="form-group has-feedback">
            <input id="email" type="email" placeholder=" ایمل" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>

                            @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
            <div class="form-control-feedback">
              <i class="fa fa-envelope-o text-muted"></i>
            </div>
          </div>
        <div>
           <input id="answerQuestionOne" placeholder="لومړۍ امنیتي پوښتنه" type="text"   class="form-control @error('answerQuestionOne') is-invalid @enderror" name="answerQuestionOne" value="{{ old('answerQuestionOne') }}" required autocomplete="answerQuestionOne" autofocus>

                            @error('answerQuestionOne')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
        </div>
        <div>
            <input id="answerQuestionTwo" placeholder="دوهمه امنیتي پوښتنه " type="text" class="form-control @error('answerQuestionTwo') is-invalid @enderror" name="answerQuestionTwo" value="{{ old('answerQuestionTwo') }}" required autocomplete="answerQuestionTwo" autofocus>

                            @error('answerQuestionTwo')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
        </div>

          <button type="submit" class="btn btn-default btn-block">خوندی کړی </button>



          <div class="clearfix"></div>
           <div class="separator">
            <p class="change_link">
              <a target="_blank" rel="nofollow noreferrer" href="#signin" class="to_register">  تاسو دمخه غړی یاست ننوتل </a>
            </p>

            <div class="clearfix"></div>
            <br />
        </form>
      </section>
    </div>
Ad

Answer

Try this

Code in the controller

public function getview(Request $request)
{
    $user=new User();
      $anserone= $request->answerQuestionOne;
      $anstwo = $request->answerQuestionTwo;
      return view('auth.question',compact('user','anserone','anstwo'));
}

public function chekQuestions(Request $request)
{
    $this->validate($request, [
        'email' => 'required|string|email',
        'answerQuestionOne' => 'required',
        'answerQuestionTwo' => 'required'
    ]);

    $user = User::where('email', $request->email)->first();

    if ( $user == null) {
        //
        $user=new User();
        $user->email= $request->email;
        $anserone= $request->answerQuestionOne;
         $anstwo = $request->answerQuestionTwo;
        Session::flash('message', 'دا ایمل شتون نلری');
        return view('auth.question',compact('user','anserone','anstwo'));

    }
       if ( $user->answerQuestionOne == null || $user->answerQuestionTwo == null) {
        //
        $user=new User();
        $user->email= $request->email;
        $anserone= $request->answerQuestionOne;
         $anstwo = $request->answerQuestionTwo;

        Session::flash('message4', 'هیڅ مورد نشته، لمړی تاسی خپل امنیتی ځوابونه خوندی کړی');
        return view('auth.question',compact('user','anserone','anstwo'));

    }
       if ($user->answerQuestionOne != $request->answerQuestionOne) {
            $anserone= $request->answerQuestionOne;
            $anstwo = $request->answerQuestionTwo;
            Session::flash('message2', 'دپښتنو ځوابونه مو ناسم وو');
            return view('auth.question',compact('user','anserone','anstwo'));
    }
      if ( $user->answerQuestionTwo != $request->answerQuestionTwo) {
          $anstwo = $request->answerQuestionTwo;
          $anserone= $request->answerQuestionOne;

        Session::flash('message3', 'دپښتنو ځوابونه مو ناسم وو');
            return view('auth.question',compact('user','anstwo','anserone'));
    }

    return view('auth.updatePassword',compact('user'));
}
public function updagePassword(Request $request)
{
    $this->validate($request, [
        'email' => 'required|string|email',
        'password' => 'required|min:8'
    ]);
    // $user = User::first();
    $user = User::where('email', $request->email)->first();
    if ( $user == null) {
        //
        $user=new User();
        $user->email= $request->email;
        // $anserone= $request->answerQuestionOne;
        //  $anstwo = $request->answerQuestionTwo;
        Session::flash('message', 'دا ایمل شتون نلری');
             return view('auth.updatePassword',compact('user'));

    }

     if ($request->password_confirmation != $request->password) {
            Session::flash('message3', 'پټ نوم مطابقت نلری');
             return view('auth.updatePassword',compact('user'));
    } 

         $user->where('email', $request->email)->update([
            'password' => Hash::make($request->password)
        ]);
        return redirect()->route('login');

}

**code in updatePassword.blade**

@extends('layouts.header')
 <body class="login">
    <div>
      <a class="hiddenanchor" id="signup"></a>
      <a class="hiddenanchor" id="signin"></a>
      <a class="hiddenanchor" id="reset"></a>

      <div class="login_wrapper">
          <section class="login_content">
          <form method="POST" action="{{route('update.question')}}" >
                        @csrf
             <h3>د پټ نوم تازه کړی </h3>
              <div class="form-group has-feedback">
              <input id="email" type="email" placeholder=" ایمل" class="form-control "name="email" value="{{$user->email}}" required autocomplete="email" autofocus>

                                 @if(Session::has('message'))
                           <p  class="bg-danger">{{session('message')}} </p>

                         @endif
                <div class="form-control-feedback">
                  <i class="fa fa-envelope-o text-muted"></i>
                </div>
              </div>

            <div>

                <input id="password" type="password" placeholder="پټ نوم" class="form-control" name="password" required minlength="8">
                          {{-- <input id="answerQuestionOne" placeholder="ځواب مو دلته ولیکی" type="text"   class="form-control" name="answerQuestionOne" value="{{$anserone}}"  required autocomplete="answerQuestionOne" autofocus> --}}
                                 @if(Session::has('message2'))
                           <p  class="bg-danger">{{session('message2')}} </p>

                         @endif
            </div>

            <div>
                <input id="password-confirm" type="password" placeholder="دپټنوم تأیید "  class="form-control" name="password_confirmation"  minlength="4" required >
            {{-- <input id="answerQuestionTwo" placeholder="ځواب مو دلته ولیکی " type="text" class="form-control " name="answerQuestionTwo" value="{{$anstwo}}" required autocomplete="answerQuestionTwo" autofocus> --}}
   @if(Session::has('message3'))
                           <p  class="bg-danger">{{session('message3')}} </p>

                         @endif
            </div>

              <button type="submit" class="btn btn-default btn-block">خوندی کړی </button>



              <div class="clearfix"></div>
               <div class="separator">
                {{-- <p class="change_link">
                  <a target="_blank" rel="nofollow noreferrer" href="#signin" class="to_register">  تاسو دمخه غړی یاست ننوتل </a>
                </p> --}}

                <div class="clearfix"></div>
                <br />
            </form>
          </section>
        </div>
        {{-- reset password via email --}}

      {{-- </div> --}}
    </div>
  </body>
</html>

**Code in question.blade**

@extends('layouts.header')
 <body class="login">
    <div>
      <a class="hiddenanchor" id="signup"></a>
      <a class="hiddenanchor" id="signin"></a>
      <a class="hiddenanchor" id="reset"></a>

      <div class="login_wrapper">
          <section class="login_content">
          <form method="POST" action="{{route('check.question')}}" >
                        @csrf
              <h3>د پټ نو بیا راګرځولو لپاره لاندی امنیتی پوښتنو ته ځواب ورکړی </h3>
    <div>    @if(Session::has('message4'))
                           <h4  class="bg-danger">{{session('message4')}} </h4>

                         @endif</div>
              <div class="form-group has-feedback">
              <input id="email" type="email" placeholder=" ایمل" class="form-control "name="email" value="{{$user->email}}" required autocomplete="email" autofocus>

                                 @if(Session::has('message'))
                           <p  class="bg-danger">{{session('message')}} </p>

                         @endif
                <div class="form-control-feedback">
                  <i class="fa fa-envelope-o text-muted"></i>
                </div>
              </div>
              <div>
                  <select 

                    style="margin-bottom:20px;width: 100%;padding: 7px; "
                  >
                    <option value selected>مهرباني وکړئ خپله لمومړنی امنيتي پوښتنه وټاکئ</option>
                    <option>ستا د خوښې رنګ کوم دی؟</option>
                    <option>تاسو په کوم ښار کې زیږیدلی یاست؟</option>
                    <option>ستا د خوښې خواړه څه دي؟</option>
                    <option>ستاسو د غوره ملګري لومړی نوم څه شی دی؟</option>
                    <option>کوم ورزش مو خوښ دی؟</option>
                    <option>ستاسو د خوښې سپورټ ټیم څه شی دی؟</option>
                  </select>

              </div>
            <div>


                          <input id="answerQuestionOne" placeholder="ځواب مو دلته ولیکی" type="text"   class="form-control" name="answerQuestionOne" value="{{$anserone}}"  required autocomplete="answerQuestionOne" autofocus>
                                 @if(Session::has('message2'))
                           <p  class="bg-danger">{{session('message2')}} </p>

                         @endif
            </div>
             <div>
                  <select  style="margin-bottom:20px;width: 100%;padding: 7px; ">
                    <option value selected>مهرباني وکړئ خپله دوهمه امنيتي پوښتنه وټاکئ</option>
                    <option>ستا د خوښې رنګ کوم دی؟</option>
                    <option>تاسو په کوم ښار کې زیږیدلی یاست؟</option>
                    <option>ستا د خوښې خواړه څه دي؟</option>
                    <option>ستاسو د غوره ملګري لومړی نوم څه شی دی؟</option>
                    <option>کوم ورزش مو خوښ دی؟</option>
                    <option>ستاسو د خوښې سپورټ ټیم څه شی دی؟</option>
                  </select>

              </div>
            <div>
            <input id="answerQuestionTwo" placeholder="ځواب مو دلته ولیکی " type="text" class="form-control " name="answerQuestionTwo" value="{{$anstwo}}" required autocomplete="answerQuestionTwo" autofocus>
   @if(Session::has('message3'))
                           <p  class="bg-danger">{{session('message3')}} </p>

                         @endif
            </div>

              <button type="submit" class="btn btn-default btn-block">خوندی کړی </button>



              <div class="clearfix"></div>
               <div class="separator">
                {{-- <p class="change_link">
                  <a target="_blank" rel="nofollow noreferrer" href="#signin" class="to_register">  تاسو دمخه غړی یاست ننوتل </a>
                </p> --}}

                <div class="clearfix"></div>
                <br />
            </form>
          </section>
        </div>
        {{-- reset password via email --}}

      {{-- </div> --}}
    </div>
  </body>
</html>
Ad
source: stackoverflow.com
Ad