LARAVEL 5.2 - Using username instead of email in default auth scaffold
Published by Aslam
php artisan make:auth
command to generate default auth scaffold.And you will be able to use laravel's default auth system, which using email and password as credentials.
Now, you want to use "
username
" instead of "email
" in login page, here is how you can do it.STEP 1: OVERRIDING FUNCTION
Lets have a look at default authentication system files,
In
line number : 191
ofvendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php
There will be this function
loginUsername
,/** * Get the login username to be used by the controller. * * @return string */ public function loginUsername() { return property_exists($this, 'username') ? $this->username : 'email'; }
As you can see,
username: 'email';
Lets override this function in
app\Http\Controllers\Auth\AuthController.php
Copy and paste same function from
AuthenticatesUsers.php
and Paste in AuthController.php
/** * Get the login username to be used by the controller. * * @return string */ public function loginUsername() { return property_exists($this, 'username') ? $this->username : 'username'; }
STEP 2: UPDATING VIEWS
Lets update
resources\views\auth\login.blade.php
's login form by replacing email label, email field to textfield with a username label.<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}"> <label class="col-md-4 control-label">Username</label> <div class="col-md-6"> <input type="text" class="form-control" name="username" value="{{ old('username') }}"> @if ($errors->has('username')) <span class="help-block"> <strong>{{ $errors->first('username') }}</strong> </span> @endif </div> </div>