Yii2注册后自动登录

时间:2017-07-18 02:07:49

标签: yii2 yii2-advanced-app

我正在使用yii2框架创建网站。我在注册时遇到问题。我家里有一个模态,它包含注册表单。现在,当我尝试注册时,是的,它保存成功,但它只保留在模态中。现在我想要的是在注册后会自动登录。

这是我的注册表单_:

<div class="row">
    <div class="col-lg-12">
        <?php yii\widgets\Pjax::begin(['id' => 'sign-up']) ?>
            <?php $form = ActiveForm::begin(['id' => 'form-signup', 'options' => ['data-pjax' => true]]); ?>
                <div class="row">
                <div class="col-sm-6">
                <?= $form->field($model, 'role')->dropDownList(['2' => 'User', '1' => 'Encoder', '3' => 'Admin']) ?>
                <?= $form->field($model, 'username')->textInput(['placeholder' => 'Username....']) ?>
                <?= $form->field($model, 'email')->textInput(['placeholder' => 'Email....']) ?>
                <?= $form->field($model, 'password')->passwordInput(['placeholder' => 'Password.....']) ?>
                </div>
                 <div class="col-sm-6">
                <?= $form->field($model, 'confirmPassword')->passwordInput(['placeholder' => 'Confirm Password.....']) ?>
                <?= $form->field($model, 'first_name')->textInput(['placeholder' => 'First Name....']) ?>
                <?= $form->field($model, 'middle_name')->textInput(['placeholder' => 'Middle Name....']) ?>
                <?= $form->field($model, 'last_name')->textInput(['placeholder' => 'Last Name....']) ?> 
                </div>
                </div>
                <center>
                <?= $form->field($model, 'verifyCode')->widget(Captcha::className()) ?>               
                </center>
                <div class="form-group">
                    <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button', 'style' => 'width: 100%; padding: 10px;']) ?>
                </div>
            <?php ActiveForm::end(); ?>
        <?php yii\widgets\Pjax::end() ?>
    </div>
</div>

这是我的模特:

class SignupForm extends Model
{
public $role;
public $username;
public $email;
public $password;
public $first_name;
public $middle_name;
public $last_name;
public $confirmPassword;
public $verifyCode;

public function rules()
{
    return [
        ['role', 'required'],
        ['username', 'trim'],
        ['username', 'required'],
        ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
        ['username', 'string', 'min' => 2, 'max' => 20],
        ['email', 'trim'],
        ['email', 'required'],
        ['email', 'email'],
        ['email', 'string', 'max' => 30],
        ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
        ['password', 'required'],
        ['password', 'string', 'min' => 6],
        ['first_name', 'trim'],
        ['first_name', 'required'],
        ['middle_name', 'trim'],
        ['middle_name', 'required'],
        ['last_name', 'trim'],
        ['last_name', 'required'],
        ['verifyCode', 'captcha'],
        ['verifyCode', 'required'],
        [['confirmPassword'], 'compare', 'compareAttribute' => 'password', 'message' => 'Passwords do not match.'],

    ];
  }

/**
 * Signs user up.
 *
 * @return User|null the saved model or null if saving fails
 */
public function signup()
{
    if (!$this->validate()) {
        return null;
    }

    $user = new User();
    $user->username = $this->username;
    $user->email = $this->email;
    $user->setPassword($this->password);
    $user->generateAuthKey();
    $user->role = $this->role;
    $user->first_name = $this->first_name;
    $user->middle_name = $this->middle_name;
    $user->last_name = $this->last_name;

    return $user->save() ? $user : null;
}
}

这是我的控制者:

  public function actionSignup()
{
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        if ($user = $model->signup()) {
            if (Yii::$app->getUser()->login($user)) {
                return $this->goHome();
            }
        }
    }

    return $this->renderAjax('signup', [
        'model' => $model,
    ]);
}

我没有任何想法,我认为我的代码是正确的,但我不知道为什么不起作用。

已更新

  • 当我点击按钮注册时,它只保留在模态中,当我再次单击按钮时,验证工作正常。这意味着它保存到数据库但不会自动登录。

2 个答案:

答案 0 :(得分:0)

您应该获得登录用户身份

public function actionSignup()
{
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {

        if ($user = $model->signup()) {
            $identity = User::findOne(['username' => $model->$username]);
             if (Yii::$app->user->login($identity)) {
                return $this->goHome();
            }
        }
    }

    return $this->renderAjax('signup', [
        'model' => $model,
    ]);
}

了解更多http://www.yiiframework.com/doc-2.0/guide-security-authentication.html

http://www.yiiframework.com/doc-2.0/yii-web-user.html

答案 1 :(得分:0)

我使用了以下代码,它为我工作。它也是YII2的标准代码。控制器动作代码:

public function actionSignup()
{
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        if ($user = $model->signup()) {
            $model= \common\models\User::findOne([$user->id]); //you need to get complete model again and pass it to login function
            if (Yii::$app->user->login($model) {
                return $this->goHome();
            }
        }
    }

    return $this->renderAjax('signup', [
        'model' => $model,
    ]);
}