Hemanth
(Hemanthguruhj)
November 4, 2015, 1:38pm
1
Just now i finished connection between yii2 and mongoDb. I have created one registration page through gii, using that i have entered some information and those are all stored in mongodb database.
Now i want to login using that username and password. How to write code and where should i write code. I am totally confused somebody please help me.
Hi,
First read:
http://www.yiiframework.com/doc-2.0/guide-security-authentication.html
When you have problems after that,
you could dig in the code of an advanced template.
(Just download, extract and browse through the files)
Look at following files:
advanced/common/models/LoginForm.php
advanced/common/models/User.php
advanced/frontend/controllers/SiteController.php (actionLogin and actionLogout)
advanced/frontend/views/site/login.php
There you should find everything needed to get login and logout working.
Regards
softark
(Softark)
November 4, 2015, 1:50pm
3
http://www.yiiframework.com/doc-2.0/guide-security-authentication.html
Your "identity class" should be the one that you have just created on mongoDb. You may have to modify it a bit to implement necessary methods of IdentityInterface.
The basic project template also provides good starting blocks.
Hemanth
(Hemanthguruhj)
November 5, 2015, 5:01am
4
Thanks for your quick reply.
Where should i write insert query, is in model?
softark
(Softark)
November 5, 2015, 10:46am
5
What do you mean by "insert query"?
Haven’t you already created an ActiveRecord class for your mongoDB collection? If you have, then I think you just need to call “save()” or “insert()” method to insert a row just as we do with the ActiveRecords of relational DB.
Hemanth
(Hemanthguruhj)
November 5, 2015, 11:12am
6
softark:
What do you mean by "insert query"?
Haven’t you already created an ActiveRecord class for your mongoDB collection? If you have, then I think you just need to call “save()” or “insert()” method to insert a row just as we do with the ActiveRecords of relational DB.
i done that with the save() method. Now i want to do login using the existing username and password that saved in ‘registration’ collection.
This is my controller
public function actionIndex()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new Login();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->render('index', [
'model' => $model,
]);
}
This is model
<?php
namespace app\models;
use Yii;
/**
* This is the model class for collection "login".
*
* @property \MongoId|string $_id
* @property mixed $username
* @property mixed $password
*/
class Login extends \yii\mongodb\ActiveRecord
{
/**
* @inheritdoc
*/
public $username;
public $password;
public $rememberMe = false;
public static function collectionName()
{
return ['user_registration', 'registration'];
}
/**
* @inheritdoc
*/
public function attributes()
{
return [
'_id',
'username',
'password',
'rememberme',
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'password'], 'safe'],
['rememberMe', 'boolean']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'_id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'rememberMe' => 'Remember Me',
];
}
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
public function getUser()
{
$collection = Yii::$app->mongodb->getCollection('registration');
$collection->find(['username'=>'robert']);
}
}
softark
(Softark)
November 5, 2015, 12:04pm
7
We use 3 classes to login user
app\models\LoginForm
yii\web\User
app\models\Something
is a model without any db backing storage. It is used to collect the user input like "username", "password", and "remember me" with the login form.
Usually the project template has already created it for you, and you only have to modify "getUser()" method so that it returns app\models\Something.
is a core component that manages the user authentication status. It uses app\models\Something to do the actual task of authentication.
We don’t need to touch it. All what we have to do with it is specifying its “identityClass”.
is the identity class that implements yii\web\IdentityInterface.
This is what you have to write for your app using your mongoDB collection of user information.
http://www.yiiframework.com/doc-2.0/guide-security-authentication.html#implementing-identity