简体   繁体   中英

how to authenticate login in laravel using only username and password?

I have a mysql database of users with only 2 field: username and password. I'm trying to build a login page but the authentication function I'm using always returns false, even if the username and password are correct.

my if statement is:

if (auth()->attempt($formFields))

where:

$formFields = $request->validate([
        'user' => 'required',
        'password' => 'required'
    ]);

I tried to check if the problem is in the input i'm getting but it looks fine. from what i found online it appears that this function uses email field to authenticate so i think this might be the problem, but i couldn't find anyway around that...

Try to update your LoginController and put following function there.

public function username()
{
    $identity  = request()->get('identity');
    $fieldName = filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'user';
    request()->merge([$fieldName => $identity]);
    return $fieldName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM