mewebstudio / captcha

Captcha for Laravel 5/6/7/8/9/10/11
MIT License
2.46k stars 452 forks source link

Validate capatcha in api middleware #115

Open gaurav-garry opened 7 years ago

gaurav-garry commented 7 years ago

I am new to laravel and writing a REST API to register the users. But I want to include the captcha in the form. I can make a query to load the image and use the same in image tag on frontend. When I post the request back, I want to validate the captcha. How can I do it?

/**
 * Class AuthController
 * @package App\Http\Controllers\Api\Auth
 */
class RegisterController extends Controller
{
    /**
     * @var Services\User
     */
    private $user;

    /**
     * @var Services\OauthClient
     */
    private $oauthClient;

    public function __construct(Services\User $user, Services\OauthClient $oauthClient)
    {
        parent::__construct();
        $this->user = $user;
        $this->oauthClient = $oauthClient;
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|unique:users|regex:/^(?=.{4,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            'captcha' => 'required|captcha',
        ]);
    }

    /**
     * @param Request $request
     * @return array
     */
    function register(Request $request)
    {
        $msg = 'You have successfully registered';
        $validator = $this->validator($request->all());
        if ($validator->fails()) {
            return [
                'msg' => 'Validation failed',
                'errors' => $validator->messages(),
            ];
        }
        // save user
        $user = $this->user->create($request->all());

        return [
            'msg' => $msg,
        ];
    }
}
jerrycheese commented 6 years ago

try this:

Route::get('captcha', function () {
    $res = app('captcha')->create('default', true);
    return $res;
});
Route::post('check_captcha', function (Request $req) {
    $captcha = $req->input('captcha');
    $key = $req->input('key');
    $res = captcha_api_check($captcha, $key);
    dump($res);
});

If you're trying to use validator, try this:

Route::post('check_captcha', function (Request $req) {
        $data = $req->all();

        $validator = Validator::make($data, [
            'ckey' => 'required',
            'captcha' => 'required|captcha_api:' . $req->input('ckey')
        ]);

        if ($validator->fails()) {
            return [
                'msg' => 'Validation failed',
                'errors' => $validator->messages(),
            ];
        } else {
            return [
                'msg' => 'Validation passes'
            ];
        }
    });
jackluo2012 commented 5 years ago

如何让这玩意儿过期

zouyan198429 commented 4 years ago

如何让这玩意儿过期

https://github.com/mewebstudio/captcha/issues/176 api调用方式 缓存,并让其过期