Reputation: 97
I've installed Yii2 advanced app, I customized the signup according to my user database. After signing up I tried to log in and it says "Incorrect username or password"
, my password is qwerty also I've checked it many times and it still does not work.
The signup model
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;
}
}
Where did I get wrong here? It is confusing I think that there's no wrong in my code because I followed the proper installation setup of Yii2 Advanced App.
Login Model
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
return false;
}
Upvotes: 0
Views: 1584
Reputation: 869
check your user table in phpmyadmin, look at the status column, if this value not equal 10, you can change with 10.
Upvotes: 3
Reputation: 1098
Have you tried setting your user model class to implement the identity interface?
First declare your class like so
class MyUser extends ActiveRecord implements IdentityInterface
now somewhere in your class you need to implement these methods
public static function findIdentity($id)
{
return self::findOne(['id'=>$id]);
}
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException("Validation method not supported as of yet.");
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
next go to the components section of you /config/web.php and search for the 'user'
component
'user' => [
'identityClass' => 'app\models\MyUser', //<---Your model class
'enableAutoLogin' => true,
],
I think with that your good to go. Let me know if this doesn't work.
Upvotes: 0
Reputation: 23768
Although you have'nt added the action for the registration or signup, but the point where you are calling the $model->signup()
function from the model you must check it inside the if
statement and then add a call to \Yii::$app->user->login($userModel);
inside, it will log you in after signup.
Your signup()
function returns the user model object after inserting the user in the table.
See below code sample
if(($userModel=$model->signUp())!==null){
\Yii::$app->user->login($userModel);
}
Hope it helps you out
Upvotes: 0