[英]Can the Laravel auth system use an existing database?
我正在使用Laravel框架在PHP中開發一個管理面板。 一些前端已經完成。 話雖如此,數據庫和用戶表已經創建並且具有內容。 有什么方法可以將我現有的數據庫和表與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); }
這樣 :
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); } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.