【问题标题】:Laravel 4 user authenticationLaravel 4 用户认证
【发布时间】:2013-12-28 10:56:31
【问题描述】:

我正在做一个简单的登录,我遇到了一个小问题,只是不知道是什么问题。

这是我的 auth.php:

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Default Authentication Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the authentication driver that will be utilized.
    | This driver manages the retrieval and authentication of the users
    | attempting to get access to protected areas of your application.
    |
    | Supported: "database", "eloquent"
    |
    */

    'driver' => 'eloquent',

    /*
    |--------------------------------------------------------------------------
    | Authentication Model
    |--------------------------------------------------------------------------
    |
    | When using the "Eloquent" authentication driver, we need to know which
    | Eloquent model should be used to retrieve your users. Of course, it
    | is often just the "User" model but you may use whatever you like.
    |
    */

    'model' => 'Login',

    /*
    |--------------------------------------------------------------------------
    | Authentication Table
    |--------------------------------------------------------------------------
    |
    | When using the "Database" authentication driver, we need to know which
    | table should be used to retrieve your users. We have chosen a basic
    | default value but you may easily change it to any table you like.
    |
    */

    'table' => 'tbl_user',

    /*
    |--------------------------------------------------------------------------
    | Password Reminder Settings
    |--------------------------------------------------------------------------
    |
    | Here you may set the settings for password reminders, including a view
    | that should be used as your password reminder e-mail. You will also
    | be able to set the name of the table that holds the reset tokens.
    |
    | The "expire" time is the number of minutes that the reminder should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'reminder' => array(

        'email' => 'emails.auth.reminder',

        'table' => 'password_reminders',

        'expire' => 60,

    ),

);

如您在上面看到的,我使用的是我自己的模型Login 和我自己的表tbl_user

下面是我的模型(Login.php)

<?php
class Login extends Eloquent {
    protected $table = "tbl_user";

    public static function checkUser($array)
    {
        $data = DB::table('tbl_user')->where('user_email', $array['user_email'])->where('user_password', $array['user_password'])->get();

        return $data;
    }
}

现在我认为模型有问题,我只是不知道它是什么。我正在从 CodeIgniter 迁移到 Laravel 4,这个 auth thingy 对我来说是新的。

路线如下:

Route::post('login', function(){    
    $userdata = array(
        'user_email' => Input::get('email'),
        'user_password' => Hash::make(input::get('password'))
    );

    if(Auth::attempt($userdata)){
        echo "Login Success!";
    }else{
        echo "Login Failed!";
    }
});

我登录失败!密码是散列的!嗯!有什么建议吗?

【问题讨论】:

    标签: authentication laravel-4


    【解决方案1】:

    Auth 使用实现用户界面的用户模型,如果您想创建自己的自定义用户模型,您应该实现用户界面,您可以在您的应用中查看默认提供的用户模型:

    <?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 e-mail address where password reminders are sent.
         *
         * @return string
         */
        public function getReminderEmail()
        {
            return $this->email;
        }
    
    }
    

    【讨论】:

    • 使用 Illuminate\Auth\UserInterface;使用 Illuminate\Auth\Reminders\RemindableInterface;我找不到那些路径...
    • 知道了:/laravel/vendor/laravel/framework/src/Illuminate/Auth/UserInterface.php
    【解决方案2】:

    正如Petkostas 提到的,你必须实现UserInterfaceRemindableInterface

    此外,还要修复此行:

    $userdata = array(
            'user_email' => Input::get('email'),
            'user_password' => Hash::make(input::get('password')) // no need to hash password
        );
    

    Auth::attempt() 之前不需要散列密码,因为 Laravel 会为你做。

    我已经从代码中删除了Hash::make()

    $userdata = array(
            'user_email' => Input::get('email'),
            'user_password' => input::get('password')
        );
    
        if(Auth::attempt($userdata)){
            echo "Login Success!";
        }else{
            echo "Login Failed!";
        }
    

    【讨论】:

    • 这对我来说有点新鲜,但我有自己的列名,如 user_id、user_username、user_email 等。如果我使用 User 模型,我可以更改这些查询以适合我的代码吗?或者如果我像上面一样创建自己的模型,我该如何实现它?谢谢。
    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2012-11-06
    • 2023-03-19
    • 2019-12-22
    • 1970-01-01
    • 2013-09-11
    相关资源
    最近更新 更多