用户传递Auth :: attempt();失败Auth :: check('用户')

时间:2016-12-14 19:34:18

标签: php laravel authentication login laravel-5

我正在处理客户数据库的旧副本,并使新的Laravel应用程序与现有用户一起使用。

我正在使用“users”表格使用我的用户模型进行构建和测试,但我正在尝试将其连接到“auth_user”表。更改后,我的新用户正在正确创建。登录是一个问题。用户正在传递Auth::attempt($credentials) as expected,但在

时失败

在我的LoginController中......

// post to /login

public function login() {

    $input = Request::all();

    // Log the user in
    if (Auth::attempt(['email'=>$input['username'], 'password'=>$input['password']])) {//Auth::attempt(Auth::attempt("admin", ['email' => $input['username'], 'password' => $input['password'], 'active' => 1])) { 
        // the user is now authenticated.
        return Redirect::to('/welcome')->with('message', 'Successfully authenticated');
    }
         return Redirect::to('/')
            ->with('message', 'Login Failed: Your Login Credentials Are Invalid');
    }
}

我肯定会传递Auth::attempt(...),但我不认为我的会话是为该用户设置的。重定向到/ welcome后,我失败了Auth::check('user')

public function welcome() {
    if (!Auth::check('user')) return Redirect::to('/'); 

    // ... Top secret stuff (no returns or redirects though)

    return view('user.welcome', [                                   
                                'user' => Auth::user()
                                // ...
                                ]);
}

这会重定向回我的登录控制器。

当我使用'users'表而不是'auth_user'时,这一切都在起作用。

Users使用id作为主键,Auth_user使用'uid'作为主键。我想将uid更改为id,但我必须重复使用一些我无法更改的可怕数量的MYSQL存储过程。

相关型号:

user.php的:

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{

    use Authenticatable, Authorizable, CanResetPassword;

    public function rules($scenario = null, $id = null) {
        $rules = [];

        switch($scenario) {
            case 'userAdd':
                $rules = [
                    'password' => 'required|confirmed|min:6'
                ];
                break;
            case 'passwordChange':
                $rules = [
                    'password' => 'required|confirmed|min:6'
                ];
                break;
        }

        return $rules;
    }

    public function isValid($scenario = null) {
        $validation = User::make($this->attributes, User::rules($scenario));

        if($validation->passes()) return true;

        $this->errors = $validation->messages();
        return false;
    }


    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'auth_user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['username', 'name', 'password', 'email', 'expire', 'active', 'organization','role'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    protected $primaryKey = 'uid';
}

Auth.php(针对多种用户类型 - 我知道,我宁愿使用角色而不是单独的模型)

<?php

return [

    'multi' => [
        'user' => [
            'driver' => 'eloquent',
            'model'  => App\User::class,
            'table'  => 'auth_user'
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model'  => App\Admin::class,
            'table'  => 'auth_admin'
        ]
     ],

];   

我认为我覆盖了主要密钥更改的所有基础,但我无法让模型通过Auth::check()。拥有更多Laravel经验的人可以照亮我做错了什么吗?提前谢谢!

2 个答案:

答案 0 :(得分:0)

这不是一个完整的解决方案。我仍然无法找到任何要告诉Laravel / Guard不要查看ID列的内容,因此我通过添加ID列和{{1}}来对其进行绑定。我并不以此为荣,但它确实有效。

答案 1 :(得分:0)

在您的User.php脚本中,您需要放置此代码,此代码使表格进行登录检查。

protected $table='auth_users';

在User.php代码中,保护表代码处于初始阶段(在类函数之后)。