Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor Accounts via external services dont set user.username

Tags:

meteor

I'm a complete newbie, and I've been fiddling with the Meteor 1.0 sample todo list app to connect google oauth to it.

When I do so the page no longer renders properly because {{username}} is not set at all.

https://docs.meteor.com/#/full/meteor_users says "username: a unique String identifying the user." but the oauth stuff doesn't create one for you.

Connect service to existing meteor account talks about linking an already existing account to another service, but in this case I just want to use the external service.

https://stackoverflow.com/questions/25182903/meteor-facebook-registration uses onCreateUser() to manually set

user.username = user.services.facebook.name

but this isn't portable across services nor to guarantee uniqueness.

https://github.com/aldeed/meteor-collection2 defines the User schema so that username is mandatory.

When I dump the users table (some fields removed) the google account doesn't have a username, and there is no field that can really take on that value automatically as there could be a clash. Email could be used but I'd rather the username wasn't the email address. Do I just force the user to specify a desired username?

meteor:PRIMARY> db.users.find()
{
    "_id" : "YNWt2cATMsKFG7od6",
    "createdAt" : ISODate("2014-11-05T11:08:00.406Z"),
    "services" : {
        "password" : {
        },
    },
    "username" : “a_user”
}
{
    "_id" : "CyQsJqcez3kWTRHyQ",
    "createdAt" : ISODate("2014-11-05T12:09:40.139Z"),
    "profile" : {
        "name" : “Alice User”
    },
    "services" : {
        "google" : {
            "email" : “[email protected]",
            "family_name" : “User”,
            "gender" : “female",
            "given_name" : "Alice”,
            "id" : "1115",
            "name" : “Alice User,
        }
    }
}

What is the correct way of handling this?

like image 752
Bae Avatar asked Nov 07 '14 10:11

Bae


Video Answer


2 Answers

This is how I did it myself with facebook and google

Accounts.onCreateUser(function (options, user) {
    if (options && options.profile) {
        user.profile = options.profile;

    }

    if (user.services) {

        var service = _.pairs(user.services)[0];

        var serviceName = service[0];
        var serviceData = service[1];

        console.log("serviceName", serviceName)

        if (serviceName == "facebook") {
            user.emails = [
                {"address": serviceData.email, "verified": true}
            ];
            user.profile = {"first_name": serviceData.first_name, "last_name": serviceData.last_name, "avatar": getFbPicture(serviceData.id)};
        }
        else if (serviceName == "google") {
            user.emails = [
                {"address": serviceData.email, "verified": true}
            ];
            user.profile = {"first_name": serviceData.given_name, "last_name": serviceData.family_name, "avatar": getGooglePicture(serviceData.id)};
        }

    }
    console.log("user created :", user)
    return user;
});

I do not use username but I use email so that I'm sure that it will be unique. After that I could allow the user to set his username or display name like Stackoverflow or other services do.

However you could use the email as username and again let the user change it later.

like image 70
user3636214 Avatar answered Oct 28 '22 12:10

user3636214


In my application, I am using this to handle the same problem.

username = user.services.facebook.name
user.username=generateUsername(username)

generateUsername = function(username) {
  var count;
  username = username.toLowerCase().trim().replace(" ", "");
  count = Meteor.users.find({"profile.un": username}).count();
  if (count === 0) {
    return username;
  } 
  else {
    return username + (count + 1)
}

This is will create a unique username. After successful signup you can allow the users to change the username and check your db for its existence.

like image 29
Rajanand02 Avatar answered Oct 28 '22 11:10

Rajanand02