Reputation: 2249
I am developing an admin panel in PHP with the Laravel framework. Some of the front end is already made. With that said, the database and user table is already created and has content. Is there any way to use my existing database and table with the Laravel Auth class?
My database has it's own way of encrypting passwords - can Laravel adapt to that?
Upvotes: 2
Views: 3965
Reputation: 1977
Following Antonio Carlos Ribeiro's advices (Thanks man !), here's how I managed it for Laravel 5.2 :
Http/Controllers/Auth/AuthController.php
, copy and paste the login()
method from vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticateUsers.php
Add these in the top of the file :
use Illuminate\Support\Facades\Auth as fAuth;
use Hash;
Replace this :
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);
}
}
}
Upvotes: 1
Reputation: 87769
You can do auth directly, if you need:
$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
}
Note that passwords hashed by Laravel are really secure and those hashed by, let's say, MySQL, are the opposite. So you could convert your passwords every time your user logs, without asking him to do so:
$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
}
Upvotes: 3