M
M
monitor812019-06-01 15:16:35
API
monitor81, 2019-06-01 15:16:35

Why is there an error in Postman response when authorizing using YII2 token?

Good afternoon! Given the API on YII2, you will need to implement the frontend, but when you try to get responses in Posman, an error is displayed. For example, when logging in. Set {'login':'user15335551863','password':'pass'} and the corresponding url. The error is displayed:
5cf269f65a2d6028715333.png
In the User model:

public function login()
    {
        if($this->validate()) {

            $user = self::findOne(['login' => $this->login]);

            $user->token = Yii::$app->security->generateRandomString();
            $user->save();

            return [
                'token' => $user->token
            ];
        } else {
            return [
                'errors' => $this->errors
            ];
        }

In controller:
public function actionLogin()
    {
        $user = new User();
        $user->load(Yii::$app->request->post(),'');
        $user->scenario = 'login';
        return $user->login();
    }

In web.php:
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'POST /api/login' => 'user/login',
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => ['user'],
                ],

API deployed on OpenServer. Run a composer update. We created a database and registered it in db.php. Then they began to test in Postman and an error appears.
Tell me, what could be the reason for such an error?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question