Jump to content

Always Incorrect Credentials when logging in


sadboys

Recommended Posts

Hello, I'm currently using Yii2 Framework in developing my project. I encounter an error that whenever I try to login it always says "Incorrect Username or Password" even though my password is correct. I checked the user table in phpmyadmin and the password is getting hashed and there's no problem since the user information is stored in the user table without problems but if I try to login it doesn't accept the username or password. Anyone encounter a similar issue like mine?

 

Here's the SignupForm.php

class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $first_name;
    public $middle_name;
    public $last_name;
    public $contact;
    public $birth_date;
    public $type;
    public $external_type;
    public $status;
    public $region_id;
    public $barangay_id;
    public $province_id;
    public $city_municipal_id;



    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['email', 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],

            ['first_name', 'required'],
            ['first_name', 'string', 'max' => 45],

            ['middle_name', 'string', 'max' => 45],

            ['last_name', 'required'],
            ['last_name', 'string', 'max' => 45],

            ['contact', 'required'],
            ['contact', 'string', 'max' => 11],

            ['birth_date', 'required'],

            ['type', 'required'],
            ['type', 'string', 'max' => 45],

            ['external_type', 'string', 'max' => 45],

            ['status', 'string', 'max' => 45],

            ['region_id', 'required'],
            ['barangay_id', 'required'],
            ['province_id', 'required'],
            ['city_municipal_id', 'required'],


        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }
        
        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();

        $user->first_name = $this->first_name;
        $user->middle_name = $this->middle_name;
        $user->last_name = $this->last_name;
        $user->contact = $this->contact;
        $user->birth_date = $this->birth_date;
        $user->type = $this->type;
        $user->external_type = $this->external_type;
        $user->status = $this->status;
        $user->region_id = $this->region_id;
        $user->barangay_id = $this->barangay_id;
        $user->province_id = $this->province_id;
        $user->city_municipal_id = $this->city_municipal_id;

        return $user->save() ? $user : null;
            
    }

function login

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

function signup

public function actionSignup()
    {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }
Link to comment
Share on other sites

I see no parameters being passed to your login function. How is it supposed to be getting the username/email & password?

 

Updated Code:

 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,
            ]);
        }
    }

I tried using the default user table and it works perfectly fine. However if I use my own user table with the other additional columns it doesn't work. 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.