Yii2: Login on server doesn't work

Created on 29 Dec 2014  Â·  16Comments  Â·  Source: yiisoft/yii2

Hello everyone,

There is a strange problem with login on server. It works on local.

It says:


PHP Notice – yiibaseErrorException

Array to string conversion

  1. in /var/www/html/vendor/yiisoft/yii2/web/User.php at line 240
  2. in /var/www/html/vendor/yiisoft/yii2/web/User.php – yiibaseErrorHandler::handleError(8, 'Array to string conversion', '/var/www/html/vendor/yiisoft/yii...', 240, ...) at line 240
  3. in /var/www/html/common/models/LoginForm.php – yiiwebUser::login(commonmodelsUser, 2592000) at line 59
  4. in /var/www/html/frontend/controllers/SiteController.php – commonmodelsLoginForm::login() at line 81

Line 240 from yii2/web/User.php is:

$log = "User '$id' logged in from $ip with duration $duration.";

die(print_r($id)) on server returns: Array ( ) 1 and on localhost it returns: 41. Of course, I can change the line to $log = "User logged in frm $ip with duration $duration" and there is no error, but there is no login too. Why id is set to an empty array? E-mail and password validation was fine..

Thanks!
Best regards.

need more info

Most helpful comment

goto:
commonmodelsuser.php
then search function:

public function getId()
{
return $this->getPrimaryKey();
}

change to:

public function getId()
{
return $this->id;
}

All 16 comments

please show more of your code that is responsible for creating the user instance and doing the login.

This is site/login:

public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

This is login's model (common/models/LoginForm)

 public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0) ;
        } else {
            return false;
        }
    }

That's everything I think. I don't know how $this->validate() can return true and the app doesn't log me in.

Then the problem lies most probably in LoginForm::getUser() or in your user model class. (getId() or findIdentity*() methods).

But why it works on my localhost?

there could be 1000 reasons for that, I can only try figuring out when you give the requested information. Otherwise you have to figure it out yourself.

This is LoginForm:

<?php
namespace common\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class LoginForm extends Model
{
    public $email;
    public $password;
    public $rememberMe = true;

    private $_user = false;


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['email', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, 'Incorrect username or password.');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     *
     * @return boolean whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
        } else {
            return false;
        }
    }

    /**
     * Finds user by [[email]]
     *
     * @return User|null
     */
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user = User::findByEmail($this->email);
        }

        return $this->_user;
    }
}

And this is User.php (Model)

<?php
namespace common\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

/**
 * User model
 *
 * @property integer $id
 * @property string $email
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property string $auth_key
 * @property integer $role
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $password write-only password
 */
class User extends ActiveRecord implements IdentityInterface
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    const ROLE_USER = 10;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%users}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],

            ['role', 'default', 'value' => self::ROLE_USER],
            ['role', 'in', 'range' => [self::ROLE_USER]],
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['IDuser' => $id, 'status' => self::STATUS_ACTIVE]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by email
     *
     * @param string $email
     * @return static|null
     */
    public static function findByEmail($email)
    {
        return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]);
    }

    /**
     * Finds user by password reset token
     *
     * @param string $token password reset token
     * @return static|null
     */
    public static function findByPasswordResetToken($token)
    {
        if (!static::isPasswordResetTokenValid($token)) {
            return null;
        }

        return static::findOne([
            'password_reset_token' => $token,
            'status' => self::STATUS_ACTIVE,
        ]);
    }

    /**
     * Finds out if password reset token is valid
     *
     * @param string $token password reset token
     * @return boolean
     */
    public static function isPasswordResetTokenValid($token)
    {
        if (empty($token)) {
            return false;
        }
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
        $parts = explode('_', $token);
        $timestamp = (int) end($parts);
        return $timestamp + $expire >= time();
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }

    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
    }

    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }
}

Everything what we get from default is the same. Except replace username -> email.

Foreign key problem.

@talovicnedim Hi! Can you please tell where the problem was? I'm having same issue

@Webkadabra, can you post your full stacktrace?

@dynasource this happens occasionally, I'm not sure how to test for it. I will post update if I figure it out!

@dynasource looks like it is reproducable if I go to Gii interface first, and then try to login. Still working on it

@Webkadabra there was a problem with a foreign key, but I really don't remember anything specific.

It is happening due to the id field in the user database model is not a primary key. Simply add the PK constraint to user->id field from the database.

From DB console:

ALTER TABLEuserADD PRIMARY KEY(id) NOT NULL AUTO_INCREMENT ;

goto:
commonmodelsuser.php
then search function:

public function getId()
{
return $this->getPrimaryKey();
}

change to:

public function getId()
{
return $this->id;
}

@ptrnov thanx! It worked!

Set primary key for all of your tables and be happy

Was this page helpful?
0 / 5 - 0 ratings