Login customizado Laravel

Asked

Viewed 985 times

0

I’m trying to do a custom login on Laravel 5.5, I can even authenticate the user, but when I direct to another page it loses the authenticated user reference and redirects to the login screen again.

My class Logincontroller:

    public function attempt(Request $request)
{

    $this->validate($request, [
        'cpf'   => 'required',
        'login' => 'required',
        'senha' => 'required',
    ]);

    $dados = $request->all();
    $pessoa = Pessoa::where('login', $dados['login'])
        ->where('cpf', $dados['cpf'])
        ->first();

    if ($pessoa == null) {

        return redirect()->back()
            ->with('fail', 'Credenciais não encontradas para o login e CPF informados')
            ->withInput();

    }

    if (md5($dados['senha']) != $pessoa->senha) {

        return redirect()->back()
            ->with('fail', 'Senha incorreta para o login ' . $dados['login'])
            ->withInput();

    }

    if (!$pessoa->ativo) {
        return redirect()->back()
            ->with('fail', 'O login não está ativo')
            ->withInput();          
    }

    \Auth::loginUsingId($pessoa->id);

    return redirect()->route('dashboard.index');



}

And my Model Person:

  
class Pessoa extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $primaryKey = 'id';


    protected $fillable = [
        'nome', 'cpf', 'senha', 'ativo', 'login',
    ];

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

Is there any method or attribute I need to overwrite to resolve this?

2 answers

0

You have some methods that you can override, in your model that replaces the default User, you have to put a data mutator, similar to this:

getPasswordAttribute() { return $this->seu_campo_de_senha; }

I’ll leave 2 posts of mine with this problem. The second was middleware error, so you can catch it, that ta more updated. In it I overwrite various methods that the Laravel Provider back by default.

And remember, in the config/auth.php file to change the User model reference to your model in question.

Oldest

Latest, most complete and most overwritten

Reference

  • Overwriting getAuthPassword did not resolve. Yes, config/auth.php is targeting the Pessoa model. So much so that when I give the loginByUser it is authenticating (if I give an Auth::User just after the loginbyuser line it is returning the user instance), but when I try to get the authenticated user on the next page, it returns nothing.

  • Make sure he’s opening the session. I had an affair that was using Annotations, and it wasn’t web middleware (which is responsible for Session), I had to add in my route notes, the middleware manually.

0


Auth::login($user, true);

must solve

Browser other questions tagged

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