1

I am creating an api using Yii2 framework . But it throws error during its execution.

BankDetailsController.php

<?php 

namespace backend\controllers;
use yii\rest\ActiveController;
use common\components\CustomAuth;


class BankDetailsController extends ActiveController
{
    public $modelClass = 'common\models\BankDetails';

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CustomAuth::className(),            
        ];
        return $behaviors;
    }
}

Admin.php

public function getUserIdFromToken($token){
        $sql = "select AdminUserID from utpl_opt_usertoken where auth_token =".$token;
        $userIdFromToken = Yii::$app->db->createCommand($sql)->queryAll();
        return $userIdFromToken;
    }

BankDetails.php

<?php

namespace common\models;
use yii\db\ActiveRecord;
use Yii;

class BankDetails extends ActiveRecord
{
    public static function tableName()
    {
        return 'th_bank_details';
    }

    public function fields()
    {
        return [ 'bank_name', 'branch_name' , 'ifsc_code' , 'account_type' , 'account_number' , 'name_on_account' , 'relationship' , 'user_id' , 'is_active' , 'listing_mode' , 'listing_user' , 'created_by' ];
    }

    public function rules()
    {
        return [
            [
                ['bank_name' , 'branch_name', 'account_type' , 'user_id' , 'is_active' , 'listing_mode' , 'listing_user', 'created_by' ],  'safe'
            ],
            [
                [ 'ifsc_code' , 'account_number' , 'name_on_account' , 'relationship'  ],  'required'
            ],
        ];
    }

    public function save($runValidation = true, $attributeNames = null)
    {   
        $id = Yii::$app->user->getId();
        $this->created_by = $id;
        return $this->insert($runValidation, $attributeNames);
    }
}

customAuth.php

<?php 

namespace common\components;
use yii\filters\auth\AuthMethod;
use common\models\Admin;

class CustomAuth extends AuthMethod
{
    public function authenticate($user, $request, $response)
    {
        //var_dump($request->getHeaders());
        $token = $request->getHeaders();
        $userId = Admin::getUserIdFromToken($token);
        $identity = Admin::findIdentity($userId);
        $user->login($identity);
        return $identity;
        return true;
    }
}

On execution this throws error as

{
  "name": "PHP Recoverable Error",
  "message": "Object of class yii\\web\\HeaderCollection could not be converted to string",
  "code": 4096,
  "type": "yii\\base\\ErrorException",
  "file": "C:\\xampp\\htdocs\\app\\common\\models\\Admin.php",
  "line": 98,
}

Any help would be great.... Thank you

  • Obviously `getUserIdFromToken($token)` takes `$token` argument which should be string and you pass there `$request->getHeaders();` which is object. – Bizley May 09 '17 at 06:10
  • 1
    `$token = $request->headers->get('YOUR_TOKEN_KEY');` – leninhasda May 09 '17 at 06:23
  • @leninhasda thanks for your response. I have tried as per your answer now i am getting error as like this "message": "Argument 1 passed to yii\\web\\User::login() must implement interface yii\\web\\IdentityInterface, null given, called in C:\\xampp\\htdocs\\app\\common\\components\\CustomAuth.php on line 17 and defined", –  May 09 '17 at 06:38
  • @leninhasda can you please help me. –  May 09 '17 at 07:53
  • @Bizley please check this error ... Argument 1 passed to yii\\web\\User::login() must implement interface yii\\web\\IdentityInterface, null given, called in C:\\xampp\\htdocs\\app\\common\\components\\CustomAuth.php on line 14 and defined –  May 09 '17 at 07:54
  • @TanmoySarkar what it says - result of `Admin::findIdentity($userId);` does not implement `IdentityInterface`. It's either not configured properly or your code is broken. – Bizley May 09 '17 at 07:59
  • @Bizley here is my identity function.... public static function findIdentity($id) { return static::findOne(['id' => $id]); } ... what to do here .. is it wrong ? –  May 09 '17 at 08:05
  • @TanmoySarkar this ActiveRecord that is fetched with findOne must implement the interface previously mentioned. – Bizley May 09 '17 at 08:30
  • @Bizley how to process this array which is returned by var_dump($userId) .... array(1) { [0]=> array(1) { ["AdminUserID"]=> string(1) "5" } } ..... how can i get this so that i can pass this to findIdentityId($id) . –  May 09 '17 at 09:00
  • Please avoid discussion in comments. If you struggle with another problem ask a separate question, remember to follow [the rules of asking a question](http://stackoverflow.com/help/how-to-ask). – Bizley May 09 '17 at 09:06

0 Answers0