Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tareenmj's avatar

How to use Authentication using Username instead of email

I want to create an app where I Log in the username based on username, instead of email address. Since I am a beginner to Authentication, I simply ran the php artisan make:auth and the migrate commands. I made no change to the user migration file or any other.

I edited my Login controller by placing a method as such:

class LoginController extends Controller
{

use AuthenticatesUsers;
    
    public function username(){
        return 'username';
    }

//the rest of the controller code

Now my Login Blade file is as:

<form class="form-horizontal" method="POST" action="{{ route('login') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">Username</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

//followed by the rest of the html.

However, as soon as I type in the name and the password, it doesn't allow me to be logged in, and does nothing. I am unsure about what I am doing wrong here, can somebody please help?

0 likes
14 replies
jlrdw's avatar

From the laravel 5.4 documentation:

Username Customization

By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:

public function username()
{
    return 'username';
}
tareenmj's avatar

@jlrdw Yea I already placed that, but it still does not have any effect, is there a possible problem with my View file?

1 like
Dunsti's avatar

the field-name in your view must be username instead of name

2 likes
Tangguh's avatar

I've tried to make changes on

public function username()
{
    return 'username';
}

but it comes out with this error after i change it.

FatalErrorException in User.php line 11: Class App\User contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifierName, Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifier, Illuminate\Contracts\Auth\Authenticatable::getAuthPassword, ...)
Dunsti's avatar

did you use the trait?

use AuthenticatesUsers;
1 like
Snapey's avatar

Our friend @SaeedPrez created a video on youtube because this comes up so much;

https://youtu.be/Qtiyo2J_-tA

Personally I don't get the approach

  1. learn new framework
  2. download and install
  3. use default authentication
  4. oh. cannot get past the point that it wants to use email and in my head I have it that I want to use username so I'll just start hacking around with stuff I have no expertise with yet rather than build the rest of my application and come back to it later
3 likes
jlrdw's avatar

The reason I do not use the built in Auth, I have my own code I like using, and works good. I did try it out, but mine is just easier yet secure. Learned a bit from Chris Shiflet articles and the OWASP site and stuck with that.

I have two sites with a custom framework that has custom Auth and csrf protection. Just a couple tweaks in error handling made the custom framework php 7 compatible.

And I do not need to worry about htmlentities like I know blade does, I have a far better solution

public static function fixValue($rvalue)
    {
        $rvalue = empty($rvalue) && !is_numeric($rvalue) ? NULL : trim(strip_tags($rvalue));
        return $rvalue;
    }

Yes I just strip tags, done. Of course these sites don't store any html. If I had to to that I'd probably use a class like http://htmlpurifier.org/.

I generally do business type stuff.

1 like
Tangaye's avatar

@tareenmj find this file:

use Illuminate\Foundation\Auth\AuthenticatesUsers;

and change the username() method from this:

public function username()
{
    return 'email';
}

to this:

public function username()
{
    return 'username'; //or whatever field
}

hope this helps!

1 like
Snapey's avatar

Ignore that advice from @Tangaye as you should never alter code in the vendor folder as it will be overwritten next time you upgrade the framework.

Use the process in the video mentioned earlier.

1 like
Tangaye's avatar

@Snapey you're right. I followed the documentation and the youtube and applied as instructed. I also noticed that I had to customize the logout method and I did the same. Thanks!

towhid's avatar

@SNAPEY - Brilliant reference - brother :) thank you

SNAPEY answer is best answer :)

towhid's avatar

@JLRDW - you Are 100% right - own code is better then Built in code of any functionality :)

gorakhyadav's avatar

You need is add a property in app/Http/AuthController.php:

  protected $username = 'username';

Open your Illuminate\Foundation\Auth\AuthenticatesUsers.php and ediit

   public function postLogin(Request $request)
   {
    $this->validate($request, [
        $this->loginUsername() => 'required', 'password' => 'required',
     ]);

And then let’s look into loginUsername() method.

  public function loginUsername()
   {
    return property_exists($this, 'username') ? $this->username : 'email';
    }

As you can see, it’s looking for a property $this->username, otherwise defaults it to ’email’.

ghufran's avatar

Hi @snapey I am developing a laravel application that is upgraded from Laravel 5.6 to latest Laravel 8. I followed the instructions mentioned in the video that you shared. and it worked perfect

Thank you very much

Please or to participate in this conversation.