If you want to check additional fields during login in laravel like security questions with username and password then this post will help to achieve that.
For initial setup checkout my previous article about extra fields in registration form.
I assume that you have already set up your laravel project, So, let’s get started to add additional fields in the login form.
To add an extra field (security question) in the login form, go to resources > views > auth > login blade file. Then add an additional field in the login form like given in the code below.
@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">{{ __('Login') }}</div> <div class="card-body"> <form method="POST" action="{{ route('login') }}"> @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="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="security_question" class="col-md-4 col-form-label text-md-end">{{ __('Who is your best friend?') }}</label> <div class="col-md-6"> <input id="security_question" type="text" class="form-control @error('security_question') is-invalid @enderror" name="security_question"> @error('security_question') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <div class="col-md-6 offset-md-4"> <div class="form-check"> <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}> <label class="form-check-label" for="remember"> {{ __('Remember Me') }} </label> </div> </div> </div> <div class="row mb-0"> <div class="col-md-8 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Login') }} </button> @if (Route::has('password.request')) <a class="btn btn-link" href="{{ route('password.request') }}"> {{ __('Forgot Your Password?') }} </a> @endif </div> </div> </form> </div> </div> </div> </div> </div> @endsection
Now, we have modified the login form UI, it will send security question to the server while checking the login credentials. But still it won’t attempt to login with security questions.
To add security questions to login credentials open the login controller.
I have overridden the credentials method to extract the security question from the request object and add to the login credentials.
Because of this method, our security question will also become part of login credentials.
protected function credentials(Request $request) { return $request->only($this->username(), 'password', 'security_question'); }
But, we need to update login form validation also.
To validate the login data, I have overridden the validateLogin
method in the login controller. It is given in the code below.
protected function validateLogin(Request $request) { $request->validate([ $this->username() => 'required|string', 'password' => 'required|string', 'security_question' => 'required|string|min:5|max:255' ]); }
Run the following command to serve your laravel project to the server.
php artisan serve
We can check the form with an empty security question. It will return an error because we have set up the form validation for a security question in the login controller.
Second thing is, if we put the wrong answer for the security question, it will again return an error that credentials are not matched.
Hence we have successfully checked additional fields during login in laravel.
That’s it, everything is done, now we can test this login form. Hope you will find this article useful. Please suggest more topics or any improvement in the comment below.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…