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

noahkuhn's avatar

Spark Login: allow email AND username

Seems this is straightforward to do with the standard Laravel logins. However, Spark seems to be doing some extra stuff? I simply want my login field to work with either an email or a username. Doesn't seem like it should be that difficult, any advice would be very welcome.

0 likes
3 replies
noahkuhn's avatar

I added this override to: app > Http > Controllers > Auth > LoginController.php

public function username()
{
        $field = filter_var(request('login'), FILTER_VALIDATE_EMAIL) ? 'email' : 'name';
        request()->merge([$field => request('login')]);
        return $field;
}

My form field is set as name="login"

The database fields are email and name respectively.

I end up getting an error saying: "The email field is required." I guess my username function is returning either email or name depending on the input, and then the validation in the AuthenticatesUsers.php trait is not finding any value since my form field is called login?

noahkuhn's avatar

Okay, so I figured out why this wasn't working. Spark sets up the /login POST route to its own implementation of a LoginController, so I don't think I can override the methods in there. Solution is thus:

  1. Setup an override route in routes/web.php
Route::post('/login', 'Auth\LoginController@login');
  1. Change the top of my scaffolded Http/Controllers/Auth/LoginController.php like so:

Add a reference to the Spark LoginController

use Laravel\Spark\Http\Controllers\Auth\LoginController as SparkLoginController;

Then change my LoginController class to extend that

class LoginController extends SparkLoginController

instead of

class LoginController extends Controller

The username() function override then actually gets called and the whole thing seems to work, albeit for a bcrypt error sometimes that now I need to run down...

Please or to participate in this conversation.