以 laravel5.7 登入為例。
步驟
首先
composer require 233sec/laravel-recaptchav3
修改 App\Http\Controllers\Auth\LoginController
, 新增如下方法
/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function validateLogin(Request $request)
{
$request->validate([
$this->username() => 'required|string',
'password' => 'required|string',
'g-recaptcha-response' => 'required|recaptchav3:login,0.5', // 重點在這一行
]);
}
修改 resources/views/auth/login.blade.php
在 <button type="submit"...>
之前加入如下程式碼
<div class="form-group
@if ($errors->has('g-recaptcha-response'))
has-error
has-feedback
@endif
">
{!! RecaptchaV3::field('login') !!}
@if ($errors->has('g-recaptcha-response'))
<span class="glyphicon glyphicon-warning-sign form-control-feedback" aria-hidden="true"></span>
<span class="help-block">{{ $errors->first('g-recaptcha-response') }}</span>
@endif
</div>
並在你的模板 javascript
處
@push('script')
{!! RecaptchaV3::initJs() !!}
@endpush
在 .env
加入如下
RECAPTCHAV3_SITEKEY=#你的sitekey#
RECAPTCHAV3_SECRET=#你的secret#
RECAPTCHAV3_ORIGIN=https://www.recaptcha.net #這一行特別重要,否則在大陸無法使用
驗證訊息自定義
修改 resources/lang/#你的語言設定#/validation.php
<?php
return [
...,
'custom' => [
'g-recaptcha-response' => [
'recaptchav3' => '驗證碼錯誤'
]
],
'attributes' => [
'g-recaptcha-response' => '驗證碼',
]
];
效果
來源
- 233sec/laravel-recaptchav3: https://github.com/233sec/laravel-recaptch... (基於 josiasmontag/laravel-recaptchav3)
- josiasmontag/laravel-recaptchav3 https://github.com/josiasmontag/laravel-re...
本作品採用《CC 協議》,轉載必須註明作者和本文連結