Best Practice? For Sign-up + Insert doc to additional collection

Background:

  • I collect information about a user’s organization during my signup process.
  • I want this organization information to be inserted as a document into a collection of “organizations”.
  • The schema for the organization includes the the user’s id
  • I take the organization data from the forms and store it in a session variable-- this all works fine.

I’m wondering where the best place/time to insert this organization document as I need the user’s Id to have been generated? I suppose I can just use a method call but where should I call the method from?

On the server, I’m currently using accounts.OnCreateUser with if()s for registering with google or linkedin:

Accounts.onCreateUser(function(options, user) {
        var attachData, email, picture, profileImageUrl,
            profilePicture, url, service,
            allEmails, firstEmail; profileImageUrl = undefined;

        user.profile = user.profile || {};

//If the google service exists
if ((service = user.services) !== undefined ? service.google : undefined) {
    user.emails = [
        {
            address: user.services.google.email,
            verified: true
    }
    ];

    user.profile.firstName = user.services.google.given_name;
    user.profile.lastName = user.services.google.family_name;
    user.profile.avatar = user.services.google.picture;
}






    //If the linkedin service exists
    if ((service = user.services) !== undefined ? service.linkedin : undefined) {


        var accessToken = user.services.linkedin.accessToken;
// http://developer.linkedin.com/documents/profile-fields
        var basicProfileFields = ['first-name', 'last-name', 'maiden-name', 'formatted-name', 'phonetic-first-name',
                'phonetic-last-name', 'formatted-phonetic-name', 'headline', 'location',
                'industry', 'num-connections', 'num-connections-capped', 'summary',
                'specialties', 'positions', 'picture-url', 'picture-urls::(original)', 'site-standard-profile-request'],
            emailFields = ['email-address'],
            fullProfileFields = ['last-modified-timestamp', 'proposal-comments', 'associations', 'interests', 'publications',
                'patents', 'languages', 'skills', 'certifications', 'educations',
                'courses', 'volunteer', 'three-current-positions', 'three-past-positions', 'num-recommenders',
                'recommendations-received', 'following', 'job-bookmarks', 'suggestions', 'date-of-birth'],
            contactInfoFields = ['phone-numbers', 'bound-account-types', 'im-accounts', 'main-address', 'twitter-accounts', 'primary-twitter-account'];

        var requestUrl = 'https://api.linkedin.com/v1/people/~:(' + _.union(basicProfileFields, emailFields, fullProfileFields, contactInfoFields).join(',') + ')';

        var response = HTTP.get(requestUrl, {
            params: {
                oauth2_access_token: accessToken,
                format: 'json'
            }
        });


        user.emails = [ { address: response.data.emailAddress, verified: true } ];

        user.profile.firstName = response.data.firstName;
        user.profile.lastName = response.data.lastName;
        user.profile.avatar = response.data.pictureUrl;
        user.profile.bio = response.data.summary;

    }



    return user;

});

help!!! please…

20chars

I guess this is not possible when using accounts-ui

I’d check out the Meteor Collection Hooks package, this could be helpful.

I’ve gotta run out but wanted to respond to your post quick, so I might be missing some info or there might be a better way about doing this.

It’s a bit hacky, but perhaps you could take the additional information from the form and temporarily store it in the user doc or user profile (using Accounts.onCreateUser), then immediately upon the user being created you could have Meteor.users.after.insert run and take this information from the profile then store it into the “organizations” collection, then remove it from the profile.

Basically once you store the info in the profile you’d have something like this on the server:

Meteor.users.after.insert(function(userId, doc) {
	
	Organizations.insert ({
		userId: doc._id, // doc is Meteor.user
		name: doc.profile.organization.name,
		number: doc.profile.organization.number,
		address: doc.profile.organization.address,
		// etc.
	});

	Meteor.users.update(doc._id, {
	    $unset: {
	       'profile.organization.name': "",
	       'profile.organization.number': "",
	       'profile.organization.address': "",
	   }
	});

});

Again, I’m pretty rushed when writing this so maybe there’s a better way about doing this, but this was just an idea that came to mind.

I ended up tacking the session variable onto “info.profile” inside of the preSignupHook. Then I insert the document in the postSignUpHook.

This seems to work okay for now!

Thanks for taking the time to respond.

How did you make preSignupHook work with services google?