Auth::attempt always return false

I have a problem with laravel 6.x authentication. Auth::attempt() always return false. please help me. Thanks all!

my User.php

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Comment;
use App\Models\Post;
use App\Models\Role;

class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;
    protected $table = "users";
    protected $dates = ['deleted_at'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'role_id', 'admin'
    ];

    /**
     * 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',
    ];

    public function comments()
    {
        return $this->hasMany(Comment::class, 'user_id', 'id');
    }

    public function posts()
    {
        return $this->hasMany(Post::class, 'user_id', 'id');
    }

    public function role()
    {
        return $this->belongsTo(Role::class, 'role_id', 'id');
    }

}

my UserController.php

public function loginAdmin(Request $request)
    {
        if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
            return redirect('admin');
        } else {
            return redirect('admin/login')->with('notify', 'Đăng nhập không thành công !');
        }
    }

my form login

<form class="login-form" action="admin/login" method="POST">
                @csrf
                <div class="form-group">
                    <div class="form-label-group">
                        <input type="email" id="inputEmail" name="email" class="form-control"
                               placeholder="Email address"
                               required="required" autofocus="autofocus">
                        <label for="inputEmail">Email address</label>
                    </div>
                </div>
                <div class="form-group">
                    <div class="form-label-group">
                        <input type="password" id="inputPassword" name="password" class="form-control"
                               placeholder="Password"
                               required="required">
                        <label for="inputPassword">Password</label>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary btn-block">Login</button>
            </form>

my function createUser. this is function i use to create user. i using bcrypt after save data but it still not work. i don't know where is my problem. please help me.

function createUser($attributes)
    {
        $data = $attributes->all();
        $data['password'] = bcrypt($attributes->passowrd);
        if (!isset($data['admin'])) {
            $data['admin'] = 0;
        }
        if ($data['admin'] == 0) {
            $data['role_id'] = null;
        }
        return $this->create($data);
    }

that's all code about this is my problem. please help me . thanks all!

Upvotes: 0

Views: 75

Answers (1)

Thai Nguyen Hung
Thai Nguyen Hung

Reputation: 1212

There is a typo, please fix as ```$attributes->password````

Upvotes: 1

Related Questions