Search code examples
javascripthtmltwitter-bootstrapparse-platform

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


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.

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

 $(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:

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

Solution

  • 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";
        });
    });