Question

My user table structure is as under, I am not able to authenticate my login form. Please let me know what should be changed in overall laravel config files? I am getting "boolean false" as result when using dd(Auth::attempt($user));

Schema::create('users', function($table){
$table->increments('userId');
$table->string('userName');
$table->string('userPassword');
$table->timestamps();
});

// My UserController function is as under
    public function postLogin() {
        $user = array(
            'userName' => Input::get('username'),
            'userPassword' => Input::get('password')
        );

        //dd(Auth::attempt($user));

        if( Auth::attempt($user) ) {
            return Redirect::to('/')->with('message', 'You are logged in');
        } else {
            return Redirect::route('login')
                ->with('message', 'Your username/password combination was incorrect')
                ->withInput();
        }
    }

// Here is my model

<?php

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

class User extends BaseModel implements UserInterface, RemindableInterface {

    public static $rules = array(
        'username' => 'required|unique:users|alpha_dash|min:4',
        'password' => 'required|alpha_num|between:4,8|confirmed',
        'password_confirmation' => 'required|alpha_num|between:4,8'
    );

    /**
     * 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 = array('userPassword');

    /**
     * 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;
    }

}

Second, I am getting "created_at, updated_at" by migrations when using "$table->timestamps()", please suggest me how can I name "created_at, updated_at" like camel case "createdAt, updatedAt" as my other fields.

Note: Register is working fine for me.

Était-ce utile?

La solution

Edit: woops just realized that you must've already solved this

$table->increments('userId');

You're using userId for your primary key instead of ID, but your Eloquent model is not aware of this. You need to add the following line to your User model:

protected $primaryKey = 'userId';  //this variable is defaulted to 'id'

The following should also be changed:

public function getAuthPassword() {
    return $this->password;  
}

You're not using "password" as your password column. Change it to:

public function getAuthPassword() {
    return $this->userPassword;  
}

The autogenerated code in the User model assumes that you use the default field names defined by Laravel, so you need to make sure that the User model and auth config match the changes you made to the table.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top