Skip to content

Instantly share code, notes, and snippets.

@MohammedMohammedBayomy
Forked from joseluisq/1README.md
Created July 17, 2019 12:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohammedMohammedBayomy/3481b4092cbea82258b8989e06582f72 to your computer and use it in GitHub Desktop.
Save MohammedMohammedBayomy/3481b4092cbea82258b8989e06582f72 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment