desidarling
desidarling

Reputation: 43

Laravel Auth attempt failing

I really try to debug my issues on my own before I bring them here, but I seriously cannot find a solution to my laravel auth problem, though it seems to be a common issue.

My authentication will not login. It always returns false and I don't understand why.

I've read through some other questions here, and their solutions haven't solved my particular situation.

Thank you so much for any insight.

User Model

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $fillable = array('email', 'username', 'password', 'password_temp', 'code', 'active');

    public $timestamps = false; 
    protected $softDelete = false;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'Users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = 'password';

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

}

Account Controller

class AccountController extends BaseController {

public function getLogin() {
    return View::make('account.login');
}

public function postLogin() {
    $validator = Validator::make(Input::all(),
        array(
            'email' => 'required',
            'password' => 'required'
        )
    );

    if($validator->fails()) {
        return Redirect::route('login')
                    ->withErrors($validator);
    } else {

        $auth = Auth::attempt(array(
                'email' => Input::get('email'),
                'password' => Input::get('password'),
                'active' => 1
                ));

            if($auth) {
                return Redirect::route('Create-Account');
            }
        }

        return Redirect::route('login')
                    ->with('global', 'There was a problem logging you in. Please check your credentials and try again.');
}

public function getCreate() {
    return View::make('account.create');
}

public function getviewReturn() {   
    return View::make('account.return');
}

public function postCreate() {

    $validator = Validator::make(Input::all(),
        array(
            'email' => 'required|max:50|email|unique:Users',
            'username' => 'required|max:15|min:4|unique:Users',
            'password' => 'required|min:6',
            'password2' => 'required|same:password'
        )
    );

    if ($validator->fails()) {
        return Redirect::route('Post-Create-Account')
                    ->withErrors($validator)
                    ->withInput();
    }

    else {
        $email = Input::get('email');
        $username = Input::get('username');
        $password = Input::get('email');

        $code = str_random(60);

        $user = User::create(array(
            'email' => $email,
            'username' => $username,
            'password' => Hash::make($password),
            'code' => $code,
            'active' => 0));
});
return Redirect::to('account/return')

Routes

Route::group(array('before' => 'guest'), function() {

Route::group(array('before' => 'csrf'), function() {

    Route::post('/account/create', array(
        'as' => 'Post-Create-Account',
        'uses' => 'AccountController@postCreate'
    ));


    Route::post('/account/login', array( 
        'as' => 'postlogin', 
        'uses' => 'AccountController@postLogin'
    ));


});

    Route::get('/account/login', array(
        'as' => 'login',
        'uses' => 'AccountController@getLogin'
));

Route::get('/account/create', array(
    'as' => 'Create-Account',
    'uses' => 'AccountController@getCreate'
));

Route::get('/account/activate/{code}', array(
    'as' => 'Activate-Account',
    'uses' => 'AccountController@getActivate'

Upvotes: 1

Views: 21454

Answers (4)

Pervez
Pervez

Reputation: 576

I think Apache version problem. You need to update Apache2.4.

Upvotes: 0

George
George

Reputation: 21

Try to add primaryKey field in your user model. It should be something like that:

protected $primaryKey = 'user_id';

Upvotes: 2

Laurence
Laurence

Reputation: 60040

When creating the user you've done

$password = Input::get('email');

It should be

$password = Input::get('password');

so if you try and login with the "email" as the password - it will work! :)

So if you change this

else {
        $email = Input::get('email');
        $username = Input::get('username');
        $password = Input::get('email');

        $code = str_random(60);

        $user = User::create(array(
            'email' => $email,
            'username' => $username,
            'password' => Hash::make($password),
            'code' => $code,
            'active' => 0));
});

to this

else {
        $user = User::create(array(
            'email' => Input::get('email'),
            'username' => Input::get('username'),
            'password' => Hash::make(Input::get('password');),
            'code' => str_random(60),
            'active' => 0));
});

that cleans up your code and fixes the issue.

Upvotes: 6

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Your code looks right to me, so you have to check some things:

1) A manual attempt works for you?

dd( Auth::attempt(['email' => 'youremail', 'password' => 'passw0rt']) );

2) The user hash checks manually?

$user = User::find(1);

var_dump( Hash::check($user->password, 'passw0rt') );

dd( Hash::check($user->password, Input::get('password')) );

Upvotes: 5

Related Questions