Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Created April 19, 2017 07:33
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save joseluisq/fb84779ea54eaebf54a9d8367117463e to your computer and use it in GitHub Desktop.
Save joseluisq/fb84779ea54eaebf54a9d8367117463e to your computer and use it in GitHub Desktop.
How add a custom field to Laravel 5.4 default login. LoginController.php

How add a custom field to Laravel 5.4 default login controller.

In this php example (app/Http/Controllers/Auth/LoginController.php) my model is called Client and the custom field for login validation is status. (Client->status)

Add in your resources/lang/en/auth.php file :

    'failed_status' => 'Your account is inactive yet. Please confirm your e-mail address.',

Feel free to customize!

<?php
namespace App\Http\Controllers\Auth;
use App\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
*/
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
// Customization: Validate if client status is active (1)
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// Customization: Validate if client status is active (1)
$email = $request->get($this->username());
// Customization: It's assumed that email field should be an unique field
$client = Client::where($this->username(), $email)->first();
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
// Customization: If client status is inactive (0) return failed_status error.
if ($client->status === 0) {
return $this->sendFailedLoginResponse($request, 'auth.failed_status');
}
return $this->sendFailedLoginResponse($request);
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
$credentials = $request->only($this->username(), 'password');
// Customization: validate if client status is active (1)
$credentials['status'] = 1;
return $credentials;
}
/**
* Get the failed login response instance.
*
* @param \Illuminate\Http\Request $request
* @param string $field
* @return \Illuminate\Http\RedirectResponse
*/
protected function sendFailedLoginResponse(Request $request, $trans = 'auth.failed')
{
$errors = [$this->username() => trans($trans)];
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
}
}
@mohamedhakika
Copy link

mohamedhakika commented Jun 22, 2017

Thanks this worked for me but, I had to change few things
($client->status === 0) to ($client->status == 0) and there is a need to check for the existence of the user before comparing
if($client){ if ($client->status == 0) { return $this->sendFailedLoginResponse($request, 'auth.failed_status'); } }
without this it will throw an error if the email or username does not exist in the database.

@mubassirhayat
Copy link

Awesome, Thanks

@mfssangadji
Copy link

good ... its work!

@yuanjianhua
Copy link

App\Client not found

@plash29
Copy link

plash29 commented Aug 31, 2017

If email is not exist the its not working

@nguyenducdev
Copy link

good! Awesome, Thanks !

@andri-sudarmawijaya
Copy link

good! Awesome,
I'll try it in my production site.
Thanks !

@acv-thanhnv
Copy link

Thanks you !

@webtamizhan
Copy link

webtamizhan commented Nov 21, 2018

For laravel 5.5 above use
throw ValidationException::withMessages([ $this->username() => [trans('auth.failed_status')], ]);

@MohammedMohammedBayomy
Copy link

You're The Best , Many Thanks For Your Efforts

@abdulrehman3725
Copy link

abdulrehman3725 commented Oct 10, 2020

@webtamizhan
For Laravel 5.5 or above use

`protected function sendFailedLoginResponse(Request $request, $trans = 'auth.failed')

{

    throw ValidationException::withMessages([ $this->username() => [trans($trans)], ]);

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment