Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Iljido's avatar

adding 2FA google authentification to an existing laravel 5.5 project

Hi, i want to add 2FA google authentification to my existing laravel project , could you please give me a link or something ? i tried this :

composer require pragmarx/google2fa-laravel

but it giving me error :

Class 'Laravel\Spark\Spark' not found

i read that u can't install laravel/spark on an existing project :-( , please help

(if u have other alternative i'm IN xD, i just want to verifiy user ! )

0 likes
5 replies
bobbybouwmann's avatar

Not sure why you get this error, because Spark is not a dependency of pragmarx/google2fa-laravel.

Did you add laravel/spark to your composer.json yourself?

Iljido's avatar

@bobbybouwmann this time i got everything xD, when i run "composer.." , it create a folder in VENDOR , the name is : EUSBIU , it contains : laravel-spark-google2fa folder, and the problem is in Google2FAServiceProvider.php here :

<?php

namespace Eusebiu\LaravelSparkGoogle2FA;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Laravel\Spark\Spark;
use PragmaRX\Google2FAQRCode\Google2FA;

class Google2FAServiceProvider extends ServiceProvider
{
    /**
     * Boot the service provider.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadMigrationsFrom(__DIR__.'/../migrations');

        $this->loadViewsFrom(__DIR__.'/../resources/views', 'google2fa');

        if ($this->app->runningInConsole()) {
            $this->definePublishing();
        }

        $this->defineRoutes();
    }

    /**
     * Define the publishable migrations and resources.
     *
     * @return void
     */
    protected function definePublishing()
    {
        $this->publishes([
            __DIR__.'/../resources/assets/js/enable-two-factor-auth.js' => resource_path('assets/js/spark-components/settings/security/enable-two-factor-auth.js'),
        ], 'assets');

        $this->publishes([
            __DIR__.'/../resources/views/enable-two-factor-auth.blade.php' => resource_path('views/vendor/spark/settings/security/enable-two-factor-auth.blade.php'),
        ], 'views');
    }

    /**
     * Define the routes.
     *
     * @return void
     */
    protected function defineRoutes()
    {
        if (! $this->app->routesAreCached()) {
            Route::group(['middleware' => 'web'], function () {
                Route::post('/settings/two-factor-auth-generate', TwoFactorAuthController::class.'@generate');
                Route::post('/settings/two-factor-auth-google', TwoFactorAuthController::class.'@enable2fa');
            });
        }
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        Spark::swap('EnableTwoFactorAuth@handle', function ($user) {
            $user->forceFill([
                'google2fa_secret' => session()->pull('spark:twofactor:secret'),
            ])->save();

            return $user;
        });

        Spark::swap('DisableTwoFactorAuth@handle', function ($user) {
            $user->forceFill([
                'google2fa_secret' => null,
            ])->save();

            return $user;
        });

        Spark::swap('VerifyTwoFactorAuthToken@handle', function ($user, $code) {
            return (new Google2FA)->verifyKey($user->google2fa_secret, $code);
        });
    }
}

as u can see at the top it's using laravel spark :// , i'm getting this error message :

In Google2FAServiceProvider.php line 68:

  Class 'Eusebiu\LaravelSparkGoogle2FA\Spark' not found


Script @php artisan package:discover handling the post-autoload-dump event returned with error code 1

Installation failed, reverting ./composer.json to its original content.

bobbybouwmann's avatar

You need to import the Spark class, then it should be all fine ;)

Iljido's avatar

@bobbybouwmann well now it works , but i get this error when i click register :

You need to install the imagick extension to use this back end

i followed a tutorial on how to add imagick in xampp but i still get the same error

bobbybouwmann's avatar

Did you restart your computer after installing the extension?

Also, try different tutorials if it's still not working.

Please or to participate in this conversation.