解析使用 Auth0 认证的用户
Parse user authenticated using Auth0
您好,我正在尝试弄清楚如何一起使用 Parse.com 和 Auth0。
当我使用 Parse.com 设置登录时,currentUser 会自动设置,我可以使用该用户来保存特定于该用户的项目。但是当我使用 Auth0 实现身份验证时,Auth0 在您执行成功登录时响应用户信息(Auth0 令牌、fb 访问令牌等),当然没有创建 Parse 用户。那么需要经过哪些步骤才能使用Parse提供的'currentUser'对象,却使用Auth0提供的认证呢?那么我如何 create/save 解析新用户?
你试过了吗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));
}
});
}
您好,我正在尝试弄清楚如何一起使用 Parse.com 和 Auth0。
当我使用 Parse.com 设置登录时,currentUser 会自动设置,我可以使用该用户来保存特定于该用户的项目。但是当我使用 Auth0 实现身份验证时,Auth0 在您执行成功登录时响应用户信息(Auth0 令牌、fb 访问令牌等),当然没有创建 Parse 用户。那么需要经过哪些步骤才能使用Parse提供的'currentUser'对象,却使用Auth0提供的认证呢?那么我如何 create/save 解析新用户?
你试过了吗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));
}
});
}