I am fairly new to Yii2 Rest API
I am currently using postman to test the Url of my Rest API
everything works fine until I recently added a Basic Authentication to my Code.
it now shows this message on Postman even though I added my auth key token to authorization
[
{
"field": "username",
"message": "Username cannot be blank."
},
{
"field": "auth_key",
"message": "Auth Key cannot be blank."
},
{
"field": "password_hash",
"message": "Password Hash cannot be blank."
},
{
"field": "email",
"message": "Email cannot be blank."
}
]
here is my controller under modules
<?php
namespace appmodulesv1controllers;
use yiirestActiveController;
use yiiwebController;
/**
* ReadingSchedule controller for the `v1` module
*/
class ReadingScheduleController extends ActiveController
{
public $modelClass="appmodelsReadingSchedule";
public function behaviors() //override
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => yiifiltersauthCompositeAuth::class,
'authMethods' => [
yiifiltersauthHttpBearerAuth::class,
yiifiltersauthHttpBasicAuth::class,
[
'class' => yiifiltersauthCompositeAuth::class,
]
]
];
$behaviors['authenticator'] = [
'class' => yiifiltersauthHttpBasicAuth::class,
'auth' => [$this, 'auth']
];
return $behaviors;
}
public function auth($username, $password)
{
return appmodelsUser::findOne(['login' => $username, 'password' => $password]);
}
}
Here is mu User Module as it stated that I need to implement identity Interface
<?php
namespace appmodels;
use Yii;
use yiiwebIdentityInterface;
/**
* This is the model class for table "user".
*
* @property int $id
* @property string $username
* @property string $auth_key
* @property string|null $verification_token
* @property string $password_hash
* @property string|null $password_reset_token
* @property string $email
* @property int $status
* @property string $created_at
* @property string $updated_at
*/
class User extends yiidbActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_INACTIVE = 9;
const STATUS_ACTIVE = 10;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'auth_key' => 'Auth Key',
'verification_token' => 'Verification Token',
'password_hash' => 'Password Hash',
'password_reset_token' => 'Password Reset Token',
'email' => 'Email',
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['auth_key' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key; // Updated case for property name
}
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey; // Updated case for property name
}
}