Anang Hariyanto N
Anang Hariyanto N

Reputation: 133

I can't get Laravel Auth::attempt working,

I've read a lot of question about it, but i couldn't got right question for my case.

So i'm learning Laravel 8 right now, i want to authenticate user manually, so i want to use Auth::attempt, but it always return false/error, i don't which part oh my code that wrong, hopefully i will got my answer. Here is my code

routes/web.php

Route::post('/login',[LoginController::class,'authenticate']);

login.blade.php

<form action="/login" method="POST">
     @csrf
     <div class="form-group">
          <input type="text" class="form-control" name="username" id="username" placeholder="Username">
     </div>
     <div class="form-group">
          <input type="password" name="password" class="form-control" placeholder="Password">
     </div>
     <div class="form-group">
          <button type="submit" class="btn btn-primary btn-login">LOGIN</button>
     </div>                                   
</form>

LoginController.php

public function authenticate(Request $request){

        //mengecek apakah user ada di database
        if(Auth::attempt(['username'=>$request->username,'password'=>$request->password])){
            echo "Login Suskes";
        }else{
            echo "Login Error";
        }
    }

model/User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'user';
    protected $primaryKey = 'id';
    protected $fillable = [
        'username',
        'password',
        'nama',
        'id_level_user',
        'id_sub_unit'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

I don't know which part is wrong, so i will very thankful if someone help me answer it, it's for my diploma Final Project

Thanks

Upvotes: 1

Views: 1071

Answers (2)

Diogo Gomes
Diogo Gomes

Reputation: 2265

How did you seed your users table?

Maybe you did not encrypt your password correctly. And the check is always returning false because of the incorrect password.

Try to reset your password. There should be a link in your form (if you are using the default auth UI). Or you can go to password/reset in your browser to reset your password.

Set you MAIL_DRIVER=log

And check your log for the link in the email, that link will send you to a form to reset your password.

Upvotes: 0

Harpal Singh
Harpal Singh

Reputation: 702

In your LoginController add the following function:

public function username()
{
    return 'username'; //or whatever field
}

Upvotes: 1

Related Questions