how to logout from rest api in yii2












0















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
];
}









share|improve this question























  • do you have the authentication enabled for the action you are accessing after logout?

    – Muhammad Omer Aslam
    Jan 19 at 9:25













  • Yes how to clear auth token @MuhammadOmerAslam

    – Pritamkumar
    Jan 19 at 9:45













  • I’d fetch token by client application and pass it to the server with each request (there’s no session concept in REST) as far as i know, or maybe i misunderstood what you are asking here

    – Muhammad Omer Aslam
    Jan 19 at 12:03











  • I pass auth token in header every time but in logout api i want to remove token from yii2 session

    – Pritamkumar
    Jan 19 at 12:15











  • @Pritamkumar Please post your configuration main.php code

    – Sudharshan Nair
    Jan 19 at 12:56
















0















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
];
}









share|improve this question























  • do you have the authentication enabled for the action you are accessing after logout?

    – Muhammad Omer Aslam
    Jan 19 at 9:25













  • Yes how to clear auth token @MuhammadOmerAslam

    – Pritamkumar
    Jan 19 at 9:45













  • I’d fetch token by client application and pass it to the server with each request (there’s no session concept in REST) as far as i know, or maybe i misunderstood what you are asking here

    – Muhammad Omer Aslam
    Jan 19 at 12:03











  • I pass auth token in header every time but in logout api i want to remove token from yii2 session

    – Pritamkumar
    Jan 19 at 12:15











  • @Pritamkumar Please post your configuration main.php code

    – Sudharshan Nair
    Jan 19 at 12:56














0












0








0








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
];
}









share|improve this question














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
];
}






php json rest yii2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 19 at 9:07









PritamkumarPritamkumar

647823




647823













  • do you have the authentication enabled for the action you are accessing after logout?

    – Muhammad Omer Aslam
    Jan 19 at 9:25













  • Yes how to clear auth token @MuhammadOmerAslam

    – Pritamkumar
    Jan 19 at 9:45













  • I’d fetch token by client application and pass it to the server with each request (there’s no session concept in REST) as far as i know, or maybe i misunderstood what you are asking here

    – Muhammad Omer Aslam
    Jan 19 at 12:03











  • I pass auth token in header every time but in logout api i want to remove token from yii2 session

    – Pritamkumar
    Jan 19 at 12:15











  • @Pritamkumar Please post your configuration main.php code

    – Sudharshan Nair
    Jan 19 at 12:56



















  • do you have the authentication enabled for the action you are accessing after logout?

    – Muhammad Omer Aslam
    Jan 19 at 9:25













  • Yes how to clear auth token @MuhammadOmerAslam

    – Pritamkumar
    Jan 19 at 9:45













  • I’d fetch token by client application and pass it to the server with each request (there’s no session concept in REST) as far as i know, or maybe i misunderstood what you are asking here

    – Muhammad Omer Aslam
    Jan 19 at 12:03











  • I pass auth token in header every time but in logout api i want to remove token from yii2 session

    – Pritamkumar
    Jan 19 at 12:15











  • @Pritamkumar Please post your configuration main.php code

    – Sudharshan Nair
    Jan 19 at 12:56

















do you have the authentication enabled for the action you are accessing after logout?

– Muhammad Omer Aslam
Jan 19 at 9:25







do you have the authentication enabled for the action you are accessing after logout?

– Muhammad Omer Aslam
Jan 19 at 9:25















Yes how to clear auth token @MuhammadOmerAslam

– Pritamkumar
Jan 19 at 9:45







Yes how to clear auth token @MuhammadOmerAslam

– Pritamkumar
Jan 19 at 9:45















I’d fetch token by client application and pass it to the server with each request (there’s no session concept in REST) as far as i know, or maybe i misunderstood what you are asking here

– Muhammad Omer Aslam
Jan 19 at 12:03





I’d fetch token by client application and pass it to the server with each request (there’s no session concept in REST) as far as i know, or maybe i misunderstood what you are asking here

– Muhammad Omer Aslam
Jan 19 at 12:03













I pass auth token in header every time but in logout api i want to remove token from yii2 session

– Pritamkumar
Jan 19 at 12:15





I pass auth token in header every time but in logout api i want to remove token from yii2 session

– Pritamkumar
Jan 19 at 12:15













@Pritamkumar Please post your configuration main.php code

– Sudharshan Nair
Jan 19 at 12:56





@Pritamkumar Please post your configuration main.php code

– Sudharshan Nair
Jan 19 at 12:56












1 Answer
1






active

oldest

votes


















1














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);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265567%2fhow-to-logout-from-rest-api-in-yii2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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);





    share|improve this answer




























      1














      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);





      share|improve this answer


























        1












        1








        1







        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);





        share|improve this answer













        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);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 19 at 12:51









        Sudharshan NairSudharshan Nair

        776115




        776115






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265567%2fhow-to-logout-from-rest-api-in-yii2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown