Authentication Via Api

I’ve been using Yii 1.x for my current applications and they all authenticate to a central User App via API. I’ve tried several ways yesterday & today to authenticate via API using Yii2.

So my question is, how do I make the UserIdentity show the user as logged in without using ActiveRecord?

When I try and manipulate the main login function below I get an error saying that I am not passing an Identity but an array. $response is the JSON response from the API User Application.




class SSOLogin extends Model implements IdentityInterface

{

...............

    public static function authenticate($username,$password)

    {

        $action = 'apiaccess/login';

        $data = array(

            'username' => $username,

            'password' => $password,

        );


        $response = static::apiCall($action,$data);


        if(!$response['authenticated'] && !$response['access']) {

            //return $response['message'];

            return false;

        }else{

            return Yii::$app->user->login($response, 3600 * 24 * 30);

        }

    }

...............

}



The problem is I can’t find a way to implement the old way of creating the UserIdentity.




UsersController

.........

    public function actionLogin()

    {

        if (Yii::app()->request->isPostRequest) {


            $this->_identity=new UserIdentity($_POST['email'],$_POST['password']);

            $this->_identity->authenticate();


            if($this->_identity->errorCode===UserIdentity::ERROR_NONE){


                $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days

                Yii::app()->user->login($this->_identity,$duration);

                $this->redirect(Yii::app()->homeUrl);


            }else{

                throw new CHttpException(400,$this->_identity->errorCode);

            }


        }else{

            throw new CHttpException(400,'Login error!');

        }


    }






UserIdentity

.............

    public function authenticate()

    {


        $action = 'apiaccess/login';

        $data = array(

            'username' => $this->username,

            'password' => $this->password,

        );


        $response = SSOApi::call($action,$data);


        if(!$response['authenticated'] && !$response['access'])

            $this->errorCode=$response['message'];

        else

        {

            $this->_id=$response['uid'];

            $this->_email=$response['email'];

            $this->errorCode=self::ERROR_NONE;

        }

        return !$this->errorCode;

    }



take a look at this

Thank you for the response, but that isn’t the fix. I found the solution to the change though. I won’t go into great detail on the entire fix as it’s more detailed and specific to my API design, but the new way to get identity to work is to create a model and put your data into that model (mind you the model has to implement the IdentityInterface. Snippet example of my fix is below.




        $model = new SSOLogin();

        $response = static::apiCall($action,$data);


        $model->username = $response['username'];

        $model->id = $response['id'];



That’s the clean way.

LoginForm is a model.

you need to just override login() method with your logic

You are missing the point of the OP. I asked how to implement the new user identity, linking me to the login modal doesn’t answer the issue, what I posted explains how to to implement the new way to get user identity to work.