ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้องเสมอ - Yii2

ฉันได้ติดตั้งแอปขั้นสูง Yii2 แล้ว ฉันปรับแต่งการสมัครใช้งานตามฐานข้อมูลผู้ใช้ของฉัน หลังจากสมัครใช้งาน ฉันพยายามเข้าสู่ระบบแล้วปรากฏว่า "Incorrect username or password" รหัสผ่านของฉันคือ qwerty และฉันได้ตรวจสอบหลายครั้งแล้ว แต่ก็ยังใช้งานไม่ได้

ตารางผู้ใช้

อย่างที่คุณเห็นที่นี่ ข้อมูลผู้ใช้หลังจากลงทะเบียนแล้วจะบันทึกข้อมูลลงในตารางผู้ใช้

รูปแบบการสมัครสมาชิก

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;
}
}

ฉันผิดตรงไหนเนี่ย? มันน่าสับสน ฉันคิดว่าโค้ดของฉันไม่มีอะไรผิดเพราะฉันได้ติดตามการตั้งค่าการติดตั้ง Yii2 Advanced App อย่างถูกต้อง

รูปแบบการเข้าสู่ระบบ

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

    return false;
}

person noobkoder    schedule 14.03.2018    source แหล่งที่มา
comment
ก่อนอื่น การติดตั้ง Yii2 อย่างเหมาะสม ไม่เกี่ยวอะไร กับความผิดพลาดของโค้ดของคุณ ประการที่สอง - คุณวางฟังก์ชัน signup() ที่นี่เท่านั้น ดังนั้นเราจะแก้ปัญหาของคุณได้อย่างไรหากคุณถามเกี่ยวกับ login()   -  person Yupik    schedule 14.03.2018
comment
เพราะเมื่อคุณสมัคร yii2 ทำให้ผู้ใช้เข้าสู่ระบบแล้วใช่ไหม? แต่หลังจากสมัครใช้งาน ระบบจะนำฉันไปที่หน้าดัชนีและผู้ใช้ไม่ได้เข้าสู่ระบบ ฉันได้ลองใช้ตารางผู้ใช้เริ่มต้นโดยใช้การโยกย้าย yii และทำงานได้ดี แต่เมื่อฉันใช้ตารางผู้ใช้ของตัวเอง มันก็ไม่ทำงาน อีกต่อไป.   -  person noobkoder    schedule 14.03.2018
comment
ฉันไม่เห็นผู้ใช้เข้าสู่ระบบหลังจากลงทะเบียนในรหัสของคุณ อัปเดตคำถามของคุณด้วยรหัสที่ถูกต้อง ในขณะนี้เราไม่สามารถช่วยคุณได้   -  person Yupik    schedule 14.03.2018
comment
ฉันคิดว่านั่นคือโค้ดที่ฉันให้ได้เท่านั้น ฉันปรับแต่งแอตทริบิวต์ของโมเดล SignupForm ตามคอลัมน์ในตารางผู้ใช้เท่านั้น ขอบคุณนะ   -  person noobkoder    schedule 14.03.2018
comment
เพิ่มฟังก์ชั่น singup/register จากคอนโทรลเลอร์ของคุณ   -  person Muhammad Omer Aslam    schedule 14.03.2018


คำตอบ (3)


ตรวจสอบตารางผู้ใช้ของคุณใน phpmyadmin ดูที่คอลัมน์สถานะ หากค่านี้ไม่เท่ากับ 10 คุณสามารถเปลี่ยนด้วย 10

person Mahmut Aydın    schedule 12.09.2019

แม้ว่าคุณจะไม่ได้เพิ่มการดำเนินการสำหรับการลงทะเบียนหรือการลงทะเบียน แต่จุดที่คุณเรียกใช้ฟังก์ชัน $model->signup() จากโมเดล คุณต้องตรวจสอบภายในคำสั่ง if แล้วเพิ่มการเรียกไปที่ \Yii::$app->user->login($userModel); ภายใน มันจะเข้าสู่ระบบคุณหลังจากนั้น ลงชื่อ.

ฟังก์ชัน signup() ของคุณส่งคืนอ็อบเจ็กต์โมเดลผู้ใช้หลังจากแทรกผู้ใช้ในตาราง

ดูตัวอย่างโค้ดด้านล่าง

if(($userModel=$model->signUp())!==null){
   \Yii::$app->user->login($userModel);
}

หวังว่ามันจะช่วยคุณได้

person Muhammad Omer Aslam    schedule 14.03.2018
comment
จริงๆ แล้ว มันมีอยู่แล้วใน SiteController pastebin.com/NBJuse8X - person noobkoder; 15.03.2018
comment
@noobkoder คุณสามารถเพิ่มฟังก์ชัน $user->setPassword(); ได้ไหม แม้ว่าฉันจะเห็นว่ารหัสผ่านถูกแฮชในตาราง และเมื่อคุณถูกเปลี่ยนเส้นทางไปยังหน้าเข้าสู่ระบบหลังจากสมัครใช้งาน มันแสดงข้อความชื่อผู้ใช้/รหัสผ่านผิดหรือไม่ ลองเพิ่ม var_dump($this->validate()) และ print_r($this->attributes) ภายในฟังก์ชัน login() แล้วดูว่าฟังก์ชันพิมพ์อะไรบ้าง - person Muhammad Omer Aslam; 15.03.2018
comment
หลังจากสมัครใช้งาน ระบบจะนำฉันไปยังหน้าดัชนี แต่ผู้ใช้ไม่ได้เข้าสู่ระบบ แต่ถ้าฉันพยายามเข้าสู่ระบบ ระบบจะแจ้งว่าข้อมูลประจำตัวไม่ถูกต้อง - person noobkoder; 16.03.2018

คุณได้ลองตั้งค่าคลาสโมเดลผู้ใช้ของคุณเพื่อใช้อินเทอร์เฟซประจำตัวหรือไม่?

ก่อนอื่นให้ประกาศชั้นเรียนของคุณเช่นนั้น

class MyUser extends ActiveRecord implements IdentityInterface

ตอนนี้คุณต้องนำวิธีการเหล่านี้ไปใช้ที่ไหนสักแห่งในชั้นเรียนของคุณ

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;
    }

ถัดไปไปที่ส่วนส่วนประกอบของคุณ /config/web.php และค้นหาส่วนประกอบ 'user'

'user' => [
        'identityClass' => 'app\models\MyUser', //<---Your model class
        'enableAutoLogin' => true,
    ],

ฉันคิดว่าคุณจะไปได้ดี แจ้งให้เราทราบหากไม่ได้ผล

person natral    schedule 14.03.2018
comment
อันนี้อยู่ที่ Yii2 Advanced User Class โดยอัตโนมัติแล้ว - person noobkoder; 15.03.2018