Wednesday, December 16, 2015

Yii2: Login With MySQL Database

1. Create a Table named Login

CREATE TABLE IF NOT EXISTS `login` (

  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(30) NOT NULL,
  `password` varchar(30) NOT NULL,
  `authKey` varchar(50) NOT NULL,
  `accessToken` varchar(50) NOT NULL,
  `role` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2. Insert sample data into the table

INSERT INTO `yii2`.`login` (`id`, `username`, `password`, `authKey`, `accessToken`, `role`) VALUES (NULL, 'john', 'john1234', 'key1234567', 'token1234567', '1');

3. Create Model for the table. You may use Gii to generate the model

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "login".
 *
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $authKey
 * @property string $accessToken
 * @property integer $role
 */
class Login extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'login';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'password', 'authKey', 'accessToken', 'role'], 'required'],
            [['role'], 'integer'],
            [['username', 'password'], 'string', 'max' => 30],
            [['authKey', 'accessToken'], 'string', 'max' => 50]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'authKey' => 'Auth Key',
            'accessToken' => 'Access Token',
            'role' => 'Role',
        ];
    }

}


4. Open User.php file in models/User.php. Goto to findIdentity static function

Original code

public static function findIdentity($id)
{
   return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}

Change to

public static function findIdentity($id)
{
   $user = Login::findOne($id);
   if(count($user)){
      return new static($user);
   }
   return null;
}

5. Change static function findIdentityByAccessToken

Original Code

public static function findIdentityByAccessToken($token, $type = null)
{
        foreach (self::$users as $user) {
            if ($user['accessToken'] === $token) {
                return new static($user);
            }
        }

        return null;
}


Change to

public static function findIdentityByAccessToken($token, $type = null)
{
        $user = Login::find()->where(['accessToken'=>$token])->one();
        if(count($user)){
            return new static($user);
        }
        return null;
}

6. Change static function findByUsername

Original Code

public static function findByUsername($username)
{
        foreach (self::$users as $user) {
            if (strcasecmp($user['username'], $username) === 0) {
                return new static($user);
            }
        }

        return null;
}

Change to


public static function findByUsername($username)
{
        $user = Login::find()->where(['username'=>$username])->one();
        if(count($user)){
            return new static($user);
        }
        return null;
}



7. Now you can test the login page with test data.










1 comment:

  1. super very simple and easy to uderstand beginers in this type

    ReplyDelete