I hope you can help me before I'm going crazy. I'm trying to login a user. This is the actionLogin
public function actionLogin() {
$model = new User();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$arrPost = Yii::$app->request->post();
$identity = User::findOne(['email'=>$arrPost['User']['email'],'password'=>$arrPost['User']['password']]);
$identity->id = $identity->getAttribute('id');
$identity->last_name = $identity->getAttribute('last_name');
$identity->first_name = $identity->getAttribute('first_name');
$identity->admin = $identity->getAttribute('admin');
$identity->address = $identity->getAttribute('address');
$identity->ministry = $identity->getAttribute('ministry');
\yii::$app->user->login($identity,86400);
return $this->redirect(Url::to(['app/index']));
} else {
// either the page is initially displayed or there is some validation error
return $this->render("@app/forms/user/login", ['model' => $model]);
}
}
If I'm doing a var_dump after
\yii::$app->user->login
I got the following dump
object(app\models\User)#88 (17) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> NULL ["admin"]=> bool(true) ["password"]=> NULL ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "" ["auth_key"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "Diakon" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "Diakon" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }
You see, the the fields for id, first_name, last_name etc. are filled. After an redirect to the entry page and doing a var_dump on
\yii::$app->user->identity
I got the following content:
object(app\models\User)#93 (17) { ["id"]=> NULL ["first_name"]=> NULL ["last_name"]=> NULL ["email"]=> NULL ["admin"]=> NULL ["password"]=> NULL ["address"]=> NULL ["ministry"]=> NULL ["auth_key"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Koester" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Dear Christian" ["ministry"]=> string(6) "" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Koester" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Dear Christian" ["ministry"]=> string(6) "" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }
You see that the contents of id, first_name, last_name etc. are gone. It is possible for to ask if the user is a guest, which is returned as wrong (the user is logged in). But it is not possible for me to get the values from fields like id, first_name, last_name etc, because they are NULL:
Config file for users:
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
],
Can anybody help me before I'm going mad!
Thanks Chris
There are lots of unnecessary code...it can be simply done by following if all the other things are as planned.
public function actionLogin() {
$model = new User();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
\yii::$app->user->login($model); //does your user model have login function??
return $this->redirect(Url::to(['app/index']));
} else {
return $this->render("@app/forms/user/login", ['model' => $model]);
}
}
It works as expected. From your 2nd dump it is clear the User model is loaded, eg "_attributes" is filled.
My assumption is that you have some public variables inside your User model(id, last_name, first_name etc), eg:
class User {
public $id;
public $last_name;
...
}
In your actionLogin(), you find your model and set the public variables for your model and then try to login. Although you set these before login, they are not set after the redirect. This is expected, as your model implements "yii\web\IdentityInterface".
When your login is successful, only the id of the identity is saved in the session, which is given by:
$identity->getId()
not the User model itself.
After the redirect to the entry page, the identity is loaded using the id saved in your session, using the function:
public static function findIdentity($id)
of the User model.
If you want to add your own variables, you can edit(or override) this function in your model. eg.
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
change to:
public static function findIdentity($id)
{
$identity = static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
$identity->id = $identity->getAttribute('id');
$identity->last_name = $identity->getAttribute('last_name');
// etc.
return $identity;
}