Reputation: 57
I am using the Yii2 basic template to create an admin panel.
For the admin login I have used the useridentity
class and the default user model of yii.
Now I have to create the login for the frontend. What should I do to make the separate login so that I can login in the same browser in frontend and backend?
In basic template I'm facing problem with same identity class and model.
I have to use the same user table of database for admin and frontend user.
Please suggest the best way to manage this.
Upvotes: 2
Views: 644
Reputation: 892
This is actually the correct way to do it and to work around this "Problem" is to declare different sessions for your frontend and backend.
Yii2 advanced template has this feature included in the stock and I would suggest you not to start inventing the wheel again and just move your project to the advanced template.
Ofcourse you can create a new model and a new table for just the admins. However this new class should still implement IdentityInterface
Something where your AdminUserModel looks something like this:
namespace app\models;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface
class AdminUser extends ActiveRecord implements IdentityInterface
{
/** Dont froget all the related stuff like behaviours, properties etc **/
/**
* @inheritdoc
*/
public static function tableName()
{
return 'admin_user';
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
}
And if you decide to go this route, I would even suggest you to extend regular User model in AdminUser model, since a lot of the properties and functionality will be the same.
Upvotes: 1