Data-storage II

User authentication

{warm-up}

{why authenticate?}

Authentication

Limit users

Customize data

Customize functionality

Authentication Steps

Sign-up

Sign-in

Sign-out

Sign-up

Create a new Parse User

Set required properties

Sign-up your user

var user = new Parse.User()
user.set("username", "my name");
user.set("password", "my pass");
user.set("email", "email@example.com"); // optional
user.signUp(null, {
  success: function(user) {
    // Hooray! Let them use the app now.
  },
  error: function(user, error) {
    // Show the error message somewhere and let the user try again.
  }
});

Sign-in, Sign-out

Sign-in

Sign-out

Parse.User.logIn("myname", "mypass").then(
    function(user) {
        // Do stuff after successful login.
      },
     function(error) {
        // The login failed. Check error to see why.
      }
);
Parse.User.logOut().then(function() {
    // Do stuff
})

Check current user

User is stored in browser cookie

Check current user

Check it out here by signing in in and reloading the page

Parse.User.current()

{create a new Facebook app}

You'll need this info for parse!

1

2

3

Initialize Facebook SDK

// Initialize app
Parse.initialize('app-id', 'js-key')

// Function called once SDK is loaded
window.fbAsyncInit = function() {
    // Initialize Facebook utilities
    Parse.FacebookUtils.init({ // this line replaces FB.init({
      appId      : 'your-app-id', // Facebook App ID
      status     : true,  // check Facebook Login status
      cookie     : true,  // enable cookies to allow Parse to access the session
      xfbml      : true,  // initialize Facebook social plugins on the page
      version    : 'v2.3' // point to the latest Facebook Graph API version
    });

}

// Activate SDK
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

Log in Facebook

Parse.FacebookUtils.logIn(null, {
  success: function(user) {
    if (!user.existed()) {
      alert("User signed up and logged in through Facebook!");
    } else {
      alert("User logged in through Facebook!");
    }
  },
  error: function(user, error) {
    alert("User cancelled the Facebook login or did not fully authorize.");
  }
});

Updating our Music App

Add authentication

Save data with "user" field

Limit loaded data to user

Assignment

CRUD Challenge (due before class next week)

parse2

By Michael Freeman

parse2

  • 448
Loading comments...

More from Michael Freeman