Framework: [5.3] Ability to override email field to username on login

Created on 15 Aug 2016  路  3Comments  路  Source: laravel/framework

Can the email field used at login to authenticate to the users table in Laravel 5.3 be overwritten to use the username field like we did in prior version calling this protected method in App/Http/Controllers/Auth/LoginController.php

/**
     * username or email field.
     *
     * @var string
     */
    protected $username = 'username';

Method needing to be changed is in file vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

/**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'email';
    }

This used to be the method in Laravel 5.2 that would allow the override

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

Any Ideas?

Most helpful comment

Was able to override the email field to username the by putting the following in Auth/LoginController.php

     /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'username';
    }

All 3 comments

I don't find similar code in 5.3, but yes, it was in 5.2.
Finally, I override AuthenticatesUsers trait to my own.

Simply override the AuthenticatesUsers trait in your auth controller.

Was able to override the email field to username the by putting the following in Auth/LoginController.php

     /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'username';
    }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

ghost picture ghost  路  3Comments

progmars picture progmars  路  3Comments