Jump to content

Always Incorrect Credentials when logging in


sadboys
Go to solution Solved by gizmola,

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. 

Edited by sadboys
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.