1

Hi I'm trying to figure out how to use Parse.com and Auth0 together.

When I setup login with Parse.com the currentUser is automatically set and I can use that user to save items specific for that user. But when I implement authentication using Auth0, Auth0 responds with the user info (Auth0 token, fb access token etc) when you perform a successful login, and of course no Parse user was created. So which steps need to be taken to make use of the 'currentUser' object provided by Parse, but use the authentication provided by Auth0? So how do I create/save the new user on parse?

2
  • Why do you need both? Can't you just use Parse? Commented Dec 28, 2015 at 11:37
  • Auth0 comes with more auth options like touchID etc. Commented Dec 28, 2015 at 11:56

1 Answer 1

0

Have you tried this rule for Parse?

function(user, context, callback) {
  // run this only for the Parse application
  // if (context.clientID !== 'PARSE CLIENT ID IN AUTH0') return callback(null, user, context);

  var PARSE_APP_ID = 'PLACE HERE YOUR PARSE APP ID';
  var PARSE_API_KEY = 'PLACE HERE YOUR PARSE REST API KEY';
  var PARSE_USER_PASSWORD = 'PARSE_USER_MASTER_KEY'; // you can use this to generate one http://www.random.org/strings/

  var username = user.email || user.name || user.user_id; // this is the Auth0 user prop that will be mapped to the username in the db

  request.get({
    url: 'https://api.parse.com/1/login',
    qs: {
      username: username,
      password: PARSE_USER_PASSWORD
    },
    headers: {
      'X-Parse-Application-Id': PARSE_APP_ID,
      'X-Parse-REST-API-Key': PARSE_API_KEY
    }
  },
  function (err, response, body) {
    if (err) return callback(err);

    // user was found, add sessionToken to user profile
    if (response.statusCode === 200) {
      user.parse_session_token = JSON.parse(body).sessionToken;
      return callback(null, user, context);
    }

    // Not found. Likely the user doesn't exist, we provision one
    if(response.statusCode === 404) {
      request.post({
        url: 'https://api.parse.com/1/users',
        json: {
          username: username,
          password: PARSE_USER_PASSWORD
        },
        headers: {
          'X-Parse-Application-Id': PARSE_APP_ID,
          'X-Parse-REST-API-Key': PARSE_API_KEY,
          'Content-Type': 'application/json'
        }
      },
      function (err, response, body) {
        if (err) return callback(err);

        // user created, add sessionToken to user profile
        if (response.statusCode === 201) {
          user.parse_session_token = body.sessionToken;
          return callback(null, user, context);
        }
        return callback(new Error('The user provisioning returned an unkonwn error. Body: ' + JSON.stringify(body)));
      });
    }
    else
    {
      return callback(new Error('The login returned an unkonwn error. Status: ' + response.statusCode + 'Body: ' + body));
    }
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Has this actually worked for anyone? I am having problems making it work with Parse running on AWS. Making individual call get/post works making post on get failure does not. See here stackoverflow.com/questions/40875328/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.