简体   繁体   中英

how to logout from rest api in yii2

I m calling logout api in yii2 framework using logout action

url : http://localhost/mobile/public/api/v1/logout

Code:

public function actionLogout()
{
    $user_t = Yii::$app->user->logout();
    return $this->apiItem(array(),'Logout Successfully');
}

but after calling logout api

when after this i calling view profile api it returns user data

public function actionViewprofile()
{
    $user = Yii::$app->user->identity;

    $profile_fetch = [
        'firstname'       => $user['member_fname'],
        'lastname'        => $user['member_lname'],
        'gender'          => $user['member_gender'],
        'dateofbirth'     => $user['member_dob']
    ];
    return $this->apiItem($profile_fetch);
}

where apitem is a function for json parameter format

 /**
 * Api Item response
 */
public function apiItem($data, $message = false,$flag = false )
{
    Yii::$app->response->statusCode = 200;
    return [
        'statusCode' => 200,
        'message' => $message ? $message : 'Data retrieval successful',
        'data' => $data,
        'flag' => $flag
    ];
}

Clear the token from DB and clear the user session

 $userID = Yii::$app->session->get('userID');

 $userModel = User::find()->where(['id'=>$userID])->one();
   if(!empty($userModel))
   {
    $userModel->token=NULL;
    $userModel->save(false);
   }
   Yii::app()->user->logout(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM