Auth laravel 5.5 with a different table

1

I am trying to authenticate users with a table different from 'users', which is the default Laravel, my table fields are called different in this case "mail" and "password"

I can not do the authentication because I do not recognize the field "password" I only manage to login when the field is called "password"

Is there any way to change this so that I recognize the "password" field? I already tried implementing the getAuthPassword method but it did not work for me.

I have the following code in the LoginController

class LoginController extends Controller
{


    // funcion que solo permite dejar pasar al usuario a la rutas si hace sesion
    public function __construct()
    {
        //$this->middleware('guest')->except('logout');

        $this->middleware('guest', ['only' => 'showLoginForm']);
    }


    // retorna a vista login
    public function showLoginForm(){

        return view('login');
    }


    // funcion para inciar sesion
    public function login(){

        $datos  = $this->validate(request(), [
            $this->username() => 'email|required|string',
            $this->getAuthPassword() => 'required|string'
        ]);


        // valida si lo datos son correctos crea la sesion
        if (Auth::attempt($datos)) {

            return redirect('ventas');
        }

        // si son incorrectos devuelve un mensaje
        return back()
            ->withErrors([$this->username() =>  trans('auth.failed')])
            ->withInput(request([$this->username()]));

            //return $this->getAuthPassword();
    }


    // funcion para cerrar sesion
    public function logout(){


        // cierra sesion y devuelve al login 
        Auth::logout();

        return redirect('/');
    }


    public function getAuthPassword () {

        return "contrasena";

    }

    public function username(){


        return "correo";
    }

}

The following code in the User.php model

class User extends Authenticatable
{

use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'usuarios';
    protected $fillable = [
        'nombres', 'correo', 'contrasena',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}
    
asked by Carolina Velasquez 28.02.2018 в 21:39
source

1 answer

0

The getAuthPassword() method should add it to the User model, basically with this method we can setear which will be the field of our table that will work as password to verify for Auth::attemp

This can be seen in the namespace namespace Illuminate\Auth; there is the class DatabaseUserProvider which has a validateCredentials method that accesses this method in the model to know if seteado has another value.

The if of attemp would be modified

$datos  = $this->validate(request(), [
     $this->username() => 'email|required|string',
     $this->getAuthPassword() => 'required|string'
]);
    // valida si lo datos son correctos crea la sesion
if (Auth::attempt(['correo'=> $datos['correo'] , 'password' => $datos['contrasena'] ] )) 

and your model User should have the following code (at least)

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'usuarios';

    protected $fillable = [
         'nombres', 'correo', 'contrasena',
    ];

    protected $hidden = [
        'contrasena',
    ];

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

}
    
answered by 28.02.2018 / 22:44
source