How to customize authentication fields in Laravel 6

Asked

Viewed 358 times

1

I’m trying to do a custom login with Laravel 6, but it’s not working.

I have the following code:

public function postLogin(Request $request)
{
    request()->validate([
        'email' => 'required',
        'senha' => 'required',
    ]);

    $credentials = $request->only('email', 'senha'); 

    if (Auth::attempt($credentials)) { 
        return redirect()->intended('dashboard');
    }
    return Redirect::to("login")->withSuccess('Oppes! You have entered invalid credentials');
}

Knowing that, Laravel works with its standard variables like email and password, I already changed in model

class User extends Authenticatable
{
   use Notifiable;

   protected $table = 'usuarios';
   public $timestamps = false;

   protected $fillable = ['email', 'senha', 'nivel_id', 'data_cadastro', 'status', 'assinante', 'site_id', 'tipo_conta_id'];

}

In BD the columns are with the names of: "email" and "password", I believe I have to change this name pattern, instead of "password", change to "password", because when I try to debug Auth::attempt($credentials) he always returns false.

  • Already tried to specify the name of the fields in Auth::attempt()? guy Auth::attempt(['email' => $credentials['email'], 'senha' => $credentials['senha'])

  • I took the dd test (Auth::Attempt(['email' => $credentials['email'], 'password' => $credentials['password'] ]); and false return still

  • one option is to exchange your bank for password then

  • the problem that the bank is not my kkkk

  • for testing, I changed here in the database to password and returned true, now I need to know how to do it in the Language

  • you can create a new Migration to change this field

  • related: https://answall.com/questions/381991/fazendo-login-no-laravel-password-sem-hash/381997#381997

Show 3 more comments

1 answer

2


Change your user model by overriding the method that returns the name of the password field:

public function getAuthPassword(){  
    return $this->senha;
}

In the form keep the input name as "password".

  • 1

    exactly that, thank you my friend, solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.