Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Google 2fa QR Code not displaying

I am currently using a package for Google 2fa and pulling in a SVG QR code in the form of a string variable from my controller and then required according to the package to add the QR code as . The problem is that this does not display the image and I think it is due to the string value that I am pulling from my controller.

This is the value of the variable I receive from my Controller:

enter image description here

When echoing this in my blade file, it simply echoes the string value. If I were to copy this string value without the "", Laravel recognised the value as html and displays my QR code. Is there a way to echo it for blade to recognise it as html code? Or how can I go about showing this SVG file in my blade file when pulling it in as a variable in this way? Please if anyone would assist me, it would be greatly appreciated!

Link to the Laravel Package -> https://github.com/antonioribeiro/google2fa-laravel

My Controller:

public function register(Request $request)
    {
        //Validate the incoming request using the already included validator method
        $this->validator($request->all())->validate();

        // Initialise the 2FA class
        $google2fa = app('pragmarx.google2fa');

        // Save the registration data in an array
        $registration_data = $request->all();

        // Add the secret key to the registration data
        $registration_data["google2fa_secret"] = $google2fa->generateSecretKey();

        // Save the registration data to the user session for just the next request
        $request->session()->flash('registration_data', $registration_data);

        // Generate the QR image. This is the image the user will scan with their app
        // to set up two factor authentication
        $QR_Image = $google2fa->getQRCodeInline(
            config('app.name'),
            $registration_data['email'],
            $registration_data['google2fa_secret']
        );

        // Pass the QR barcode image to our view
        return view('google2fa.register', ['QR_Image' => $QR_Image, 'secret' => $registration_data['google2fa_secret']]);
    }

My View:

<div>
     <img src="{{ $QR_Image }}">
</div>
like image 333
Adam Avatar asked Mar 25 '26 03:03

Adam


1 Answers

This should be fairly easy:

<div>
     {!! $QR_Image !!}
</div>

You need the {!! !!}to render out HTML/SVG data without escaping it. And you need to remove the img tags, as it's not a real image, but an SVG.

like image 168
Farkie Avatar answered Mar 26 '26 16:03

Farkie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!