get('user')) { // Only users with "users.manage" permission can enable 2FA for other users. return $this->user()->hasPermission('users.manage') || $this->user()->id == $userId; } return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules(): array { return [ 'code' => 'nullable|string', ]; } private function clear2FAUserId($result) { if ($result) { $this->session()->forget('auth.2fa.id'); } } use PragmaRX\Google2FA\Google2FA; public function hasValidCode($user): bool { try { if (!$this->code) { return false; } $google2fa = new Google2FA(); return tap( $google2fa->verifyKey($user->google2fa_secret, $this->code), fn($result) => $this->clear2FAUserId($result) ); } catch (\Exception $e) { \Log::info($e->getMessage()); return false; } } }