Auth always returning false

Asked

Viewed 495 times

1

Well I’m developing a login own and in it I have others campos and another tabela, everything goes perfectly but always returns result false.

I changed the file Authenticatable.php inside \Illuminate\AuthZAuthenticatable for :

public function getAuthPassword()
{
   return $this->password; //anteriormente era assim
    return $this->Senha;
}

In my file auth.php inside config is like this:

return [

'defaults' => [
    'guard' => 'web',
    'passwords' => 'pessoas',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'pessoas',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'pessoas',
    ],
],

'providers' => [
    'pessoas' => [
        'driver' => 'eloquent',
        'model' => App\Pessoa::class,
    ],

     'pessoas' => [
         'driver' => 'database',
         'table' => 'pessoas',
     ],
],


'passwords' => [
    'pessoas' => [
        'provider' => 'pessoas',
        'email' => 'auth.emails.password',
        'table' => 'password_resets',
        'expire' => 60,
    ],
  ],
];

Man model Pessoa is like this:

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Pessoa extends Authenticatable
{
   use SoftDeletes;
   protected $fillable = [
    'NmPessoa',
    'CPF',
    'Email',
    'Senha',
    'Telefone',
    'DtNasc',
    'Sexo',
    'FglNewsLater',
    'FglStPessoa',
    'ObsPessoa',
    'FglADM',
    'FglCliente',
    'FglFuncionario',
    'Cidade',
    'Estado',
    'Bairro',
    'Rua',
    'Num',
    'Complemento',
    'CEP'
  ];

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

   protected $primaryKey = 'CdPessoa';
   protected $dates = ['deleted_at'];

  public function setPasswordAttribute($Senha)
  {
    $this->attributes['Senha'] = bcrypt($Senha);
  }
}

In my controller AuthController did so:

use Auth;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class AuthController extends Controller
{

  public function authenticate(Request $request)
  {
    $email = $request->input('email');
    $password = $request->input('senha');


    dd(Auth::attempt(['Email' => $email, 'Senha' => $password]));

    if (Auth::attempt(['Email' => $email, 'Senha' => $password])) {
        // Authentication passed...
        return redirect()->intended('/');
    }
 }
}

And in my HTML:

<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
     {{ csrf_field() }}
    <div class="form-group">
       <input type="email" name="email" lass="form-control" id="email-modal" placeholder="E-mail">
    </div>
    <div class="form-group">
         <input type="password" name="senha" class="form-control" id="password-modal" placeholder="Senha">
    </div>
    <p class="text-center">
       <button type="submit" class="btn btn-primary"><i class="fa fa-sign-in"></i> Entrar</button>
    </p>
 </form>

With all this, what could cause the problem.

Thinking here I think it might be part of senha encrypted, I saved in the bank she bcrypt(Password), already tried to change this part the Attempt() but it did not work and continued giving false. Remember that I have the user in the database.

1 answer

2


No need to change the code that comes with the , because, it has several ways of implementing login of user, go back to before and follow this logic.

First thing into the briefcase config filing cabinet auth.php and has an array providers put inside model your classe Person.

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => Pessoa::class,
        ],

Create in the verification method a instance of class User, and do the research by, email, or by any field (or fields) you wish and at the end of the expression use first(). If the result was favorable and brought a instance of class User, use the authentication method by instance of class User (Authenticate A User Instance) \Auth::login($find); that is effected the authentication of the user, being very similar to the command \Auth::attempt, different, because the responsibility to check the data is the responsibility of the developer.

Simple Example:

$user = new \App\Models\Pessoa();
$find = $user->where('Email', '[email protected]')->first();

if ($find) // se encontrou o usuário
{
    //verifiando a senha texto enviado pelo form
    //comparando com hash gravado no banco
    if (\Hash::check($request->input('senha'), $find->Senha)) 
    {
        //autorizando o login do usuário.
        \Auth::login($find);
    }
}

return redirect('/home');

Link demonstrates other means of authentication:

Other Authentication Methods - Other Methods of Authentication

  • Authenticate A User Instance - By class instance User.
  • Authenticate A User By ID- For the primary key configured in the class User.
  • Authenticate A User Once - Authentication by request (this is not what you need).
  • Brother need the changes, for example my instance needs to be person, I can not have the user as main

  • Brother just make a change to the folder config/auth.php in the key 'providers' => [&#xA; 'users' => [&#xA; 'driver' => 'eloquent',&#xA; 'model' => App\Models\User::class,&#xA; ], put your class in the key model.

  • Ai utilizo su codigo ? Model User ?

  • I did the editing, and you use the code I did or do your way, that method only needs an instance that in your case is pessoa.

  • In the case of hash::check, it already does the password bycrypt itself ?

  • Even the find part is working but the password is giving problem

  • in the case bycrypt is a helper of the stabbing Hash, in the stab you have all the options.

  • I don’t know how you saved the password you need maybe generate again. It happens a lot to confuse the password.

  • Yes, I’ll forehead here, just a moment

  • I’m having trouble saving the password with bcrypt, how should I do it ? I know it’s another question :d

  • takes an instance of the class $p = Pessoa::find(1); and then $p->Senha = bcrypt(123456); and $p->save();

  • automatically by the model has as ?

  • Make a method and run only to record once! then comment the line.

  • OK, just a lie

  • Thanks, solved my problem, actually it was going wrong because I personally generated the hash, so I was not matching with what had in the database.

  • Not at all @Renanrodrigues

Show 11 more comments

Browser other questions tagged

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