Laravel 4 - Auth :: attempt()无效并返回FALSE

时间:2014-08-29 11:04:17

标签: authentication laravel laravel-4

我已经安装了Laravel 4.2,但遇到了'Auth :: attempt()'函数不愿意让用户进入但我可以使用'Auth :: loginUsingId()'函数登录的问题。

我的用户模型

<?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;
        }
}

我的路线(我已经提出了一些建议)

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;
});

我的数据库

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)

2 个答案:

答案 0 :(得分:1)

用户密码栏必须是密码&#39;。这被硬编码到Laravel中并且不可配置(即使使用getAuthPassword()函数)。

因此,请从

更改数据库迁移
user_pass | char(60)

password | varchar(60)

并更改

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

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

注册用户时,它将起作用。

答案 1 :(得分:0)

将用户密码列字段从char(60)更改为&#34; varchar 255&#34;删除用户密码哈希重新创建并尝试一下!