0

私はサービス Parse を使用して、ユーザー認証システムを作成しています。Parse は、現在のユーザーをログアウトする Logout 関数を提供しますが、その関数はコールバック オプションを提供しません。

私のジレンマは、非同期ログアウト関数が完了した後に関数を実行する必要があることです。Parse フレームワークを変更せずにそれを達成するにはどうすればよいでしょうか?

これが私のコードです:

function logOut() {

    if (getCurrentUsername()) {

    Parse.User.logOut();

    // Need this to wait till the logOut function completes
    loggedIn();

    } else {

    }

}

function loggedIn() {

 if (getCurrentUsername()) {
    //Hide the sign in form because it is not needed
    $('#sign_in_form').hide();

    //Get the username of the current user by calling the function getCurrentUsername()
    var currentUsername = getCurrentUsername();

    //Write a welcome message to the current user and give a log out option
    $('#signed_in_note').html('Welcome <a href="#">'+ currentUsername +'</a> | <a href="#" id="log_out">log out</a>')

    //Show the welcome message
    $('#signed_in_note').show();

 } else {

 //If no user is signed in, we show the login form and hide the welcome message
  $('#sign_in_form').show();
  $('#signed_in_note').hide();
 }
}
4

1 に答える 1

0

ユーザーがまだ認証されているかどうかを確認する簡単なタイマーを設定できます。

var currentUser;
var timerId;

function logOut() {
    currentUser = getCurrentUsername();
    if (currentUser) {
      Parse.User.logOut();
      timerId = setInterval(checkAuth, 200);
    } else {
      //show msg saying already logged out or log an error
    }

}

function checkAuth() {
    // check if user is logged out
    if(!currentUser.authenticated()) {
      abortTimer();
      loggedIn();    //now this will run only after user is logged out
    }
}

function abortTimer() { 
  clearInterval(timerId);
}

PS: ログアウト呼び出しが戻る前にユーザーがブラウザーを閉じる可能性があるため、100% のケースでこれに依存しないでください。

于 2013-01-19T10:08:32.443 に答える