Skip to content

Instantly share code, notes, and snippets.

@ardok
Created September 13, 2012 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardok/3710962 to your computer and use it in GitHub Desktop.
Save ardok/3710962 to your computer and use it in GitHub Desktop.
Second part for JS Tutorial

Objective

Login and Logout User

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Login With an Existing User

Assuming there is a user whose username is test and password test, let's log in with test user account.

    <body>
    ...

    <script type="text/javascript">
    	var user = new StackMob.User({ username: 'test', password: 'test' });
    	user.login(false, {
    		success: function(model) {
                  // redirect user to a new page
    		},
    		error: function(model, response) {
    		  console.log(response['error']);
    	  	}
      	});
      </script>
    </body>
</html>

On the new page (where user gets redirected), we can do various things with authentication such as:

  StackMob.isLoggedIn(); // this should be true since there is a logged in user

  StackMob.isUserLoggedIn('test'); // this should return true since we just logged 'test' in to your webapp

  StackMob.isUserLoggedIn('joshua'); // this should return false since we did not log in with user 'joshua'

  var user = new StackMob.User({ username: 'test' });
  user.isLoggedIn(); // this should return true since user 'test' is logged in

Now, let's log out our user test. Say your user test clicks on logout button.

  var loggedInUsername = StackMob.getLoggedInUser(); // this returns the username for the logged in user
  var user = new StackMob.User({ username: loggedInUsername });
  user.logout({
    success: function(model) {
      // redirect user to your log in page or some other page
      // model is always be undefined here
    }
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment