Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Authenticate Through Rest API with YII2User? #217

Open
diegoid opened this issue Jun 13, 2022 · 3 comments
Open

How to Authenticate Through Rest API with YII2User? #217

diegoid opened this issue Jun 13, 2022 · 3 comments

Comments

@diegoid
Copy link

diegoid commented Jun 13, 2022

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.

@amnah
Copy link
Owner

amnah commented Jun 13, 2022

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?

@diegoid
Copy link
Author

diegoid commented Jun 14, 2022

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.

@amnah
Copy link
Owner

amnah commented Jun 14, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants