laravel 4身份验证始终返回false

时间:2014-04-30 17:40:45

标签: authentication laravel return

请帮忙。我的laravel 4身份验证始终返回false。

到目前为止,我已尝试制作身份验证系统。我遵循了几个教程,但似乎都没有。

以下是我的一些重要文件:

我的auth.php

<?php

 return array(
    'driver'   => 'eloquent', 
    'model'    => 'User',  
    'table'    => 'users',
    'reminder' => array(
        'email'  => 'emails.auth.reminder',
        'table'  => 'password_reminders',
        'expire' => 60,
     ),
);

在我的控制器中,我有这个代码来处理注册工作正常。

public function store()
{
    // validate 
    $password=Input::get('password');
    $data = array(
    'email'           => Input::get('email'),
    'firstname'       => Input::get('firstname'),
    'lastname'        => Input::get('lastname'),
    'password'        => Hash::make($password)
    );              
    $rules = array(
        'firstname'       => 'required',
        'lastname'        => 'required',
        'email'           => 'unique:users',
        'password'        => 'required'
    );
    $validator = Validator::make($data, $rules);        

    // process the registration
    if ($validator->fails()) 
    {
        Session::flash('message', 'This email is already registered, please choose    another one.');
        return Redirect::to('login')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } 
    else 
    {
        // store
        $user = new User;
        $user->firstname       = Input::get('firstname');
        $user->lastname        = Input::get('lastname');
        $user->email           = Input::get('email');
        $user->password        = Input::get('password');
        $user->save();

        // redirect
        Session::flash('message', 'Successfully created account! Please login to submit your ad.');
        return Redirect::to('login');
    }
}

以下是检查登录过程的代码

// process the login
public function doLogin()
{

    $email    = Input::get('email');
    $pass     = Input::get('password');
    $password = Hash::make($pass);

    $credentials = array(
       'email'    => $email,
       'password' => $password
    );

    if(Auth::attempt($credentials)) 
    {
         return 'success';
    }
    else 
    {
         return 'auth failed';
    }       

}

以下是我的用户模型上的代码

<?php

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

class User extends Eloquent implements UserInterface, RemindableInterface {

/**
 * 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('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 token value for the "remember me" session.
 *
 * @return string
 */
public function getRememberToken()
{
    return $this->remember_token;
}

/**
 * Set the token value for the "remember me" session.
 *
 * @param  string  $value
 * @return void
 */
public function setRememberToken($value)
{
    $this->remember_token = $value;
}

/**
 * Get the column name for the "remember me" token.
 *
 * @return string
 */
public function getRememberTokenName()
{
    return 'remember_token';
}

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

}

3 个答案:

答案 0 :(得分:3)

登录时,您不必哈希密码。 Auth::attempt()方法处理输入密码,并根据数据库中的哈希密码进行检查。只需从$password = Hash::make($pass);方法中删除doLogin(),然后将您的凭据数组设置为

即可
$credentials = array(
  'email' => $email,
  'password' => $pass
);

现在你的身份验证工作正常。

答案 1 :(得分:1)

对于所有使用有效信用卡面临Auth :: attempt()登录失败的laravel 4开发人员!

解决方案:

  • 在app / config / app.php中更改类型:'cipher'=&gt; MCRYPT_RIJNDAEL_128到'cipher'=&gt; MCRYPT_RIJNDAEL_256并重新哈希所有密码
  • 在模型添加方法中:

     public function setPasswordAttribute($pass) {
    
         $this->attributes['password'] = Hash::make($pass);
     }
    
  • 在UserController中:

    //创建用户或注册

       public function postCreate() {
    
        $input = Input::all();
        $user = new User;
        $user->username = Input::get('username');
        $user->email = Input::get('email');
        $user->password = Hash::make(Input::get('password'));    
        $user = User::create($input);
        return Redirect::action("Frontend\HomeController@index");
    

    }

    //登录或登录

    public function postSignin() {
    
        $userdata = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );
    
        if (Auth::attempt($userdata, true)) {
             Session::put('username', Auth::user()->username);
             return Redirect::action("Frontend\HomeController@index");
        } else {
             return Redirect::action("Frontend\UserController@getLogin")
                    ->with('message', 'Your username/password combination was incorrect')
                    ->withInput();
    }
    

希望我帮助了那里的人

答案 2 :(得分:0)

尝试将app / auth.php中的默认驱动程序从eloquent更改为数据库。它对我有用。