Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plz support save email faild on log in with Oath types #2942

Closed
wants to merge 1 commit into from

Conversation

oazya500
Copy link

could you plz support save email failed on log in with oath types and mack user log with the same email if it is already log in with other Oauth

also could you plz add support make username as email

thanks

my code on cloud for google that do this behave

var AppValues = require('./controllers/AppValues.js');

var _ = require('underscore');
var Buffer = require('buffer').Buffer;

var clientsIds = [AppValues.iOSGoogleserverClientsId,AppValues.AndroidGoogleserverClientsId];
var googleValidateEndpoint = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=';

Parse.Cloud.define('accessGoogleUser', function(req, res) {

Parse.Cloud.useMasterKey()

var data = req.params;
if (!(data && data.accessToken)) {
    res.error('Invalid request received. "accessToken" is required');
    return;
}

Parse.Promise.as().then(function() {
    return callTokenInfoEndPoint(data.accessToken);
}).then(function(httpResponse) {       
            //console.log("tokeninfo endpoint: " + httpResponse.text);

    var tokenInfoData = JSON.parse(httpResponse.text);
    // "Once you get these claims, you still need to check that the aud claim contains one of your app's client IDs."
    // from https://developers.google.com/identity/sign-in/ios/backend-auth
    if ( tokenInfoData && ( _.contains(clientsIds,tokenInfoData.aud,tokenInfoData.email,tokenInfoData.exp) )) {
        var userId = tokenInfoData.sub;
        return upsertGoogleUser(data.accessToken, userId,tokenInfoData.email,tokenInfoData.name,req.user,tokenInfoData.exp);
    } else {
        return Parse.Promise.error("Unable to parse Google data");
    }

}).then(function(user) {  
     // send back the session token in the response to be used with 'become/becomeInBackground' functions
    res.success(user.getSessionToken());

}, function(error) {   
            console.log('erorr333',error)

    if (error && error.code && error.error) {
        error = error.code + ' ' + error.error;
    }
    res.error(JSON.stringify(error));
});

});

var callTokenInfoEndPoint = function(accessToken) {
return Parse.Cloud.httpRequest({
url: googleValidateEndpoint + accessToken
});
};

var upsertGoogleUser = function(accessToken, userId,email,name,AnonymousUser,exp) {
var userquery = new Parse.Query(Parse.User);
userquery.equalTo('email', email);
return userquery.first({useMasterKey: true}).then(function(user) {
if (!user) {
return newGoogleUser(accessToken,email,name,userId,AnonymousUser,exp);
}
return user.fetch({useMasterKey: true}).then(function(user) {
var password = new Buffer(24);
_.times(24, function(i) {
password[i]= _.random(0, 255);
});
user.setPassword(password.toString('base64'));
user.set('googleId', {"id":userId})
if(user.get("nikName")=="U"){
user.set("nikName",name)
}
return SaveAndLogIn(accessToken,email,name,userId,user,exp,password.toString('base64'))

    })
});

};

var SaveAndLogIn = function(accessToken,email,name,userId,user,exp,password) {
return user.save(null,{useMasterKey: true}).then(function(user){
return Parse.User.logIn(user.getUsername(), password)
}).then(function(usere) {
return unlinkFromAnonymous(usere,userId,accessToken,exp);
});
};

var newGoogleUser = function(accessToken,email,name,userId,AnonymousUser,exp) {
var user;

if(AnonymousUser){
 return   AnonymousUser.fetch({useMasterKey: true}).then(function(user) {
      return upsetUser(accessToken,email,name,userId,user,exp);

    })

}else{
    user = new Parse.User();
}
return upsetUser(accessToken,email,name,userId,user,exp)    

};

var upsetUser = function(accessToken,email,name,userId,user,exp) {
var password = new Buffer(24);
_.times(24, function(i) {
password[i]= _.random(0, 255);
});
user.set("username", email);
user.set("password", password.toString('base64'));
user.set("nikName", name);
user.set("email", email);
user.set('googleId', {"id":userId})

return user.signUp().then(function(usere) {
return unlinkFromAnonymous(usere,userId,accessToken,exp);
});

};
var liknWithgoogle = function(user,userId,accessToken,exp) {

 return user._linkWith('MyGoogleOauth', {
                   authData: {"id":userId,"access_token":accessToken,expiration_date:exp}
                   }).then(function(user) {

                          return Parse.Promise.as(user);


                           },function(error){
                          console.log("liknWithgoogle",error)

                           })

};

var unlinkFromAnonymous = function(user,userId,accessToken,exp) {
if(user._isLinked('anonymous')){

                            return  user._unlinkFrom(AnonymousProvider,{}).then(function(usere){

                           return liknWithgoogle(usere,userId,accessToken,exp)

                            },function(error){
                                    console.log("unlinkFromAnonymous",error);

                            });
                       }else{
                           return liknWithgoogle(user,userId,accessToken,exp)
                       }

};
var AnonymousProvider = {
getAuthType: function () {
return 'anonymous';
},
restoreAuthentication: function () {
return true;
},
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants