Search code examples
phplaravelauthenticationmigrating

Better way to use Laravel login into a "legacy system"?


I need to make a migration of a legacy system to Laravel. I'm using PHP 5.6 and we have a login system using session and cookies to personalize some user stuffs. Well, I want to migrate some parts of my system and the first one I believe that will be Login, I think that I need to share login between Laravel and the old PHP, but what is the better way to do it? The users password wasn't into bcrypt encryption and I will need to share the "login" into old system pages, and the new pages migrated to Laravel..

Thanks


Solution

  • Create a new ServiceProvider such as LegacyHashProvider

    namespace App\Providers;
    
    use App\Services\LegacyHasher;
    use Illuminate\Support\ServiceProvider;
    
    class LegacyHashProvider extends ServiceProvider
    {
        public function register()
        {
            $this->app->singleton('hash', function ($app) {
                return new LegacyHasher($app);
            });
        }
    
        public function provides()
        {
            return ['hash'];
        }
    }
    

    Create a class LegacyHasher which implements Illuminate\Contracts\Hashing\Hasher interface. Write down your methods(override) such as;

    public function make($value, array $options = [])
    {
        return hash('sha512', $value); // this will be your legacy hashing system 
    }
    

    Navigate to config/app.php and replace Illuminate\Hashing\HashServiceProvider::class with App\Providers\LegacyHashProvider and it should be ready to use your legacy login system.