0

I've installed Laravel 4.2 but ran into a problem where the 'Auth::attempt()' function is not willing to let the user in but I can log in with the 'Auth::loginUsingId()' function.

My User model

<?php

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

class User extends Eloquent implements UserInterface, RemindableInterface {

        public $timestamps = false; 
        protected $table = 'user';
        protected $primaryKey = 'user_id';

        protected $hidden = array('user_pass', 'remember_token');

        public function getAuthIdentifier(){
            return $this->getKey();
        }

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

        public function getRememberToken(){
            return $this->user_remembertoken;
        }

        public function setRememberToken($value){
            $this->user_remembertoken = $value;
        }


        public function getRememberTokenName(){
            return "user_remembertoken";
        }

        public function getReminderEmail(){
            return $this->user_email;
        }
}

My routes (I've put in some exaple)

Route::get('login', function(){
        $user = 'dd';
        $pass = '123456';
        $credentials =["user_nickname" => "dd","user_pass" => "123456"];
        if(Auth::attempt($credentials)){
            echo 'Login Successful';
        }
        else{
            echo 'Login Failed';
        }
});

Route::get('check', function()
{
if(Auth::check()){echo 'identified';}
else{echo 'not identified';}
return;
});

Route::get('reg', function()
{
    $user = new User;
    $user->user_nickname = 'dd';
    $user->user_pass = Hash::make('123456');
    $user->user_realname = 'dd';
    $user->user_email = 'test@gmail.com';
    $user->user_accesslevel = 1;
    $user->user_language = 'HU';

    $user->save();
});



Route::get('forcelogin', function()
{
Auth::loginUsingId(6,TRUE);
return;
});

My Database

user_id | smaillint
user_nickname | varchar(25)
user_pass | char(60)
user_email | varchar(255)
user_realname | varchar(60)
user_accesslevel | tinyint(3)
user_language | char(2)
user_regdate | timestamp
user_remembertoken | char(60)
davidsarkany
  • 7
  • 1
  • 5
  • TIP: please do a better database design, when you have a table "user" and a column "id" it obviously means user's id, you dont have to specify as user_id. same applies to every column you have mentioned. $user->id makes more sense than $user->user_id. – Ajay Kumar Ganesh Aug 29 '14 at 13:39

2 Answers2

1

The user password column MUST BE 'password'. This is hard coded into Laravel and is not configurable (even with the getAuthPassword() function).

So change your database migration from

user_pass | char(60)

to

password | varchar(60)

and change

$user->user_pass = Hash::make('123456');

to

$user->password = Hash::make('123456');

when registering the user and it will work.

Laurence
  • 58,936
  • 21
  • 171
  • 212
  • Thank you, working. The char(60) is better for now that the varchar(60) because the brypt always returns 60 characters. http://stackoverflow.com/questions/1885630/whats-the-difference-between-varchar-and-char – davidsarkany Aug 29 '14 at 14:56
0

Change user password column field from char(60) to "varchar 255" delete user password hash recreate it and give it a try!

Oguzhan
  • 724
  • 6
  • 12