grails (11) vaadin (11) meteor (6) java (4) elasticsearch (3) apple (2) centos (1) cloudbees (1) google analytics (1) gradle (1) heroku (1) javafx (1) javascript (1) jdbc (1) jug (1) logback (1) logging (1) mac os (1) management (1) mongodb (1) mongolab (1) mysql (1) twitter (1) ubuntu (1)

Sunday, July 21, 2013

Meteor: How to login with Email, GitHub, Twitter, Google and Facebook account and add credentials to an existing user account

When an user has already an account in the Meteor application, we don't want to create another user in MongoDB when he tries to login with other OAuth service. Rather we connect the accounts and add a new login service to the existing user's services collection.

This code doesn't work for Twitter, because Twitter does not return email in the user data. Therefore, there is no way how to to connect Twitter account by email. I propose to exclude Twitter from your application until it is solved by Meteor (requestPermissions).

Also Github can have an account without email. So when user tries to login with GitHub without email, a new account is created.

Just create a new file oauth.js in server folder and copy paste the code below.

Thanks to Gadi Cohen post.
TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.

4 comments:

  1. kinda think performing 4 queries (or one per service) could be optimized a bit..

    ReplyDelete
  2. If the user doesn't have an email associated with the service, or the email associated with the service does not match on their external service then this breaks?

    ReplyDelete
  3. Service Google does not have appId. It must be renamed to clientId.

    ReplyDelete
  4. There is small problem in your code. It won't work. You expect "user" in Accounts.onCreateuser to have loginTokens as well, however, loginTokens are generated after user object is returned from this function back to Meteor. Hence, following line will cause error

    existingUser.services.resume.loginTokens.push(
    user.services.resume.loginTokens[0]
    );


    You should comment these lines and it will work smoothly.

    ReplyDelete