Mena
Mena

Reputation: 2029

auth attempt failing in Laravel 5.4

I have some post about auth attempt failure but my case seems to be different. Still in dev phase so my password is in plain text. I try to login but i keep getting false and so redirected back to login page.

The error message says username/password does not match but dd reveals that both email and password are correct.

What could be responsible for this failure? PS: it's my first time working with laravel

web.php

Route::post('/login', 'AuthController@authenticate');
Route::get('/', 'PostController@index');

AuthController

public function auth()
{
    //dd($request);

    // attempt to login the user
    if (! auth()->attempt(request(['email', 'password']))) {

        return back()->withErrors([
            'message' => 'Username/Password does not macth'
        ]);
    }

    return redirect('/');
}

PostController

public function index()
{
    $posts = Post::latest()->limit(3)->get();

    return view('post.index', compact('posts'));
}

Upvotes: 2

Views: 3958

Answers (2)

dparoli
dparoli

Reputation: 9161

Use this code in your User model and the password will be hashed automatically only if it needs:

public function setPasswordAttribute($value)
{
    if( \Hash::needsRehash($value) ) {
        $value = \Hash::make($value);
    }
    $this->attributes['password'] = $value;
} 

and change your password after, so you have the hashed password in the database

Upvotes: 6

JeffBeltran
JeffBeltran

Reputation: 739

Not sure i understand... but if you are using the Laravel Authentication (php artisan make:auth) you will not be storing the password in plain text... so if you are setting the password directly in your db it will not work as it will check the password field in the db with the assumption that it is stored with a dbcrypt hash...

So if you are using the default auth that comes with laravel, use the registration form to create your user account

Since it's your first time working with laravel i would recommend taking a look at https://laracasts.com/series/laravel-from-scratch-2017/episodes/17 as it covers the auth concept and gives you a quick walkthrough on setting up user auth

Upvotes: 0

Related Questions