inchirags@gmail.com Chirag's Laravel Tutorial https://www.chirags.in
*********************************************************************************************
Laravel 11 - Login with OTP valid for 10 minutes - Part 2
*********************************************************************************************
YouTube Link:
Tutorial Part 1 link:
https://www.chirags.in/index.php/155/laravel-11-login-with-otp-valid-for-10-minutes-part1
-------------------------------------------Continue from part 1-----------------------------------------------------
Step 17 : Create method for confirmloginwithotppost inside the OTPController.php controller.
public function confirmloginwithotppost(Request $request)
{
//dd($request->all());
$request->validate([
'email' => 'required|email',
'otp' => 'required',
]);
$checkUser = User::where('otp',$request->otp)->where('email', $request->email)->first();
if(is_null($checkUser) ){
return redirect()->route('confirm.login.with.otp')->with('error','OTP or Email is incorrect.');
} else {
$now = now();
//echo $now."=".$now->isAfter($checkUser->expire_at); exit();
$expireAtTime = Carbon::createFromFormat("Y-m-d H:i:s", $checkUser->expire_at);
$currentTime = Carbon::createFromFormat("Y-m-d H:i:s", date("Y-m-d H:i:00"));
# count difference in minutes
$minutesDifference = $expireAtTime->diffInMinutes($currentTime);
//echo $minutesDifference; exit();
if($minutesDifference > 10){
return redirect()->route('confirm.login.with.otp')->with('error', 'Your OTP is valid for 10 min only or Your OTP has been expired.');
}
$user = User::where('email',$request->email)->first();
if($user){
$userupdate = User::where('email',$request->email)->update([
'otp' =>NULL,
'expire_at' =>NULL,
]);
Auth::login($user);
return redirect()->route('home')->with('success','Welcome to user dashboard.');
}
return redirect()->back()->with('error','Login with otp failed.');
}
}
Step 18 : create confirmloginwithotp.blade.php page inside the "resource->views->auth" folder.
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Confirm Login With OTP') }}</div>
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
@endif
@if(Session::has('error'))
<div class="alert alert-danger">
{{ Session::get('error') }}
</div>
@endif
<div class="card-body">
<form method="POST" action="{{ route('confirm.login.with.otp.post') }}">
@csrf
<div class="row mb-3">
<label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" 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>
</div>
<div class="row mb-3">
<label for="otp" class="col-md-4 col-form-label text-md-end">{{ __('OTP') }}</label>
<div class="col-md-6">
<input id="otp" type="password" class="form-control @error('otp') is-invalid @enderror" name="otp" value="{{ old('otp') }}" required>
@error('otp')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Step 19 : Now open in browser and test the pages.
A. Register yourself with valid email address.
http://127.0.0.1:8000/register
B. go to Login with OTP page.
http://127.0.0.1:8000/login-with-otp
After putting the email address the submit the page. You will get an email with OTP and OTP is valid for 10 minutes.
C. now goto
http://127.0.0.1:8000/confirm-login-with-otp
It will ask you the email and OTP. Provide the registered email address and OTP then click on submit. You ll be loggedin now.
Thankyou :)
For any doubts and query, please write on YouTube video comments section.
Note : Flow the Process shown in video.
Please, Subscribe and like for more videos:
https://www.youtube.com/@chiragstutorial
Don't forget to, Follow, Like, Share &, Comment
Thanks & Regards,
Chitt Ranjan Mahto "Chirag"
_____________________________________________________________________
Note: All scripts used in this demo will be available in our website.
Link will be available in description.
#chirags
#chiragstutorial
#chiragslaraveltutorial
#chiragslaraveltutorials
#laraveltutorial
#laravel11
#laravelcourse
#installlaravel
#laravel_tutorial
#laravelphp
#chiragdbatutorial
#chiragsdbatutorial
#chriagstutorial
#laravel11Cart
#laravelShoppingCart
chirags, chirags tutorial, chirags laravel tutorial, chirags Laravel tutorial, Laravel tutorial, laravel11, Laravel course, install laravel, laravel_tutorial, Laravel php, chirags dba tutorial, chirags tutorial, chirag tutorial, Add Product to Shopping Cart in Laravel 11