Answer the question
In order to leave comments, you need to log in
Yii 2 user authorization from database?
Perhaps it makes sense to use the built-in functionality.
here are the signal models
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return null;
}
<?php
use yii\helpers\Html;
use frontend\models\SignupForm;
/* @var $this yii\web\View */
/* @var $model backend\models\MyUser */
$this->title = 'Create My User';
$this->params['breadcrumbs'][] = ['label' => 'My Users', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="my-user-create">
<h1><?= Html::encode($this->title) ?></h1>
<?php
$model = new SignupForm();
if ($model->load(Yii::$app->request->post()))
{
if ($user = $model->signup())
{
if (Yii::$app->getUser()->login($user))
{
return $this->goHome();
}
}
}
echo $this->render('_signup', [
'model' => $model,
])
?>
</div>
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */
$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to signup:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->username = $this->username;
//$user->email = $this->email;
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
$this->auth_key = Yii::$app->security->generateRandomString();
//$user->setPassword($this->password);
//$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return null;
}
Answer the question
In order to leave comments, you need to log in
the salvation of the drowning is the work of the drowning themselves
What does it mean to create a hash and a password in another field?
$user->setPassword($this->password); This is what creates a hash and puts it in password_hash.
$user->generateAuthKey(); This is what generates AuthKey (this is for the Remember Me checkbox, it still seems to put it in the cookie for the user)
Or did I not understand the question?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question