0

我正在使用 Parse 服务编写用户身份验证系统。Parse 提供了注销当前用户的注销功能,但该功能不提供回调选项。

我的困境是我需要在异步注销功能完成后运行一个功能。在不修改 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 回答