Dominus77 / yii2-advanced-start

Yii2 Start Project Advanced Template
https://dominus77.github.io/yii2-advanced-start/
MIT License
23 stars 12 forks source link

User login serivce in RESTful #19

Closed polinwei closed 6 years ago

polinwei commented 6 years ago

Hi Sir:

I plan to write a user login service in RESTful . The procedure is after user login , then system generate a new auth_key and return to user. My code is below, but not work . Could you help me to point where is wrong? thanks!!

  1. modules\users\BootstrapApi
    // Rules for RESTful
    [
    'class' => 'yii\rest\UrlRule',
    'controller' => [
        'users/default'
    ],
    'extraPatterns' => [
        'POST user/login' => 'users/default/login',
    ]
    ],
  2. modules\users\controllers\api\DefaultController

    class DefaultController extends ActiveController
    {
    public $modelClass = 'modules\users\models\api\User';
    
    public function actionLogin(){
        $model = new LoginForm();
        $model->email = $_POST['email'];
        $model->password = $_POST['password'];
    
        if( $model->login()){
            $user = User::findByUsernameEmail($model->email);
            $user->generateAuthKey();
            $user->save();            
            return $user->getAuthKey();
        } else {
            $model->validate();
            return $model;
        }
    
    }
  3. Url http://mysite.com/api/user/login?email=user@gmail.com&password=123
Dominus77 commented 6 years ago

Hi! Well, for starters, you pass parameters by the method of Get and try to get from POST

  1. Url http://mysite.com/api/user/login?email=user@gmail.com&password=123
$model->email = $_POST['email'];
$model->password = $_POST['password'];

My version:

  1. modules\users\BootstrapApi.php
    public function bootstrap($app)
    {
    $app->getUrlManager()->addRules(
        [
                // Rules for RESTful
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => [
                        'users/default',
                    ],
                    'pluralize' => false,
                ],
                'GET user/login' => 'users/default/login',
                // @see http://www.yiiframework.com/doc-2.0/guide-rest-routing.html
                //'PUT,PATCH user/<id>' => 'users/default/update',
                //'DELETE user/<id>' => 'users/default/delete',
                'GET,HEAD user/<id>' => 'users/default/view',
                //'POST user' => 'users/default/create',
                'GET,HEAD users' => 'users/default/index',
                //'user/<id>' => 'users/default/options',
                //'users' => 'users/default/options',
        ]
    );
    parent::bootstrap($app);
    }
  2. modules\users\controllers\api\DefaultController
    //...
    /**
    * @return LoginForm
    */
    public function actionLogin()
    {
    $model = new LoginForm();
    if($model->load(Yii::$app->request->get(), '') && $model->login()) {
        $modelClass = $this->modelClass;
        $user = $modelClass::findOne(Yii::$app->user->identity->getId());
        $user->generateAuthKey();
        if($user->save())
            return 'auth_key: '. $user->auth_key;
    }
    $model->validate();
    return $model;
    }
    //...

    We check: http://mysite.com/api/users/login?email=user@gmail.com&password=123 return new auth key: "t19TwQouZWLtNW232tqd-J3L7iHz6K6D"

Dominus77 commented 6 years ago

See how the AngularJS and Yii2 bunch works: http://blog.neattutorials.com/angularjs-and-yii2-part-2-authentication/

polinwei commented 6 years ago

It works now . I should to study more. thanks

polinwei commented 6 years ago

I tried the rule also can work as below.

        $app->getUrlManager()->addRules(
            [
                // Rules for RESTful
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => [
                        'users/default'
                    ],
                    'pluralize' => false,
                    'extraPatterns' => [
                        'GET login' => 'login',
                    ],
                ],
                // @see http://www.yiiframework.com/doc-2.0/guide-rest-routing.html
                //'PUT,PATCH user/<id>' => 'users/default/update',
                //'DELETE user/<id>' => 'users/default/delete',
                'GET,HEAD user/<id>' => 'users/default/view',
                //'POST user' => 'users/default/create',
                'GET,HEAD users' => 'users/default/index',
                //'user/<id>' => 'users/default/options',
                //'users' => 'users/default/options',
            ]
        );
Dominus77 commented 6 years ago

If it works, then it can) http://www.yiiframework.com/doc-2.0/guide-rest-routing.html

Dominus77 commented 6 years ago

By the way for testing RESTful I'm using a plugin for the Mozilla Firefox browser REST Easy