Can't authenticate user

Published 2 months ago by Ghaleon

I'm struggling myself trying to find the reason but can't. I'm using sqliteand this is my code:

    public function doLogin(Request $request)
    {
        $email          = $request['email'];
        $password       = $request['password'];

        if ( Auth::attempt(['email' => $email, 'password' => $password]) )
            return redirect()->route('home');
        return redirect()->back(); 
    }

Already printed the variables, the values are exatcly the same as the database.

Possible cause of the problem ?

Testing the Variable Receiving Value (Prints the right values)

$usuario = \App\User::find(1)->pluck('email', 'password');  
return ($usuario);  

Printed Value:

{"teste123":"[email protected]"}
Best Answer (As Selected By Ghaleon)
Jaytee

@bexter989 You don't hash the password during the login process. the attempt() method will hash it behind the scenes. You just pass the plain string.

However, you do need to has the password when creating an account. If you're just passing a plain password, it's being stored as a plain password. Then when you go to authenticate, it's checking whether the hashed version of the password matches the one stored in the database.

So make sure you're hashing your password upon registration.

tisuchi
tisuchi
2 months ago (262,085 XP)

Isn't that suppose to be like this-

$request->input('email')

instead of -

$request['email']

Note: Make sure that you are able to receive user's data properly.

Ghaleon

@tisuchi Thanks for the reply ! As I mentioned, already checked the values from the variables and I'm getting the correct values.

tisuchi
tisuchi
2 months ago (262,085 XP)

Change your function a bit, hope it will work-

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

    if ( Auth::attempt(['email' => $email, 'password' => $password]) ){
        return redirect()->route('home');
    }

    return redirect()->back(); 
}

Make sure that your form action is POST.

Ghaleon

Hi, @tisuchi Thanks again !

Already checked. the form is method=POST. Unfortunatelly the strange behavior remains =(

<form method="POST" action="{{ route('doLogin') }}"  class="login-form">
      {{ csrf_field() }}
....
tisuchi
tisuchi
2 months ago (262,085 XP)

Can you run following code and show the output after submitting your form?

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

    dd($request->all());

}
Ghaleon

Done ! It's NULL

array:4 [▼
  "_token" => "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
  "email" => "[email protected]"
  "password" => "teste123"
  "action" => null
]
tisuchi
tisuchi
2 months ago (262,085 XP)

is that working now?

Ghaleon

Nop. Same as before =\

tisuchi
tisuchi
2 months ago (262,085 XP)

can you show ur database structure?

Ghaleon
tisuchi
tisuchi
2 months ago (262,085 XP)

It seems everything is working fine. How about your route?

Ghaleon

Told ya. I'm getting crazy with this, man. No idea what's causing it.

Route::post('/doLogin',['as' => 'doLogin', 'uses' => 'Auth\[email protected]']);  
tisuchi
tisuchi
2 months ago (262,085 XP)

Hay, in your question, you mentioned that, based on your code-

$usuario = \App\User::find(1)->pluck('email', 'password');  
return ($usuario);  

You got this Value:

{"teste123":"[email protected]"}

If your printed output is correct in order, than there is something wrong because you pluck email then password but you are getting password before email.

If so, you need to correct your login a bit.

Ghaleon

I think that's normal for pluck. Because if I print them separetely it works fine, both email and password...

tisuchi
tisuchi
2 months ago (262,085 XP)

Yes, you are right.... clueless...!!!

Please sign in or create an account to participate in this conversation.