yii2-user
yii2-user copied to clipboard
How to Authenticate Through Rest API with YII2User?
Hi,
I like the extension but i'm finding some difficulties to implement API authentication when using it (i already have a controller that works fine with the traditional User model but once i use YII2User i can't make it work).
Do you have any example of a behavior that i could insert into an API controller to authenticate a user through Rest Api?
Best Regards,
Diego.
Hey,
There isn't really anything special needed to do restful api authentication with this module. You can just do the basic yii2 stuff following the guide
If you're writing custom classes, are you updating the config properly?
Thanks for your fast reply Amnah,
Your extension is working well everywhere, is just an api controller which is not working.
Just in case, here's the code....
======================================================== namespace app\controllers; use yii\rest\ActiveController; use Yii;
use yii\helpers\ArrayHelper; use yii\filters\auth\HttpBasicAuth;
class FilmController extends ActiveController
{
public $modelClass = 'app\models\Film';
protected function verbs()
{
return [
'index' => ['GET', 'HEAD',],
'view' => ['GET', 'HEAD'],
'create' => ['POST'],
'update' => ['PUT', 'PATCH'],
'delete' => ['DELETE'],
];
}
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::class,
];
return $behaviors;
}
}
========================================================
And (if it helps), here's my Url Manager....
'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => false, 'showScriptName' => false, 'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'film'],
],
],
],
If you have any clue, it's more than welcome.
All the Best,
Diego.
Hmm, exactly what isn't working? What's happening