I can do a login with different models: User and Client, the REST service works well and it points to the corresponding tables to generate the access token. In each ActiveController I set the right model class.
I can also consume the different REST services from my app from the different controllers but I have an issue when I try to run a logout: whatever ActiveController I set the logout action, Yii2 will call only the user table to check the access token what raises the error if I am for instance with the Client “session”.
The way I have implemented the logout on user and client active controller is just like the following:
public function actionLogout(): array
{
$transaction = Yii::$app->db->beginTransaction();
$model = $this->findModel($this->request->post('id'));
$model->access_token = null;
$model->status = 0;
if ($model->save()) {
$transaction->commit();
Yii::$app->user->logout();
return ['success' => true];
}
$transaction->rollBack();
$message = '';
foreach ($model->errors as $key => $error) {
$message .= "$error[0]\n";
}
throw new BadRequestHttpException($message, 400);
}
And the findModel methods on each controller are:
// UserController
protected function findModel($id)
{
if (($model =User::findOne(['id' => $id])) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('yii', 'The requested page does not exist.'));
}
// ClientController
protected function findModel($id)
{
if (($model = Client::findOne(['id' => $id])) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('yii', 'The requested page does not exist.'));
}
My question is why in the logout for Client it calls the User model if I’m calling in the findModel the Client model?