LoginSignup
1
0

More than 3 years have passed since last update.

LaravelでGoogle2FAを使用して詰まった点メモ

Last updated at Posted at 2019-05-03

LaravelでGoogle2FAを使用しようとして、QRコードが生成できなくって詰まったのでメモ

・google2faとQRコードのライブラリ導入

composer require pragmarx/google2fa
composer require bacon/bacon-qr-code

このときcomposer.json

 "pragmarx/google2fa": "^5.0"

となっています。

Lravelの二段認証で調べると下記のような、getQRCodeGoogleUrlというメソッドを使っていますが、google2fa 5.0で消えていて使えません

 $qrUrl = $g2fa->getQRCodeGoogleUrl(
            config('app.name'),
            $user->email,
            $user->g2fa_key
        );

4.0.2では使用できるので、バージョンを変えましょう

 "pragmarx/google2fa": "^4.0"

5.0のREADMEでのQRコードの生成は下記のようにするようにあるのですが、パクっても
You need to install the imagick extension to use this back end
とでて、サーバーにインストールが必要らしく諦めました

//Bacon/QRCode
<?php

use PragmaRX\Google2FA\Google2FA;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;

$google2fa = app(Google2FA::class);

$g2faUrl = $google2fa->getQRCodeUrl(
    'pragmarx',
    'google2fa@pragmarx.com',
    $google2fa->generateSecretKey()
);

$writer = new Writer(
    new ImageRenderer(
        new RendererStyle(400),
        new ImagickImageBackEnd()
    )
);

$qrcode_image = base64_encode($writer->writeString($g2faUrl));
//And show it as an image:

<img src="data:image/png;base64, <?php echo $qrcode_image; ?> "/>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0