I'm new to meteor and react. I successfully followed this tutorial here https://www.meteor.com/tutorials/react/adding-user-accounts
Now I want to customize the code created from this tutorial. I want to change the registration system so that when a user creates a new account, they need to supply the following mandatory information:
profile picture of themselves
valid credit card information so that I can charge them $5/month against a credit card processing gateway
the values 1, 2 or 3 to denote which type of monthly subscription they
would like
I'm trying to figure out to what extent Meteor's account package what I need, and for the things I can't support, what properties can I override to introduce the behaviour that I need? Or do I need to build my own registration system from scratch?
Hopefully my question isn't too broad...I just need some direction on what is or isn't possible out of the box with Meteor accounts.
You can add some stuff when user is created with:
//server
Accounts.onCreateUser(function(options,user){
if (typeof(user.services.facebook) != "undefined") {
user.email = user.services.facebook.email;
user.profilePicture = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=large";
user.username = user.services.facebook.name;
//other fields which you can also insert as '' or null
}
if (typeof(user.services.twitter) != "undefined") {
user.profilePicture = user.services.twitter.profile_image_url;
user.username = user.services.twitter.screenName;
//other fields which you can also insert as '' or null
}
if (typeof(user.services.google) != "undefined") {
user.email = user.services.google.email;
user.profilePicture = user.services.google.picture;
user.username = user.services.google.name;
//other fields which you can also insert as '' or null
}
if (typeof(user.services.password) != "undefined") {
user.profilePicture = '/predefined-logo.png';
//e-mail, username & password are not defined/added here
}
return user;
});
Later, you can add/edit more stuff with Methods
Meteor.methods({
methodName: function(userId, var1, var2) {
Meteor.users.update(userId,{
$set: {
someField: var1,
someField2: var2
}
})
}
})
So you could create a template with a form and update the user like ^.
Related
In Meteor, I'd like to add a referrer field to new user signups. I can do this for users that register with a password, by passing in extra options to be used with Accounts.onCreateUser(function (options, user).
But this doesn't seem to work with social login. Any ideas how I can get this working?
I think I had a similar issue, and ended doing something like
create the user without the field you would like to pass as an option
ask the client for the value of that field
update the user
So:
//server
Accounts.onCreateUser(function(options, user) {
if (user.services.facebook)
user.askReferer = true;
retur user;
});
//client
var userId;
Tracker.autorun(function() {
var user = Meteor.user();
if (!user || user.id === userId || !user.askReferer) {return;}
userId = user.id;
var referer = ...;
Users.update({_id: user.id}, {$set: {referer: referer}}, {$unset: {askReferer: 1}});
});
Would love if someone comes up with a better solution!
I'm dealing with one problem more than 10 days now, and I don't know what to do with it, so I hope that I'll get my solution here.
I have two roles 'admin' and 'user': the first user is added as the admin thanks to the alanning:roles package. However, the problem is that I'm not sure if I set the 'user' role as default.
The picture below shows the code for the createUser function.
Client//Account//account.js
Template.signup.events({
'submit form': function(event) {
event.preventDefault();
var nameVar = event.target.signupName.value;
var emailVar = event.target.signupEmail.value;
var passwordVar = event.target.signupPassword.value;
Accounts.createUser({
name: nameVar,
email: emailVar,
password: passwordVar,
profile: {
roles: ["user"]
}
});
}
});
And below code is the Accounts.onLogin function:
Client//Lib//routes.js
if (Meteor.user().roles = 'admin'){
FlowRouter.go('dashboard');
}
else if (Meteor.user().roles = 'user'){
FlowRouter.go('account');
}
I hope that you understand what my problem is and I looking forward the solution. In conclusion, I need to have 'admin' and 'user' role, and when it is admin it should go to /admin-dashboard route, if it is user it should go to /account route.
Thank you all :D
Problem is here:
if (Meteor.user().roles = 'admin'){ // assigning, not equality check
FlowRouter.go('dashboard');
}
else if (Meteor.user().roles = 'user'){
FlowRouter.go('account');
}
But roles field is an array, so do this instead:
if (Meteor.user().roles.indexOf('admin') !== -1){
FlowRouter.go('dashboard');
}
else if (Meteor.user().roles.indexOf('user') !== -1){
FlowRouter.go('account');
}
As the title suggests I would like to provide functionality to allow a user to update the email they use to login to my app using Firebase Simple Login. Cannot figure out an elegant way to do this. App uses AngularFire if that is relevant.
Does one exist or do I need to create a new account and delete the old one using the $removeUser() and $createUser() methods?
Update for Firebase 2.1.x
The Firebase SDK now provides a changeEmail method.
var ref = new Firebase('https://<instance>.firebaseio.com');
ref.changeEmail({
oldEmail: 'kato#domain.com',
newEmail: 'kato2#kato.com' ,
password: '******'
}, function(err) {
console.log(err ? 'failed to change email: ' + err : 'changed email successfully!');
});
Historical answer for Firebase 1.x
In Simple Login, this is equivalent to changing the user's ID. So there is no way to do this on the fly. Simply create the new account, remove the old one as you have already suggested.
If you're user profiles in Firebase, you'll want to move those as well. Here's brute force, safe method to migrate an account, including user profiles. You could, naturally, improve upon this with some objectification and futures:
var ref = new Firebase('URL/user_profiles');
var auth = new FirebaseSimpleLogin(ref);
// assume user has logged in, or we obtained their old UID by
// looking up email address in our profiles
var oldUid = 'simplelogin:123';
moveUser( auth, ref, oldUid, '123#abc.com', '456#def.com', 'xxx' );
function moveUser( auth, usersRef, oldUid, oldId, newId, pass ) {
// execute activities in order; first we copy old paths to new
createLogin(function(user) {
copyProfile(user.uid, function() {
// and once they safely exist, then we can delete the old ones
removeOldProfile();
removeOldLogin();
});
});
function copyProfile(newId, next) {
ref.child(oldUid).once('value', function(snap) {
if( snap.val() !== null ) {
ref.child(newId, snap.val(), function(err) {
logError(err);
if( !err ) { next(); }
});
}
});
}
function removeOldProfile() {
ref.child(oldId).remove(logError);
}
function createLogin(next) {
auth.createUser(newId, pass, function(err, user) {
logError(err);
if( !err ) { next(user); }
});
}
function removeOldLogin() {
auth.removeUser(oldId, pass, logError);
}
}
function logError(err) {
if( err ) { console.error(err); }
}
I am using Meteor and people can connect to the site via Facebook. I'm using people username to identify them. But, some of them don't have a username. For example, a new user has null as a username. What I'm trying to do is, if the person has a username then OK we use their username. If not, I wanna use their Facebook id as their username. The problem is, my if condition is not working properly. If the person has a username, the if condition considers that the person doesn't. The weird thing is, if I do a console.log of the username before the if condition, it will show the username. But once in the if, it considers that the username is null. Here is the code :
Accounts.onCreateUser(function(options, user) {
var fb = user.services.facebook;
var token = user.services.facebook.accessToken;
if (options.profile) {
options.profile.fb_id = fb.id;
options.profile.gender = fb.gender;
options.profile.username = fb.username
console.log( 'username : ' + options.profile.username);
if ( !(options.profile.username === null || options.profile.username ==="null" || options.profile.username === undefined || options.profile.username === "undefined")) {
console.log('noooooooo');
options.profile.username = fb.id;
} else {
console.log('yessssssss');
options.profile.username = fb.username;
}
options.profile.email = fb.email;
options.profile.firstname = fb.first_name;
user.profile = options.profile;
}
sendWelcomeEmail(options.profile.name, options.profile.email);
return user;
});
With this code, if I log in with my Facebook that has a username. The condition will show 'noooooooo' but the console.log( 'username : ' + options.profile.username); will show my username. Why does it do that? :l
It's because creating is called before logging and logging is asynchronous .. so you cannot ensure that your if will be or will not true/false. Your putting information from fb service is redundant, because all these informations are already saved with user.
http://docs.meteor.com/#meteor_user
You should obtain information about user after he is loggedIn because in that moment you will be able to recognise what kind of identifier you can use username/id.
//Server side
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId});
// You can publish only facebook id..
/*return Meteor.users.find({_id: this.userId},
{
fields: {
'services.facebook.id': true
}
}
);*/
});
//Client side
Meteor.subscribe("userData");
// .. you can see more informations about logged user
console.log(Meteor.users.find({}).fetch());
Firebase Simple login provides an email/password option, how do I use it? Starting from from creating a user, storing data for that user, to logging them in and out.
There are three distinct steps to be performed (let's assume you have jQuery):
1. Set up your callback
var ref = new Firebase("https://demo.firebaseio-demo.com");
var authClient = new FirebaseAuthClient(ref, function(error, user) {
if (error) {
alert(error);
return;
}
if (user) {
// User is already logged in.
doLogin(user);
} else {
// User is logged out.
showLoginBox();
}
});
2. User registration
function showLoginBox() {
...
// Do whatever DOM operations you need to show the login/registration box.
$("#registerButton").on("click", function() {
var email = $("#email").val();
var password = $("#password").val();
authClient.createUser(email, password, function(error, user) {
if (!error) {
doLogin(user);
} else {
alert(error);
}
});
});
}
3. User login
function showLoginBox() {
...
// Do whatever DOM operations you need to show the login/registration box.
$("#loginButton").on("click", function() {
authClient.login("password", {
email: $("#email").val(),
password: $("#password").val(),
rememberMe: $("#rememberCheckbox").val()
});
});
}
When the login completes successfully, the call you registered in step 1 will be called with the correct user object, at which point we call doLogin(user) which is a method you will have to implement.
The structure of the user data is very simple. It is an object containing the following properties:
email: Email address of the user
id: Unique numeric (auto-incrementing) ID for the user
FirebaseAuthClient will automatically authenticate your firebsae for you, not further action is required. You can now use something like the following in your security rules:
{
"rules": {
"users": {
"$userid": {
".read": "auth.uid == $userid",
".write": "auth.uid == $userid"
}
}
}
}
This means, if my User ID is 42, only I can write or read at example.firebaseio-demo.com/users/42 - when I am logged in - and no-one else.
Note that Simple Login does not store any additional information about the user other than their ID and email. If you want to store additional data about the user, you must do so yourself (probably in the success callback for createUser). You can store this data as you normally would store any data in Firebase - just be careful about who can read or write to this data!
Just incase someone is reached to this thread and looking for some example application using the firebase authentication. Here are two examples
var rootRef = new Firebase('https://docs-sandbox.firebaseio.com/web/uauth');
......
.....
....
http://jsfiddle.net/firebase/a221m6pb/embedded/result,js/
http://www.42id.com/articles/firebase-authentication-and-angular-js/