Learn how to change Laravel’s login validation


October 26th, 2016

Learn how to change Laravel’s login validation

Laravel’s included auth system is a great way of scaffolding out a basic flow for authenticating users through a complete registration, login, logout, and forgot password system.

When it’s all setup the login and password reset validation is stored in an AuthenticatesUsers and ResetsPasswords trait. Even though it’s a little hidden away it’s still easy to adjust this to your needs. Let’s take a look at how to adjust this:

Login Validation

By default, the LoginController is a class that only includes the middleware and it uses the Illuminate\Foundation\Auth\AuthenticatesUsers trait. If you open this trait you will find a validateLogin method:

1protected function validateLogin(Request $request)
2 {
3 $this->validate($request, [
4 $this->username() => 'required', 'password' => 'required',
5 ]);
6 }

If you would like to change this an easy way is to copy this whole method and past it into your LoginController and then make any changes you need. Just be sure and add the use line.

3namespace App\Http\Controllers\Auth;
5use Illuminate\Http\Request;
6use App\Http\Controllers\Controller;
7use Illuminate\Foundation\Auth\AuthenticatesUsers;
9class LoginController extends Controller
11 use AuthenticatesUsers;
13 public function __construct()
14 {
15 $this->middleware('guest', ['except' => 'logout']);
16 }
18 protected function validateLogin(Request $request)
19 {
20 $this->validate($request, [
21 $this->username() => 'required',
22 'password' => 'required',
23 // new rules here
24 ]);
25 }

Reset Password Validation

Starting at v5.3.20 the reset password validation works the same way. Inside Illuminate\Foundation\Auth\ResetsPasswords it includes a rules method that you can extend by defining your own in the ResetPasswordController

1protected function rules()
3 return [
4 'token' => 'required', 'email' => 'required|email',
5 'password' => 'required|confirmed|min:6',
6 ];

With Laravel having it setup this way it makes it easy for you to customize to suit your needs while accounting for the common use case.

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.