Help Laravel 5.5: auth :: attempt returns false

0

I have a login that worked perfect with the id in its respective table, change the migration so that the primary one was username (in table users), deleting the id, I put in the model the new primaryKey "username", but the auth::attempt always returns false and the data is going well in the form.

User Model

<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;

    protected $primaryKey = 'username';

    public $incrementing = false;

   // protected $primary_key = 'username';

    //public $incrementing = false;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'password', 'tipo_usuario',
    ];

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

LoginController

<?php

namespace App\Http\Controllers\Auth;

use Auth;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest', ['only' => 'showLoginForm']);
    }

    public function showLoginForm()
    {
        return view('auth.login');
    }

    public function login()
    {
        $credentials = $this->validate(request(), [
                        $this->username() => 'required|string|min:4',
                        $this->password() => 'required|string|min:6',
        ]);



        if (Auth::attempt($credentials))
        {
            return $credentials;
            //return redirect()->route('dashboard');
        }

        return back()
                ->withErrors([$this->username() => 'Estas credenciales no coinciden con nuestros registros'])
                ->withInput(request([$this->username()]));
    }

    public function logout()
    {
        Auth::logout();

        return redirect('');
    }

    public function username()
    {
        return 'username';
    }

    public function password()
    {
        return 'password';
    }
}
    
asked by pinisco 31.01.2018 в 13:33
source

1 answer

1

I think you have some code left over. To customize the field you log in, you just need to set it, with the username function the rest ( login (), password () ) Laravel does it

LoginController

protected $redirectTo = '/url hacia dashboard';    

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

public function showLoginForm()
{
    return view('auth.login');
}   
public function logout()
{
    Auth::logout();

    return redirect('');
 }  
public function username()
{
     return 'username';
}
    
answered by 01.02.2018 в 10:07