我正在尝试在我的Laravel Passport API中使用google reCaptcha进行登录。我有一个代码结构,但是不能正常工作。我在AuthenticatesUsers
函数中调用validateRc
特征时遇到问题(validateRc
函数是另一个特征名称ReCaptcha
的一部分)。
ReCaptcha.php
<?php
namespace App\Validators;
use GuzzleHttp\Client;
use Illuminate\Foundation\Auth\AuthenticatesUsers as AuthenticatesUsers;
trait ReCaptcha
{
public function validateRc(Request $request){
$recaptcha = $request->input('g-recaptcha-response');
if($recaptcha){
$client = new Client();
$this->validate($request, [
$this->
$recaptcha => 'required|string'
]);
$response = $client->post(
'https://www.google.com/recaptcha/api/siteverify',
['form_params'=>
[
'secret'=>env('GOOGLE_RECAPTCHA_SECRET'),
'response'=>$recaptcha
]
]
);
$body = json_decode((string)$response->getBody());
if ($body->success)
{
Session::flash('Success','Human verified');
return redirect('/home');
$this->AuthenticatesUsers();
} else {
dd($body);
Session::flash('Error','Robots not allowed');
return redirect('/login');
}
} else {
Session::flash('Error','Verification Incomplete');
}
}
}
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Validators\ReCaptcha as ReCaptcha;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use ReCaptcha;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}