-1

first of thank you for looking into my problem

I got a bug where even with the right credentials, the authentication returns false. I tested the $request and it worked. The connection to the database also works without flawes.

p.s. I know that the password usually needs to be hashed, I tryed it first with the standard hashing, same result as stated above

used: Laravel 8 Apache MySQL xampp

LoginController.php

public function store(Request $request){

        $this->validate($request,[
            'email' => 'required|email',
            'password' => 'required',
        ]);
        
        dd(auth()->attempt(['playerMail'=>$request->email,'password'=>$request->password]));
}

Migration of the User

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('playerName');
            $table->string('playerMail')->unique();
            $table->timestamp('email_verified_at');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

Model

class User extends Authenticatable
{
    use HasFactory, Notifiable;


    protected $fillable = [
        'playerName',
        'playerMail',
        'password',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];


    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}
user14136803
  • 1
  • 1
  • 2
  • the password needs to be hashed in the database, the plain text version is passed to `attempt` ... also assuming you are using the default auth setup here – lagbox Jul 12 '21 at 13:54
  • use the index `email` for the array `dd(auth()->attempt(['email'=>$request->email,'password'=>$request->password]));` – N69S Jul 12 '21 at 14:11
  • @N69S they don't have an `email` column on that table – lagbox Jul 12 '21 at 14:15
  • @lagbox yop, but the default laravel auth uses specificaly those indexes, so he need to rename the column `email`. or use his own `attempt()` method. – N69S Jul 12 '21 at 14:19
  • 2
    @N69S no ... `attempt` takes the credentials and does where conditions with them except for the password field ... the only field in the credentials that specifically has to be a certain name is `password` ... right from the docs "In these examples, `email` is not a required option, it is merely used as an example. You should use whatever column name corresponds to a "username" in your database table." – lagbox Jul 12 '21 at 14:20
  • @lagbox then you need to overload the method `getAuthIdentifierName()` in your model to overload the one in Authenticatable class – N69S Jul 12 '21 at 14:26
  • @N69S not for use with `attempt` you don't ... the only thing on the model that would have to be adjusted is if the password field wasn't named 'password' in the database for use with `attempt` .... again, every element in the credentials except for 'password' is just a where condition on the query ... which is why you can add things like `'active' => 1` to the credentials – lagbox Jul 12 '21 at 14:27
  • https://github.com/laravel/framework/blob/8.x/src/Illuminate/Auth/EloquentUserProvider.php#L106 you can see it just adding where conditions for each item in the credentials and excluding the 'password' field, that is all it is doing – lagbox Jul 12 '21 at 14:33

1 Answers1

-1

my dummy head didnt use email as the column name in the database. thank you to @N69S

I also needed to hash the password, otherwise it dosnt work out of the box. thank you @lagbox

user14136803
  • 1
  • 1
  • 2