dongpalou5352 2018-03-22 15:55
浏览 128
已采纳

Laravel 5.6 - 在自定义表上使用内置Auth - 不验证用户/密码

I am struggling with using Laravel's built-in auth against an existing 'Users' table that already contains username and password. The column names are 'UserEmail' and 'UserPassword'. To test it out, I used tinker to create an entry, hashing the password as:

$user->UserPassword=Hash::make('1234');

When trying to log in using Auth::attempt, authentication keeps failing. To debug, I started adding var_dump and even do a comparison of the password request that comes through against the hashed password in the database and it matches. So I am not sure why the Auth::attempt fails to authenticate it:

This is the function I have in my LoginController.php:

public function login(Request $request)
{
    // validate the form data
    $this->validate($request, [
        'UserEmail' => 'required',
        'UserPassword' => 'required'
    ]);

    var_dump($request->UserEmail);
    var_dump($request->UserPassword);
    $user = App\FarmUser::where('UserEmail', $request->UserEmail)->first();
    var_dump($user->UserPassword);
    $authenticated = Hash::check($request->UserPassword, $user->UserPassword);
    var_dump($authenticated);

    // attempt to log the user in
    if (Auth::attempt(['UserEmail' => $request->UserEmail, 'UserPassword' => $request->UserPassword], $request->remember)) {
        // if successful, then redirect to their intended location
        Debugbar::addMessage('successfully authenticated');
        return redirect()->intended($redirectTo);   
    }

    // if unsuccessful, then redirect back to login with the form data
    Debugbar::addMessage('authentication failed');
    //return redirect()->back();
}

When I try to log in, I see the correct UserEmail, UserPassword, and the hash check also returns true. However, when I call the Auth::attempt, it does not successfully authenticate and redirects back. Any idea why I can't get the Auth::attempt to authenticate properly?

When I log in, this is the output generated from those var dumps:

string(20) "user@test.com" string(8) "1234" string(60) "<hash>" bool(true)

Here is what tinker returns for this App\FarmUser model:

>>> $user = App\FarmUser::where('UserLogin', 'testuser')->first();
=> App\FarmUser {#809
     ID: 1,
     UserLogin: "testuser",
     password: "<hash>",
     Active: "Yes",
     UserEmail: "user@test.com",
     Created: "2018-03-20 15:09:24",
     CreatedByID: null,
     Modified: null,
     ModifiedByID: null,
     created_at: "2018-03-20 19:09:09",
     updated_at: "2018-03-20 19:09:09",
   }
  • 写回答

1条回答 默认 最新

  • dt888999 2018-03-22 16:07
    关注

    You will need to override the getAuthPassword() method in your User model, like this:

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

    And don't call UserPassword in your Auth::attempt(), but call it password. Like this:

    Auth::attempt([
        'UserEmail' => $request->UserEmail, 
        'password' => $request->UserPassword
    ]);
    

    This is because Laravel can't recognize which fields are supposedly a password field and has a "reserved" word "password" for it (it will perform a hash on that field, but not others).

    Edit:

    Since your model is actually \App\FarmUser, you also need to change it in the config/auth.php file, like so:

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\FarmUser::class,
        ],
    
        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱