yii2-user icon indicating copy to clipboard operation
yii2-user copied to clipboard

How to Authenticate Through Rest API with YII2User?

Open diegoid opened this issue 2 years ago • 3 comments

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.

diegoid avatar Jun 13 '22 21:06 diegoid

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?

amnah avatar Jun 13 '22 23:06 amnah

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.

diegoid avatar Jun 14 '22 00:06 diegoid

Hmm, exactly what isn't working? What's happening

amnah avatar Jun 14 '22 01:06 amnah