Created
March 12, 2017 16:42
-
-
Save SaeedPrez/b3d35af0bb039e8b8d5caf85ec3b2476 to your computer and use it in GitHub Desktop.
Laravel 5.4 Additional Login Conditions - Add custom error message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
thanks a lot.
Thank you very much!
Thanks man, omg !!
Thank you!
Thank you friend, its very good code...
Thank you sir... It help me a lot..
Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice. thanks a lot.