Login help

Dear all,

I entered incorrect login info, it told me "incorrect email or password".

I entered correct login info, it bring me back to site/index with my status remain as Guest.

Still no luck after few hours of trial and debug.

Appreciate if anyone could tell me where I did wrong.

SiteController




public function actionLogin()

{

    if(Yii::$app->user->isGuest)

    {

        $model = new LoginForm();


	if ($model->load(Yii::$app->request->post()) && $model->login())

	    return $this->goBack();


	return $this->render('login', ['model' => $model]);

    }

    else

        return $this->redirect(['index']);

    }

}



LoginForm




namespace app\models;


use Yii;

use yii\base\Model;


class LoginForm extends Model

{

    public $email;

    public $password;

    public $rememberMe = true;


    private $_user = false;




    /**

     * @return array the validation rules.

     */

    public function rules()

    {

        return [

                [['email', 'password'], 'required'],

                [['email'], 'email'],

                ['password', 'validateLogin'],

        ];

    }

	

    public function validateLogin($attribute, $params)

    {

        if (!$this->hasErrors()) {

            $user = $this->getCust();


            if (!$user || !$user->validateLogin($this->email, $this->password)) {

                $this->addError($attribute, 'Incorrect email or password.');

            }

        }

    }


    /**

     * Logs in a user using the provided username and password.

     * @return bool whether the user is logged in successfully

     */

    public function login()

    {

        if ($this->validate()) {

            return Yii::$app->user->login($this->getCust(), $this->rememberMe ? 3600*24*30 : 0);

        }

        return false;

    }


    /**

     * Finds user by [[username]]

     *

     * @return User|null

     */

    public function getCust()

    {

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

            $this->_user = Custs::validateLogin($this->email, $this->password);

        }


        return $this->_user;

    }

}



Custs.php




namespace app\models;


use yii\db\ActiveRecord;

use yii\web\IdentityInterface;


class Custs extends ActiveRecord implements IdentityInterface

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'my_custs';

    }

	

    public static function findIdentity($id)

    {

	$user = self::find()->where(["id" => $id])->one();

	if(!count($user))

		return null;

	

	return new static($user);

    }


    /**

     * @inheritdoc

     */

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

    {

	$user = self::find()->where(["accessToken" => $token])->one();

	if(!count($user))

		return null;

	

	return new static($user);

    }


    /**

     * Finds user by username

     *

     * @param string $username

     * @return static|null

     */

    public static function findByUsername($cmail)

    {		

	$user = self::find()->where(["cmail" => $cmail])->one();

	if(!count($user))

		return null;

	

	return new static($user);

    }


    /**

     * @inheritdoc

     */

    public function getId()

    {

        return $this->id;

    }


    /**

     * @inheritdoc

     */

    public function getAuthKey()

    {

	return $this->authKey;

    }

	

    public function validateAuthKey($authKey)

    {

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

    }


    /**

     * Validates password

     *

     * @param string $password password to validate

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

     */

    public function validatePassword($password)

    {

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

    }

	

    public static function hashPassword($email,$password)

    {

        return md5("$email."-".$password");

    }

	

    public static function validateLogin($email,$password)

    {		

	$user = self::find()->where(["cmail" => $email, "cpwd" => self::hashPassword($email,$password)])->one();

	

        if(!count($user))

	    return null;

		

        return new static($user);

    }

}



Thank you for time! ;)

Hi,

Have you checked your config file for identity class?

Are you exactly pointing your identity class?

The following code may help you





'user' => [

            'identityClass' => 'app\models\Custs',

            'enableAutoLogin' => true,

        ],



Yes! You are right!

Working charm now

Thank you very much ;)