I changed User Controller and User Model, at least to check if I can authenticate a user, but without luck. Postman (basic auth with username and password) still return
"name": "Unauthorized",
"message": "You are requesting with an invalid credential.",
"code": 0,
"status": 401,
"type": "yii\\web\\UnauthorizedHttpException"
This is my actual api config
<?php
//$db = require(__DIR__ . '/../../config/db.php');
$params = require(__DIR__ . '/../../config/params.php');
$config = [
'id' => 'basic',
'name' => 'test',
// Need to get one level up:
'basePath' => dirname(__DIR__).'/..',
'bootstrap' => ['log'],
'language' => 'it-IT', // da fare: ottenere il valore dal browser
'timeZone' => 'Europe/Rome',
'components' => [
'request' => [
'class' => '\yii\web\Request',
'enableCookieValidation' => false,
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
'user' => [
'identityClass' => 'app\api\modules\v1\models\User',
'enableSession' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/../../config/db.php'),
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['v1/user']],
/*'OPTIONS v1/user/login' => 'v1/user/login',
'POST v1/user/login' => 'v1/user/login',*/
],
],
],
'modules' => [
'v1' => [
'class' => 'app\api\modules\v1\Module',
],
],
'params' => $params,
];
return $config;
This User.php model:
<?php
namespace app\api\modules\v1\models;
use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\base\NotSupportedException;
/**
* This is the model class for table "user".
*
* @property string $id
* @property string $role_id
* @property integer $status
* @property string $email
* @property string $new_email
* @property string $username
* @property string $password
* @property string $auth_key
* @property string $api_key
* @property string $login_ip
* @property string $login_time
* @property string $create_ip
* @property string $create_time
* @property string $update_time
* @property string $ban_time
* @property string $ban_reason
*/
class User extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'user';
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['api_key' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->auth_Key === $authKey;
}
}
And finally my UserController.php:
<?php
namespace app\api\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use yii\filters\auth\HttpBasicAuth;
use yii\helpers\ArrayHelper;
use yii\base\NotSupportedException;
use yii\web\Response;
use yii\filters\ContentNegotiator;
use app\api\modules\v1\models\User;
class UserController extends ActiveController
{
public $modelClass = 'app\api\modules\v1\models\User';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
'auth' => [$this, 'auth']
];
return $behaviors;
}
public function auth($username, $password) {
return User::findOne([
'username' => $username,
'password' => $password,
]);
}
I read the official documentation and every blog that I found around the web, also in Russian and Chinese, but I just can not solve the problem.
With this controller I can list all the users record in db via postman get, so I think the configuration works fine, but I still have a problem to understand how to do authentication via rest
<?php
namespace app\api\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use yii\filters\auth\HttpBasicAuth;
use yii\helpers\ArrayHelper;
use yii\base\NotSupportedException;
use yii\web\Response;
use yii\filters\ContentNegotiator;
use app\api\modules\v1\models\User;
class UserController extends ActiveController
{
public $modelClass = 'app\api\modules\v1\models\User';
}