52 lines
1.9 KiB
PHP
52 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
class SettingsController extends Controller
|
|
{
|
|
/**
|
|
* Display the consolidated account settings page.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$user = $request->user();
|
|
$twoFactorEnabled = !is_null($user->two_factor_confirmed_at);
|
|
|
|
$qrCode = null;
|
|
$secret = null;
|
|
|
|
if (!$twoFactorEnabled) {
|
|
if (!$user->two_factor_secret) {
|
|
$g2fa = new \PragmaRX\Google2FA\Google2FA();
|
|
$user->update(['two_factor_secret' => $g2fa->generateSecretKey()]);
|
|
}
|
|
$secret = $user->fresh()->two_factor_secret;
|
|
$g2fa = new \PragmaRX\Google2FA\Google2FA();
|
|
$otpUrl = $g2fa->getQRCodeUrl(config('app.name'), $user->email, $secret);
|
|
$renderer = new \BaconQrCode\Renderer\ImageRenderer(
|
|
new \BaconQrCode\Renderer\RendererStyle\RendererStyle(200),
|
|
new \BaconQrCode\Renderer\Image\SvgImageBackEnd()
|
|
);
|
|
$qrCode = 'data:image/svg+xml;base64,' . base64_encode((new \BaconQrCode\Writer($renderer))->writeString($otpUrl));
|
|
}
|
|
|
|
return Inertia::render('Settings/Index', [
|
|
'mustVerifyEmail' => $user instanceof \Illuminate\Contracts\Auth\MustVerifyEmail,
|
|
'status' => session('status'),
|
|
'twoFactor' => [
|
|
'enabled' => $twoFactorEnabled,
|
|
'qr_code' => $qrCode,
|
|
'secret' => $secret,
|
|
'email_enabled' => (bool)$user->email_2fa_enabled,
|
|
'recovery_codes' => $user->two_factor_recovery_codes
|
|
? json_decode($user->two_factor_recovery_codes, true)
|
|
: [],
|
|
],
|
|
]);
|
|
}
|
|
}
|