Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
systems:yii2:basic_user_login_from_database [2018/04/03 08:43]
smayr
systems:yii2:basic_user_login_from_database [2018/04/05 17:49] (current)
smayr
Line 3: Line 3:
 Perform these steps: Perform these steps:
   * First create a ''user'' database table with your own requirements.   * First create a ''user'' database table with your own requirements.
-  * In Gii, generate a User model. In this model, implement the ''IdentityInterfaceclass User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface'' +  * In Gii, generate a User model. In this model, implement the ''IdentityInterface''. <code php>class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface 
-  * Now in Login form (''[app]/site/login.php''), define User model so it will return back the userclass ''LoginForm extends Model { public $username; public $password; public $email; public $rememberMe = true;''+
 +    //... 
 +}</code> 
 +  * Now in ''LoginForm'' model (''[app]/models/LoginForm.php''), define: <code php>class LoginForm extends Model  
 + 
 +    public $username;  
 +    public $password;  
 +    public $email;  
 +    public $rememberMe = true; 
 +
 +</code>
  
 User Table:(''user.sql'') User Table:(''user.sql'')
Line 22: Line 32:
   PRIMARY KEY (`id`),   PRIMARY KEY (`id`),
   UNIQUE KEY `username` (`username`),   UNIQUE KEY `username` (`username`),
-  INDEX `FK_contact_external_customer_id` (`external_customer_id`), +
-  CONSTRAINT `FK_contact_customer_idFOREIGN KEY (`customer_id`)  +COLLATE='utf8_unicode_ci' 
-      REFERENCES `customer` (`id`) ON UPDATE CASCADE ON DELETE CASCADE+ENGINE=InnoDB 
 +AUTO_INCREMENT=1; 
 +</code> 
 + 
 +Or: 
 +<code sql> 
 +CREATE TABLE `user` ( 
 +  `idint(11NOT NULL AUTO_INCREMENT
 +  `first_namevarchar(255) NOT NULL, 
 +  `last_namevarchar(255NOT NULL, 
 +  `usernamevarchar(255) NOT NULL, 
 +  `phonevarchar(255) NOT NULL, 
 +  `email` varchar(255) NOT NULL, 
 +  `password` varchar(255) NOT NULL, 
 +  `password_hash` varchar(255) NOT NULL, 
 +  `auth_key` varchar(255) NOT NULL, 
 +  `password_reset_token` varchar(250) NOT NULL, 
 +  `avatar` varchar(255) NOT NULL, 
 +  `role` enum('Admin','Manager','Editor','Author','PowerUser','Registered'NOT NULL DEFAULT 'Registered'
 ) )
 COLLATE='utf8_unicode_ci' COLLATE='utf8_unicode_ci'
Line 67: Line 95:
     public static function findIdentity($id)      public static function findIdentity($id) 
     {     {
-        $user = self::find() +        $user = self::find()->where(["id" => $id])->one();
-                ->where([ +
-                    "id" => $id +
-                ]) +
-                ->one();+
         if (!count($user)) {         if (!count($user)) {
             return null;             return null;
Line 83: Line 107:
     public static function findIdentityByAccessToken($token, $userType = null)      public static function findIdentityByAccessToken($token, $userType = null) 
     {     {
-        $user = self::find() +        $user = self::find()->where(["accessToken" => $token])->one();
-                ->where(["accessToken" => $token]) +
-                ->one();+
         if (!count($user)) {         if (!count($user)) {
             return null;             return null;