Laravel认证系统可以使用现有的数据库吗?


Can the Laravel auth system use an existing database?

我正在开发一个PHP管理面板与Laravel框架。一些前端已经做好了。至此,数据库和用户表已经创建并拥有了内容。是否有办法使用我现有的数据库和表与Laravel Auth类?

我的数据库有它自己的加密密码的方式-可以Laravel适应吗?

如果需要,可以直接验证:

$user = User::where('email', Input::get('email'))->first();
if( $user && $user->password == md5(Input::get('password')) )
{
    Auth::login($user); /// will log the user in for you
    return Redirect::intended('dashboard');
}
else
{
   /// User not found or wrong password
}

请注意,由Laravel散列的密码是非常安全的,而由MySQL散列的密码则相反。因此,您可以在每次用户登录时转换密码,而无需要求他这样做:

$password = Input::get('password');
$email = Input::get('email');
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
    return Redirect::intended('dashboard');
}
else
if( $user && $user->password == md5($password) )
{
    Auth::user()->password = Hash::make($password);
    Auth::user()->save();
    return Redirect::intended('dashboard');
}
else
{
    /// User not found or wrong password
}

根据Antonio Carlos Ribeiro的建议(谢谢!),以下是我对Laravel 5.2的管理方法:

  • Http/Controllers/Auth/AuthController.php中,复制粘贴vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticateUsers.php
  • 中的login()方法
  • 将这些添加到文件的顶部:

    • use Illuminate'Support'Facades'Auth as fAuth;
    • use Hash;
  • 替换为

    if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
        return $this->handleUserWasAuthenticated($request, $throttles);
    }
    

    By this:

    if (fAuth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
        return $this->handleUserWasAuthenticated($request, $throttles);
    } else {
        $user = User::where('email', $request->email)->first();
        if ($user && $user->password == md5($request->password)) {
            $user->password = Hash::make($request->password);
            $user->save();
            if (fAuth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
                return $this->handleUserWasAuthenticated($request, $throttles);
            }
        }
    }