How to use a customizable name for the password field in Laravel 8?

Asked

Viewed 64 times

0

I’ve tried everything, but nothing works. I’m pretty sure the reason is that the default name of Attempt "password" does not match the name of the passwordhash column,

<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
    public function login(Request $request){
        $data =  $request->all();
        

        if(Auth::guard('admin')->attempt(['login'=>$data['loginadmin'],'password'=>$data['passadmin'],'isadmin'=>1]))
        {
            return 'logou';
        }else{
            return 'naao logou';
        }
        
    }
    
}

My Model User:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends \Illuminate\Foundation\Auth\User
{
    protected $table = "tb_user";
    protected $fillable = [
        'login',
        'passwordhash',
        'fullname',
        'cpf'
    ];
    public $timestamps = false;
    
    
}

I think I’ll need to change this pattern from Attempt "password" to passwordhash, but I have no idea how to do it. Or it might even be another solution and I don’t which.

From now on I’d like to thank anyone who helps me.

  • Voce has tried to change the array key of passsword for passwordhash in the method attempt?

  • worst I ever tried, but when I do it gives an error q the password attribute is null, IE, it is mandatory to use this way

2 answers

0


You need to overwrite the method getAuthPassword in its model of User for him to return the attribute passwordhash, see:

class User extends \Illuminate\Foundation\Auth\User
{
    protected $table = "tb_user";
    protected $fillable = [
        'login',
        'passwordhash',
        'fullname',
        'cpf'
    ];
    public $timestamps = false;
    

    public function getAuthPassword()
    {
        return $this->passwordhash;
    }
}
  • 1

    Dude helped so much, just went to implement that worked, thank you very much.

-1

I would try to look in the Authenticateusers trait, in the validateLogin function, change the password in the validation by the passwordhash, maybe the error is in this validation that is giving the false Return.

Browser other questions tagged

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