【问题标题】:Laravel 5 Auth change table columnsLaravel 5 Auth 更改表列
【发布时间】:2015-11-27 04:19:09
【问题描述】:

我更改了用户数据库表中的一些字段。

table name: users
primaryKey: user_id
username: user_username
password: user_password
e-mail: user_mail

在 Illuminate\Foundation\AuthAuthenticatesUsers 我添加了受保护的$username = 'user_username'

当我尝试登录我的帐户时,我在输入用户名和密码后看到一个空白页面。调试已打开但无法正常工作。发生了什么?

Auth::attempt(array(
            'user_username' => 'pionas',
            'user_password'  => '12345',
        ));

User 模型中,我添加了getAuthPassword 并将列名更改为user_password。日志清晰。

Auth::loginUsingId(1); - 不工作

可能 Auth 中的所有方法都不起作用。

我的用户模型是:

<?php
namespace App\User;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
protected $primaryKey = 'user_id';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
protected $fillable = ['user_username', 'user_mail', 'user_password', 'user_group_id', 'user_code', 'user_rang'];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
protected $hidden = array('user_password', 'remember_token');

/**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->user_password;
    }

    public function comment()
    {
        return $this->hasOne('UserField', 'user_id');
    }
    /**

【问题讨论】:

    标签: php mysql laravel-5 laravel-5.1


    【解决方案1】:

    您似乎已经剥夺了 Laravel 5.1 使用的一些必需特征。这是一个更新后的用户模型,其中恢复了这些特征:

    <?php
    
    namespace App;
    
    use Illuminate\Auth\Authenticatable;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Auth\Passwords\CanResetPassword;
    use Illuminate\Foundation\Auth\Access\Authorizable;
    use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
    use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
    use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
    
    class User extends Model implements AuthenticatableContract,
                                        AuthorizableContract,
                                        CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;
    
        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'users';
    
    
        protected $primaryKey = 'user_id';
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = ['user_username', 'user_mail', 'user_password', 'user_group_id', 'user_code', 'user_rang'];
    
        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = array('user_password', 'remember_token');
    
        /**
         * Get the unique identifier for the user.
         *
         * @return mixed
         */
        public function getAuthIdentifier()
        {
            return $this->getKey();
        }
    
        /**
         * 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->user_mail;
        }
    
        /**
         * Get the password for the user.
         *
         * @return string
         */
        public function getAuthPassword()
        {
            return $this->user_password;
        }
    
        public function comment()
        {
            return $this->hasOne('UserField', 'user_id');
        }
    
        /**
         * Scope a query to only include users of a given type.
         *
         * @return \Illuminate\Database\Eloquent\Builder
         */
        public function scopeOfStatus($query, $type)
        {
            return $query->where('user_status', $type);
        }
    
    }
    

    【讨论】:

    • 我必须编辑 EloquentUserProvider if (!Str::contains($key, 'password')) { 到 if (!Str::contains($key, 'user_password')) { ** 和 **$plain = $credentials['password'];到 $plain = $credentials['user_password']; 现在可以工作了 :) 非常感谢 :)
    • 永远不要接触那些供应商文件。请参阅我的答案以更好地解决此问题。
    【解决方案2】:

    你需要做两件事。

    1. 在你的模型上,添加方法getAuthPassword()(你已经这样做了)

      public function getAuthPassword()
      {
          return $this->user_password;
      }
      
    2. 在您的 AuthController 上,当将 $credentials 传递给 $this->auth->validate() 方法(或 Auth::attemp() 或其他任何方法)时,您必须使用密钥设置数组以检查密码,并且这个密钥应该被命名为“密码”。所有其他键可以有任何名称,您将使用您的用户名列的名称

      $credentials['user_username'] = $request->input('user_username');
      $credentials['password'] = $request->input('user_password');
      
      Auth::attempt($credentials);
      

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 2016-10-09
      • 1970-01-01
      • 2019-03-27
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多