Yii2 - Login functionality

Hi ,

I have one problem , in my localhost login working fine but in my server its not working . If i login correct username and password also its redirecting as same page . Its not logged in .If anyone know plz tell how to solve the problem.

Some question…

What do you use the User table to authenticate?

If yes did you populated the server database?

Post your login action so we can understand a bit more…

Thank you for your reply

User.php :




<?php


namespace app\models;


use yii\base\NotSupportedException; // For handling exceptionn

use yii\db\ActiveRecord; // For handling activerecord

use yii\helpers\Security; // For handling security purpose

use yii\web\IdentityInterface; // For handling user identityInterface


use yii\behaviors\TimestampBehavior; // For handling timeBehaviors

use yii\db\Expression; // For handling Dbexpressions


class User extends ActiveRecord implements IdentityInterface

{

	/**

	 * @inheritdoc

	 */

	public static function tableName()

	{

		return 'tbl_user';

	}

   


	/**

 	* @inheritdoc

 	*/

	public static function findIdentity($id)

	{

    	//To get the user value and fetch into db

		return static::findOne(['id' => $id]);

	}


	/**

 	* @inheritdoc

 	*/

	public static function findIdentityByAccessToken($token, $type = null)

	{

    	foreach (self::$users as $user) {

        	if ($user['accessToken'] === $token) {

            	return new static($user);

        	}

    	}

    	return null;

	}


	/**

 	* Finds user by username

 	*

 	* @param  string  	$username

 	* @return static|null

 	*/

	public static function findByUsername($username)

	{

		return static::findOne(['user_name' => $username]);

	}


	/**

 	* @inheritdoc

 	*/

	public function getId()

	{

    	return $this->getPrimaryKey();

	}


	/**

 	* @inheritdoc

 	*/

	public function getAuthKey()

	{

    	return isset($this->auth_key) ? $this->auth_key  : NULL;

	}


	/**

 	* @inheritdoc

 	*/

	public function validateAuthKey($authKey)

	{

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

	}


	/**

 	* Validates password

 	*

 	* @param  string  $password password to validate

 	* @return boolean if password provided is valid for current user

 	*/

	public function validatePassword($password)

	{

    	return $this->password === $password;

	}

	

	/**

	 * Using behaviours method save the created and updated timemin db 

	 * @see \yii\base\Component::behaviors()

	 */

   	public function behaviors()

	{

		return [

				'timestamp' => [

						'class' => TimestampBehavior::className(),

						'attributes' => [

								ActiveRecord::EVENT_BEFORE_INSERT => ['created', 'updated'],

								ActiveRecord::EVENT_BEFORE_UPDATE => ['updated']

						],

						'value' => new Expression('NOW()'),

				],

		];

	}

}



LoginForm.php :




<?php


namespace app\models;


use Yii;

use yii\base\Model;


/**

 * LoginForm is the model behind the login form.

 */

class LoginForm extends Model

{

	public $username;

	public $password;

	public $rememberMe = true;


	private $_user = false;




	/**

 	* @return array the validation rules.

 	*/

	public function rules()

	{

    	return [

        	// username and password are both required

        	[['username', '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 [[username]]

 	*

 	* @return User|null

 	*/

	public function getUser()

	{

    	if ($this->_user === false) {

        	$this->_user = User::findByUsername($this->username);

    	}


    	return $this->_user;

	}

}



So you use a databse for authenticating: did you populated the server database?

In your controller (probably the site one) you should have a actionLogin(), can you post this a s well?