mobeen
mobeen

Reputation: 188

PHP Laravel auth is not working properly

My auth function is always returning false in my UserController function, here is my controller function.

public function postSignin(Request $request)
{
    $userdata = array(
        'email'      => $request->input('email'),
        'password'   => $request->input('password')
    );

    if (Auth::attempt($userdata))
    {
        return redirect()->route('user.profile');
    }
    else
    {
        return redirect()->back();
    }
}

and here is my route code

Route::post('/signin', [
    'uses' => 'UserController@postSignin' , 'as' => 'user.signin'
]);

When I enter the credentials, the if part of auth is not working and I am redirected to my page as instructed by the else condition...

Upvotes: 1

Views: 223

Answers (1)

Dastur
Dastur

Reputation: 714

You may be saving your users passwords as plain text into the database, because when using the Auth::attempt() function laravel automatically hashes the password to compare. Make sure when inserting you properly hash the password with the bcrypt() helper function. The only other possibility I can think of is if your sure your putting in the right password or username?

Example of bcrypt when creating a new user:

User::create([
    "username" => $username,
    "password" => bcrypt($password);
]);

Hope this helps!

Upvotes: 2

Related Questions