Customizing a Laravel user model 5.4 - Login problem

6

I have tried everything already, but I can not solve a problem with the login system of Laravel which is this: I created a model called Usuarios , I put all the information in it that has to be properly placed, like the array $fillable and $hidden , however internally Laravel still treats as model default login to User model, I was able to change this, through this line:

protected $table = 'Usuarios';

There was another problem, not finding the password field that was default ( default ), I went to the file EloquentUserProvider and changed from password to Senha ( I've been looking for some solution, all I tried was in vain.)

Model code Auth::attempt() :

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $model = 'Usuario';
    protected $increments = false;
    protected $table = 'Usuarios';
    protected $primaryKey = 'UsuarioID';

    public function getAuthIdentifier() {
        return $this->getKey();
    }

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

    public function getReminderEmail() {
        return $this->Login;
    }
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

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

Model code User :

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Usuario extends Authenticatable
{
    use Notifiable;

    /*protected $model = 'Usuario';
    protected $increments = false;
    protected $table = 'Usuarios';
    protected $primaryKey = 'UsuarioID';

    public function getAuthIdentifier() {
        return $this->getKey();
    }

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

    public function getReminderEmail() {
        return $this->Login;
    }*/

    protected $fillable = [
        'UsuarioID', 'Nome', 'Login', 'Senha'
    ];

    protected $hidden = [
        'Senha', 'remember_token',
    ];
}

Function code that logs into the controller:

public function entrar(Request $request)
{
    $dadosFormulario=$request->all();
    $dadosLogin=['Login'=>$dadosFormulario['login'],'Senha'=>$dadosFormulario['senha']];

    if( dd(Auth::attempt($dadosLogin)) ){
        return redirect()->route('admin.cursos');
    } else {
        return redirect()->route('site.home');
    }
}

Here I have checked if the data is coming correctly from the form and any help is welcome.

    
asked by anonymous 02.03.2017 / 19:31

1 answer

8

Note: You do not need to change anything that comes in framework of the vendor folder which is core , if there were any changes go back to what it was.

Walkthrough:

1) Creation of Model Usuarios

<?php namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Usuarios extends Authenticatable
{
    use Notifiable;

    protected $fillable = ['Nome', 'Login', 'Senha'];
    protected $hidden = ['Senha', 'remember_token'];
    protected $primaryKey = 'UsuarioID';
    protected $table = "usuarios";

}

2) Migration Model Usuarios

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Usuarios extends Migration
{
    public function up()
    {
        Schema::create('usuarios', function (Blueprint $table) {
            $table->increments('UsuarioID');
            $table->string('Nome');
            $table->string('Login')->unique();
            $table->string('Senha');
            $table->rememberToken();
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('usuarios');
    }
}

3) Change the configuration of the config\auth.php file in the providers key, informing the new class that will be responsible for authentication:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Usuarios::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

4) Manual creation of the records of the Usuarios table:

$usuario = Usuarios::create(array(
    'Nome' => 'StackOverFlow',
    'Login' => '[email protected]',
    'Senha' => bcrypt('stack')
));

5) To log in with the information contained in the Usuarios table:

$usuario = Usuarios::where('Login', '[email protected]')
                   ->first();

if (Hash::check('stack', $usuario->Senha))
{
    Auth::loginUsingId($usuario->UsuarioID);
}

This is the five steps to autenticar in an application by means of a particular code and the natural process has to be changed to those 5 steps even at the time of logging in.

References

02.03.2017 / 21:42