Meteorite accounts through external services do not set user.username

I'm a complete newbie and I was messing around with a Meteor 1.0 note list app to connect google oauth to it.

When I do this, the page no longer displays correctly because {{username}} is not set at all.

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

Connect the service to an existing account meteor says that the existing account is connected to another service, but in this case I just want to use an external service.

https://stackoverflow.com/questions/25182903/meteor-facebook-registration uses onCreateUser () for manual installation

user.username = user.services.facebook.name 

but this is not portable between services and does not guarantee uniqueness.

https://github.com/aldeed/meteor-collection2 defines the user's scheme so that the username is required.

When I delete the user table (some fields are deleted), the google account does not have a username, and there is no field that can really take this value automatically, as there may be a conflict. You can use email, but I would prefer that the username is not an email address. Am I just forcing the user to enter the 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" : " a_user@example.com ", "family_name" : "User", "gender" : "female", "given_name" : "Alice", "id" : "1115", "name" : "Alice User, } } } 

What is the correct way to solve this problem?

+6
source share
3 answers

In my application, I use this to solve 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 will create a unique username. After successful registration, you can allow users to change the username and check their db for its existence.

+2
source

Here's 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 I am sure that it will be unique. After that, I could let the user set his username or display name as Stackoverflow or other services.

However, you can use the email address as username and let the user change it again later.

+3
source

In my application I use

 if(user.services.facebook) this.user = user.services.facebook.name if(user.services.google) this.user = user.services.google.name 
0
source

Source: https://habr.com/ru/post/977874/


All Articles