Cannot Made Login Using Database Working (User Model)

I’ve install the basic structure of Yii 2.0-alpha latest version.

And i’m trying to change the login to use the data from my database which has following field

  • uid

  • username

  • password

  • authKey

  • email

All i did it using Gii to overwrite new generated User model and did implement IdentityInterface and not forgetting to add the implemented method.

I’m also add validatePassword & findByUsername method to corresponding to the existed demo coding.

the getId method also return $this->uid since the name is different.

Unfortunately, after login complete, there is no any error show in every channel (error_log.log / yii log / browser etc.)

The browser just say, not receiving data with HTTP 302 status. and php error_log just empty.

That weird.

If anyone would have any suggestion, i’d be really appreciated.

Here is my coding in User.php file


<?php


namespace app\models;


/**

 * This is the model class for table "user".

 *

 * @property integer $uid

 * @property string $username

 * @property string $password

 * @property string $email

 * @property string $authKey

 */

class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface

{

	/**

	 * @inheritdoc

	 */

	public static function tableName()

	{

		return 'user';

	}


	/**

	 * @inheritdoc

	 */

	public function rules()

	{

		return [

			[['username', 'password', 'email', 'authKey'], 'required'],

			[['username', 'email'], 'string', 'max' => 50],

			[['password', 'authKey'], 'string', 'max' => 255]

		];

	}


	/**

	 * @inheritdoc

	 */

	public function attributeLabels()

	{

		return [

			'uid' => 'Uid',

			'username' => 'Username',

			'password' => 'Password',

			'email' => 'Email',

			'authKey' => 'Auth Key',

		];

	}


	public function getAuthKey() {

		return $this->authKey;

	}


	public function getId() {

		return $this->uid;

	}


	public function validateAuthKey($authKey) {

		return $this->authKey === $authKey;

	}

	

	public function validatePassword($pwd) {

		return (crypt($pwd, $this->password) == $this->password);

	}


	public static function findByUsername($username){

		$model = self::find(['username' => $username]);

		if(isset($model)){

			return new static($model->getAttributes());

		}else{

			return null;

		}

		

	}

	

	public static function findIdentity($id) {

		$model = self::find($id);

		if(isset($model)){

			return new static($model->getAttributes());

		}else{

			return null;

		}

	}

}



Here is what does the yii log say

5190

Screen Shot 2014-02-09 at 1.59.23 AM.png

I wonder what’s the meaning of this


return new static($model->getAttributes());

when you can just


return $model;

Thank for your advice,

i try to imitate the code in the basic and it written like this (new static of data array).

I check that the return is and object of the values, so that’s why i made that line.

Is returning activeRecord working fine?

After made the change, the web still not working with not error (http 302)

Yes, because basic app doesn’t use AR.

Works for me )

302 is not an error, it’s redirect.

You should follow the login process line by line to see what has been modified.

You can also post some code here (login action, login form…)

Current code was changed significantly since alpha so it’s better to update.

Thank you, I’ll wait for it.

I found that there is something on redirect, after the method login, the redirect doesn’t work any more. (But if i change that redirect to render, it work fine)


		


	public function actionLogin()

	{

		if (!\Yii::$app->user->isGuest) {

			$this->goHome();

		}

		$model = new LoginForm();

		if ($model->load($_POST) && $model->login()) {

// 			return $this->render('index'); << Work Fine

			return $this->goBack(); //<< Don't Work

		} else {

			return $this->render('login', [

				'model' => $model,

			]);

		}

In case anyone confront this problem, try to turn off any cache in your webserver

After @samdark told me to do so, i try to config mamp to disable XCache which open by default and everything now going fine.