Skip to content

Instantly share code, notes, and snippets.

@cprakashagr
Last active January 4, 2016 14:21
Show Gist options
  • Save cprakashagr/08cc9084ee92c2e378a0 to your computer and use it in GitHub Desktop.
Save cprakashagr/08cc9084ee92c2e378a0 to your computer and use it in GitHub Desktop.
The Meteor.java file can be used for social accounts login and signup. The file is made as an enhancement for the [Android-DDP] (https://github.com/delight-im/Android-DDP) repository. The users need to override the server side Accounts Handler which is shown in the file named, "socialAccountsLoginHandler.js". A working example is here: https://g…
/*
* modified by: cprakashagr
* date: Dec 01, 2015
* comments: registerWithGoogle method added for Google Plus Login in Android DDP Android Client
* Need to override the LoginHandler at calling the remote method login.
* For details, check the "socialAccountsLoginHandler.js" at server package.
*
* registerWithLinkedIn method added for Linked In Login in Android DDP Android Client
* Need to override the LoginHandler at calling the remote method login.
* For details, check the "socialAccountsLoginHandler.js" at server package.
*
* Repository: https://github.com/delight-im/Android-DDP
* Working Example: https://github.com/cprakashagr/Android-DDP
*/
/**
* Registers a new user with the Google oAuth API
*
* This method will automatically login as the new user on success
*
* Please note that this requires `accounts-base` package
*
* @param email the email to register with. Must be fetched from the Google oAuth Android API
* @param userId the unique google plus userId. Must be fetched from the Google oAuth Android API
* @param idToken the idToken from Google oAuth Android API
* @param oAuthToken the oAuthToken from Google oAuth Android API for server side validation
* @param listener the listener to call on success/error
*/
public void registerWithGoogle(final String email, final String userId, final String idToken, final String oAuthToken,final ResultListener listener) {
final boolean googleLoginPlugin = true;
Map<String, Object> accountData = new HashMap<String, Object>();
accountData.put("email", email);
accountData.put("userId", userId);
accountData.put("idToken", idToken);
accountData.put("oAuthToken", oAuthToken);
accountData.put("googleLoginPlugin", googleLoginPlugin);
call("login", new Object[] { accountData }, listener);
}
/**
* Registers a new user with the Linked In
*
* This method will automatically login as the new user on success
*
* Please note that this requires `accounts-base` package
*
* @param email the email to register with. Must be fetched from the LinkedIn Android API
* @param userId the unique LinkedIn id. Must be fetched from the LinkedIn Android API
* @param firstName the firstName from LinkedIn Android API
* @param lastName the lastName from LinkedIn Android API
* @param listener the listener to call on success/error
*
* @param more parameters could be added as per your requirements.
*/
public void registerWithLinkedIn(final String email, final String userId, final String firstName, final String lastName,final ResultListener listener) {
final boolean linkedInLoginPlugin = true;
Map<String, Object> accountData = new HashMap<String, Object>();
accountData.put("email", email);
accountData.put("userId", userId);
accountData.put("idToken", firstName);
accountData.put("oAuthToken", lastName);
accountData.put("googleLoginPlugin", linkedInLoginPlugin);
call("login", new Object[] { accountData }, listener);
}
/*
* author: cprakashagr
* date: 30 Nov 2015
* fileName: socialAccountsLoginHandler.js
* comments: This file is required to override the LoginHandler method at server for Google Plus Login from Android DDP lib.
* It makes sure that it will be called only at googleLoginPlugin == true or linkedInLoginPlugin == true;
* Make sure that you have the clientId and the clientSecret from the google developer console.
* The same clientId must be used in the Android side for the proper validation.
*/
Accounts.registerLoginHandler(function(req) {
var googleApiKey = {
clientId: "",
clientSecret: ""
};
if (req.googleLoginPlugin || req.linkedInLoginPlugin) {
var user = Meteor.users.findOne({
"emails.address": req.email,
});
var userId = null;
var serviceResponse = {};
var profileData = {};
// First frame the insertObject for google or linkedin
if (req.googleLoginPlugin) {
var res = Meteor.http.get("https://www.googleapis.com/oauth2/v3/tokeninfo", {
headers: {
"User-Agent": "Meteor/1.0"
},
params: {
id_token: req.idToken
}
});
if (res.error) {
throw res.error;
} else {
if (req.userId == res.data.sub && res.data.aud == googleApiKey.clientId) {
var googleResponse = _.pick(res.data, "email", "email_verified", "family_name", "gender", "given_name", "locale", "name", "picture", "profile", "sub");
googleResponse["accessToken"] = req.oAuthToken;
googleResponse["id"] = req.userId;
if (typeof(googleResponse["email"]) == "undefined") {
googleResponse["email"] = req.email;
}
}
serviceResponse = {google: googleResponse};
profileData = {
firstName: googleResponse.given_name,
lastName: googleResponse.family_name,
fullName: googleResponse.name
};
}
} else if (req.linkedInLoginPlugin) {
serviceResponse = {linkedIn: req};
profileData = {
firstName: req.firstName,
lastName: req.lastName,
fullName: req.firstName + ' ' + req.lastName
};
}
if (!user) {
// 2 cases: make the service doc. 1 google or 2 linkedin
var insertObject = {
createdAt: new Date(),
services: serviceResponse,
emails: [{
address: req.email,
verified: true
}],
profile: profileData
};
userId = Meteor.users.insert(insertObject);
} else {
userId = user._id;
// Fetch the services.google or services.linkedin
// and merge them.
var updateQuery = {};
if (req.googleLoginPlugin) {
updateQuery = {'services.google':serviceResponse.google};
} else if (req.linkedInLoginPlugin) {
updateQuery = {'services.linkedIn':serviceResponse.linkedIn};
}
Meteor.users.update({
_id: userId
}, {
$set: updateQuery
}
);
}
var stampedToken = Accounts._generateStampedLoginToken();
var stampedTokenHash = Accounts._hashStampedToken(stampedToken);
Meteor.users.update({
_id: userId
}, {
$push: {
"services.resume.loginTokens": stampedTokenHash
}
});
Meteor.users.update({
'emails.address': req.email
}, {
$set: {
'emails.$.verified': true,
'profile.firstName':profileData.firstName,
'profile.lastName':profileData.lastName,
'profile.fullName':profileData.fullName,
}
});
return {
token: stampedToken.token,
userId: userId
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment