如何创建注销脚本 - Parse (JavaScript)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29952249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:23:31  来源:igfitidea点击:

How to create log-out script - Parse (JavaScript)

javascripthtmltwitter-bootstrapparse-platform

提问by hero8110

I'm having a problem with logging out on my website. I have looked at the documentation on the parse website but it does not really provide much detail on how to log out the user, also does not help when I am not proficient with JavaScript.

我在登出我的网站时遇到问题。我查看了 parse 网站上的文档,但它并没有真正提供有关如何注销用户的详细信息,当我不精通 JavaScript 时也无济于事。

When I click my log out button, it just refreshes the page and nothing more but I would like to take it back to the sign in screen.

当我单击注销按钮时,它只会刷新页面,仅此而已,但我想将其带回登录屏幕。

My current script file that I have written is shown below (for obvious reasons I have removed my parse unique ids):

我编写的当前脚本文件如下所示(出于显而易见的原因,我已删除了我的解析唯一 ID):

 $(function() {

    Parse.$ = jQuery;
Parse.initialize("MY CODE HERE", "MY CODE HERE");

    $('.form-logout').on('submit', function(e) {

    // Prevent Default Submit Event
    e.preventDefault();

    //logout current user
    var currentUser = Parse.User.current();
        if (currentUser) {
            Parse.User.logout();
            window.location="Sign_In.html";
        } else {
            window.location="Sign_In.html";
        }

    });

});

The section where I create the button is located here in my html file:

我创建按钮的部分位于我的 html 文件中:

<form class="form-logout" role="form">
   <input type="submit" value="Logout" id="logout" class="btn btn-primary btn-lg">
</form>

回答by dekkard

Add

添加

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://www.parsecdn.com/js/parse-1.4.2.min.js"></script>

to your page. Then use this script:

到您的页面。然后使用这个脚本:

jQuery(document).ready(function() {

    Parse.$ = jQuery;
    Parse.initialize("...", "...");

    $('.form-logout').on('submit', function (e) {
        // Prevent Default Submit Event
        e.preventDefault();

        console.log("Performing submit");

        //logout current user
        if ( Parse.User.current() ) {
            Parse.User.logOut();

            // check if really logged out
            if (Parse.User.current())
                console.log("Failed to log out!");
        }

        // do redirect
        //window.location.replace("Sign_In.html");
        // or
        window.location.href = "/Sign_In.html";
    });
});

回答by user305883

You can self-execute a custom callback function on the logout() object.

您可以在 logout() 对象上自行执行自定义回调函数。

That is you first wrap your logout()function within an anonymous function, a syntax like:

也就是说,您首先将logout()函数包装在匿名函数中,语法如下:

function(){logout()}

You self execute it:

你自己执行它:

(function(){
logout()
})()

and you pass your callback as parameter!

并且您将回调作为参数传递!

(function(aFunctionIsToBePassedHere){
logout()
})(myCallbackFunction())

All of this become:

所有这一切都变成了:

  function logout() {
    (function(myCallbackGoesHereAsVariable) {
      Parse.User.logOut();
    })(myFunctionToShowTheLoginScreen())

  }

and then you bind the logout()function to your form.

然后将logout()函数绑定到表单。

回答by gagansaini

create a function

创建一个函数

const handleLogout = () => {
  window.localStorage.clear();
  window.location.reload(true);
  window.location.replace('/');
};

then call it in this way

然后这样调用

<Button className="button" onClick={() => handleLogout()}>
  <i className="fas fa-power-off"></i>
</Button>