This assumes that you have properly put in all the required scripts and have initialized StackMob.
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
}
});