Skip to content

Instantly share code, notes, and snippets.

@SaeedPrez
Created March 12, 2017 16:42
Show Gist options
  • Save SaeedPrez/b3d35af0bb039e8b8d5caf85ec3b2476 to your computer and use it in GitHub Desktop.
Save SaeedPrez/b3d35af0bb039e8b8d5caf85ec3b2476 to your computer and use it in GitHub Desktop.
Laravel 5.4 Additional Login Conditions - Add custom error message.
<?php
// This is code to return custom error message
// for Youtube tutorial: Laravel 5.4 Additional Login Conditions
// https://www.youtube.com/watch?v=Z8s6uhhD1Pk
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
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 = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
/**
* 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');
$credentials['active'] = 1;
return $credentials;
}
/**
* Get the failed login response instance.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
protected function sendFailedLoginResponse(Request $request)
{
$errors = [$this->username() => trans('auth.failed')];
// Load user from database
$user = \App\User::where($this->username(), $request->{$this->username()})->first();
// Check if user was successfully loaded, that the password matches
// and active is not 1. If so, override the default error message.
if ($user && \Hash::check($request->password, $user->password) && $user->active != 1) {
$errors = [$this->username() => 'Your account is not active.'];
}
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
}
}
@gideonp
Copy link

gideonp commented May 20, 2021

Thank you sir... It help me a lot..

@NguyenNhutTien
Copy link

Thank you.

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