我正在使用Laravel框架在PHP中开发一个管理面板。一些前端已经制作完成。话虽如此,数据库和用户表已经创建并具有内容。有没有办法在Laravel Auth类中使用我现有的数据库和表?
我的数据库有自己的加密密码的方法 - Laravel可以适应吗?
答案 0 :(得分:3)
如果需要,您可以直接进行身份验证:
$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
}
答案 1 :(得分:1)
按照安东尼奥·卡洛斯·里贝罗的建议(谢谢男人!),这里是我为Laravel 5.2管理的方式:
Http/Controllers/Auth/AuthController.php
中,复制并粘贴login()
vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticateUsers.php
方法
在文件顶部添加以下内容:
use Illuminate\Support\Facades\Auth as fAuth;
use Hash;
替换它:
if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
由此:
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);
}
}
}