pctux/yii2-invisible-recaptcha

Google Invisible reCaptcha for Yii2

Installs: 6 407

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Type:yii2-extension

0.2.1 2018-03-14 21:29 UTC

This package is not auto-updated.

Last update: 2024-11-10 03:00:22 UTC


README

Based on reCaptcha API 2.0. Fork of szaboolcs/yii2-invisible-recaptcha.

Installation

The preferred way to install this extension is through composer.

  • Either run

php composer.phar require --prefer-dist "pctux/yii2-invisible-recaptcha" "*"

or add

"pctux/yii2-invisible-recaptcha" : "*"

to the require section of your application's composer.json file.

  • Sign up for an reCAPTCHA API keys.
  • Add the next component to your configuration file (web.php)
'components' =>  [
  'captcha' => [
    'name'    => 'captcha',
    'class'   => 'pctux\recaptcha\InvisibleRecaptcha',
    'siteKey' => 'your siteKey',
    'secret'  => 'your secret'
  ]
]

Usage

login.php

<?php
use pctux\recaptcha\InvisibleRecaptcha;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
	'id' => 'login-form'
]);
echo $form->field($model, 'username');
echo $form->field($model, 'password');
echo InvisibleRecaptcha::widget([
  'name'         => 'Submit',
  'formSelector' => '#login-form'
]);
ActiveForm::end();

LoginController.php

<?php
namespace app\controllers;

use pctux\recaptcha\InvisibleRecaptchaValidator;
use app\models\Login;
use Yii;

class LoginController
{
  public function actionLoginForm()
  {
    $model = new Login();
    
    return $this->render('login', [
      'model' => $model
    ]);
  }

  public function actionLogin()
  {
    $model = new Login();

    $model->load(Yii::$app->request->post());

    if ($model->validate() && InvisibleRecaptchaValidator::validate(Yii::$app->request->post(InvisibleRecaptchaValidator::POST_ELEMENT)) && Yii::$app->user->login($model->getUser())) {
      return $this->goHome();
    }

    return $this->render('login', [
      'model' => $model
    ]);
  }
}

Login.php

<?php
use yii\base\Model;
use app\models\User;

class Login extends Model
{
  public $username;

  public $password;

  private $_user;

  public function rules()
  {
    // ...
  }

  public function getUser()
  {
    if (!$this->_user) {
      $this->_user = User::find()->byUsername($this->username)->one();
    }
    
    return $this->_user;
  }
}