Fuxi
Fuxi

Reputation: 7589

Laravel5: Auth::attempt not working as expected

i've read the Laravel 5 Docs about Authentication but couldn't manage to Login as described.

My users table contains both fields, username and password (which is type TEXT), i've stored a hashed password which is like $2y$10$XCyEOyvC6Yp/O6HaeemPheO4KV1I8aEMUytZZt77Yjw9hp/j6uivWnope

here's my code:

public function validateLogin()
{
    $email = Input::get('email');
    $pass = Input::get('password');
    $pass = Hash::make($pass);

    $credentials = [
            'username' => $email,
            'password' => $pass
    ];
    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return "passed";
    }else{
        return "nope";
    }
}

I've tried anything, Auth::attempt always returns false :( Imo it's exactly as described in the Docs, anyone knows what's wrong? Thanks

Upvotes: 1

Views: 90

Answers (2)

Rameez Rami
Rameez Rami

Reputation: 5728

did u write the function in AuthController? or in any new/other controller?

if u wrote the function in other controller make sure you have added this code at top

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;




public function login()
    {
        $email = Input::get('email');
        $password = Input::get('password');
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
        else{
             return redirect()->intended('admin/login');
        }
    }

Upvotes: 0

Glad To Help
Glad To Help

Reputation: 5387

You probably don't need to do the $pass = Hash::make($password) call by yourself.

Try removing that line see if it works.

From the docs:

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Routing\Controller;

class AuthController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
}

The attempt method accepts an array of key / value pairs as its first argument. The values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the hashed password value passed to the method via the array.

For further reference check:

http://laravel.com/docs/master/authentication#authenticating-users

Upvotes: 2

Related Questions