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

jcergolj's avatar

Login with username?

Hi, is it possible to modify how Spark logs in user? I would like to use a username instead of an email. Probably modifying Laravel\Spark\Http\Controllers\Auth\LoginController is not the answer. How to set custom redirectUrl after login?

0 likes
9 replies
ejdelmonico's avatar

Well, Spark uses Laravel for auth so it should work. Although, I have not tried to change it in my projects. You should be able to override the username method with

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

It should work. The $redirecTo variable still applies. It is also in the Login Controller. I guess you could try modifying the sample project to see if it breaks anything. Just click on the button for the sample after logging in to spark site.

2 likes
tisuchi's avatar

I think you may simply add in your auth method like this way-

$credentials = [
            'username' => $request->input('username'),
            'password' => $request->input('password'),
            'flag' => 1
        ];
      

        if ( Auth::attempt($credentials) ) {
           //logged in successfully 
        } 
2 likes
kiogo's avatar

Hi, has anyone gotten this to work with 5.4 and Spark 4.0? I tried the method mentioned by @ejdelmonico but am getting a 'the email field is required' notice on login. Thanks

ejdelmonico's avatar

@kiogo In 5.4, you can just override the credentials method in the login controller. Fore example, here is a sample that adds an is_active requirement to login requirements.

/**
  * Override the credential method for login
  *
  * @param \Illuminate\Http\Request $request
  * @return array
*/
protected function credentials(Request $request)
{
    return array_merge($request->only($this->username(), 'password'), 
          ['is_active' => 1]);
}
kiogo's avatar

@ejdelmonico , thanks for that, however I can't figure out how to override the Spark login controller. The controller at app/Http/Controllers/Auth/LoginController.php does not seem to override the one at vendor/laravel/spark/src/Http/Controllers/Auth/LoginController.php

Thanks for any advise.

Cronix's avatar

Correct, Spark doesn't use the default Laravel auth controller.

clat23's avatar

@cronix What does spark use? Need to know as I'm also trying to set up logging in with username instead of email in Spark. I've gotten the user registration part solved. I've changed the login view to accept username instead of email, but I am getting a validation error saying email is required.

clat23's avatar

@kiogo

Jul 8, 2017 Hi, has anyone gotten this to work with 5.4 and Spark 4.0? I tried the method mentioned by @ejdelmonico but am getting a 'the email field is required' notice on login. Thanks

Spark has its own LoginController at app\spark\src\Http\Controllers\Auth\LoginController.php so adding

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

to app\Http\Controllers\Auth\LoginController.php will not do anything as Spark uses its own LoginController as mentioned above. To ensure proper implementation of

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

as per Laravel (not Spark) documentation on "Username Customization" (https://laravel.com/docs/5.8/authentication#included-authenticating) you must add the following to app\routes\web.php:

Route::post('/login', 'Auth\LoginController@login');

Please or to participate in this conversation.