Answer the question
In order to leave comments, you need to log in
How to authenticate a user in Yii2?
Hello. I'm trying to switch from microframeworks to full-fledged ones, namely Yii2 or Laravel, but from my point of view, they are both almost equally incomprehensible. Yii2 already has basic tables with basic fields for registration and authorization and so on. I want to use my fields and my authentication logic. For starters, at least the fields. Everything according to the instructions.
My controller:
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\User;
class TestController extends Controller
{
public function actionRegister() {
$user=User::findOne(["id"=>1]);
Yii:$app->user->login($user);
if(Yii::$app->user->isGuest) {
return "Вы гость";
}
}
}
namespace app\models;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'users';
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
// return 1;
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
// return $this->authKey;
}
public function validateAuthKey($authKey)
{
//return $this->authKey === $authKey;
}
}
Yii:$app->user->login($user);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question