1 Answer

answered by

inchirags@gmail.com   Chirag's Laravel Tutorial https://www.chirags.in

*********************************************************************************************

Laravel 11 - Login with OTP valid for 10 minutes - Part 2

*********************************************************************************************

YouTube Link:

https://youtu.be/W4kRh3F-V70

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

Most popular tags

postgresql laravel replication laravel-10 ha postgresql mongodb ubuntu 24.04 lts mongodb database mongodb tutorial streaming-replication laravel-11 mysql database laravel postgresql backup laravel login register logout database mysql php technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 - login with otp valid for 10 minutes. laravel 11 gaurds user and admin registration user and admin login multiauth postgresql 16 to postgresql 17 postgresql migration zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase ubuntu 24.04 koha 24.05 letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...