Trying to publish users in Meteor - meteor

I'm quite new in Meteor and I got an issue: I added some information to users and I'd like to be able to see it in a view.
So I create my users, here is my event:
Accounts.createUser({
username: username,
email: email,
password: password,
firstname:firstname,
lastname:lastname,
chief:chief
},
function(error){
if(error){
alert(error.reason)
}else{
Router.go('home');
}
});
Accounts.onCreateUser(function(options,user){
user.firstname = options.firstname;
user.lastname = options.lastname;
user.chief = options.chief;
return user;
});
then I publish my collection to get access on my client side of the app:
Meteor.publish("personaldata", function () {
return Meteor.users.find({_id: this.userId});
});
And without doing anything else I got an issue on my app: there is no error message on my command prompt, but when I open my app I got an Ironn:Router message. Here is the message:
'organize your application'
Router.route('/', function () {
this.render('Home', {
data: function () {
return Items.findOne({_id: this.params._id});
}
});
});
Yes the whole part is the message. I tried to subscribe to the collection in 'home' to settle the issue but it doesn't work, the same message is displayed.Does someone know why this message is displayed ? How can I have access to those data? Isn't it the proper way?
Hope you guys can figure out what the problem is, thanks.

Found the problem...
As I'm stupid I put my publish in the lib directory, so this was on both the server and the client side... Meteor didn't know what to do with it on the client side.

Related

How to insert a username into a post in meteor

I want to insert the username along with the post. When I tried adding a post using Post.insert({content: content}) the post got inserted and displayed on the client successfully. Now I try to insert a username also, so I try like this:
var username = new Meteor.user().username;
Post.insert({content: content, username: username});
Now not only the username not inserted it fails to insert the content as well. No error. What am I doing wrong?
The above is the problem. But I did a mistake and deleted insecure package so now I can not check the results from console or inserting directly at client. So the following is the method I used. (Please be patient, I'm new to meteor, if I ask stupid questions, pardon me).
In imports/both/post.js I put:
export const Post = new Mongo.Collection('post');
In server/main.js
import { Post } from '/imports/both/post.js';
Meteor.methods({
addPost: function (content) {
var user = Meteor.users.findOne(this.userId);
var username = user ? user.username : 'Anonymous';
Post.insert({content: content, username: username});
}
});
In client/template/postsForm.js, the following code:
import { Post } from '/imports/both/post.js';
Template.postsForm.events({
'submit form': function(event){
event.preventDefault();
var content = document.getElementById('content').value;
Meteor.call('addPost', content);
event.target.reset();
}
});
I can insert a post from server and get displayed on client but from client side I can't. And I get no error.
are you sure it's failing on the insert? you have a "new" in front of Meteor.user(), i suspect it's failing there. try the following, which includes a null check on the user and shows any error coming from the insert:
let username = Meteor.user() && Meteor.user().username;
Post.insert({content: content, username: username}, function(error) {
if (error) {
console.error(error);
}
};
you also have 'content' in quotes, which looks wrong.

Accounts.createUser doesn't save profile

I have the following piece of code in my server/fixtures.js file:
var userId = Accounts.createUser({
username: "tester",
email: "a#b.com",
password: "foobar",
profile: { name: "Max" }
});
var user = Meteor.users.findOne({_id: userId});
console.log(user.profile.name);
Now when I run meteor it logs undefined. What am I doing wrong?
I'm pretty sure I've had an Accounts.onCreateUser callback defined somewhere that was responsible for this. My bad!
I think you need to use Meteors Publications & Subscriptions. Check this link
for more info.
Example:
if (Meteor.isServer){
Meteor.publish('userdata', function() {
if(!this.userId) return null;
return Meteor.users.find( this.userId );
});
}
if (Meteor.isClient){
Meteor.subscribe('userdata');
console.log(Meteor.user());
}
So, the problem is that console.log is not available on the server (where this file runs according to Meteor directory conventions.). If you would like to log to the server console, you can use Meteor._debug() which works the same as console.log. You may also want to consider wrapping your code in a Meteor.startup(function() {}); block so that it runs as soon as the server spins up.

resetPassword issues in meteor

I sent enrollment email to the user and when he enters password and other details I'm trying to reset the password but it is throwing error
uncaught error extpected to find a document to change
As you can see in the mage
I've subscribed to the user record
my code
this.route('enroll', {
path: '/enroll-account/:token',
template: 'enroll_page',
onBeforeAction: function() {
Meteor.logout();
Session.set('_resetPasswordToken', this.params.token);
s = this.subscribe('enrolledUser', this.params.token).wait();
}
}),
After I'm displaying form and on the submit event
onSubmit: function(creds) {
var options = {
_id: Meteor.users.findOne()._id,
name: creds.name
}
var token=Session.get('_resetPasswordToken');
Meteor.call('updateUser', options, function(error, result) {
if(!error) {
Accounts.resetPassword(token, creds.password, function(error) {
if (error) {
toastr.error("Sorry we could not update your password. Please try again.");
return false;
}
else{
toastr.error("Logged In");
Router.go('/');
}
});
} else {
toastr.error("Sorry we could not update your password. Please try again.");
return false;
}
});
this.resetForm();
this.done();
return false;
}
Everything is working fine but resetpassword callback is not triggering and the above error is displaying in console.
my token is get deleted from the user record and I'm able to login using login form but
From the docs
Reset the password for a user using a token received in email. Logs the user in afterwards.
I'm not able to automatically login after resetting the password,above error is throwing
What am I missing here?
this.subscribe('enrolledUser', this.params.token).wait();
here you're subscribing using resetPassword token
when you call Accounts.resetPassword method the method will reset the password and delete the token from user record.
So your subscription is lost and there are no records available in client side to modify
(That is waht the error Expected to find a document to change)
Instead on first subscription save the user Id and subscribe to the user record using Id
so the subscription will not be lost
path: '/enroll-account/:token',
template: 'enroll_page',
onBeforeAction: function() {
Meteor.logout();
Session.set('_resetPasswordToken', this.params.token);
s = this.subscribe('enrolledUser', this.params.token).wait();
},
onAfterAction:function(){
if(this.ready()){
var userid=Meteor.users.findOne()._id;
Meteor.subscribe("userRecord",userid);
}
}
Alternatively, you could do something like as follows in your publication. This worked for me (but mine was a slightly more involved query than this).
Meteor.publish('enrolledUser', function (token) {
check(token, String);
return Meteor.users.find({
$or: [{
_id: this.userId
}, {
'services.password.reset.token': token
}]
});
});
From the docs, it says
Reset the password for a user using a token received in email. Logs the user in afterwards.
So basically, you have to subscribe to the logged in user after the fact as well. A little silly, but whatever.

How to validate data in onCreateUser without losing the form data?

I'm writing an Appliction using Meteor. In this App I want to implement a server-side validation of the user data using Accounts.onCreateUser. There is some data passed which can only be verified on the server side.
At client side I call:
Template.register.events({
'submit form': function (e) {
e.preventDefault();
var attributes = {
username: $("#inputUsername").val(),
password: $("#inputPassword").val(),
confirmation: $("inputConfirmation").val(),
email: $("#inputEmail").val(),
...
};
Accounts.createUser(attributes, function(err){
if (err) {
throwError(err);
} else {
}
});
}
});
And on the server side:
Accounts.onCreateUser(function(options, user) {
if(!verifyData(options))
throw new Meteor.Error(403, "Wrong input");
return user;
});
After the server side verification fails, all form data is lost. What is the best way to keep the data?
I went ahead and reproduced your code on a Meteorpad and from what I can tell, the form data does still persist. You just need to access it via the attributes variable in the client-side.
There may be something I am missing, but i took what you posted above and put it in there.

How to get Google+ profile with Meteor.loginWithGoogle?

I'm looking for a working example of Meteor.loginWithGoogle (with meteor 0.6.4.1).
I found this one for loginWithGitHub (https://www.eventedmind.com/posts/meteor-customizing-login) that works fine with GitHub.
It works fine without parameters as show here on client side :
Template.user_loggedout.events({
"click #login": function(e, tmpl){
Meteor.loginWithGoogle({
}, function (err) {
if(err) {
//error handling
alert('error : '+err.message);
} else {
}
});
}
});
with the Accounts params on server side :
Accounts.loginServiceConfiguration.remove({
service: 'google'
});
Accounts.loginServiceConfiguration.insert({
service: 'google',
clientId: 'XXXXXX',
secret: 'YYYYYY'
});
In this case how can i get currentUser information especially the mail ?
Is there a way to get the Google+ profile of the user (if he has one and allows this), the user's avatar for example ?
What are the needed parameters for requestPermissions: , what can i get with this ?
Thanks
After some research i build my own example available here : https://github.com/lc3t35/googlelogin
Thanks to :
https://github.com/m2web/githublogin
https://github.com/ananta-IO/marq
Meteor/MongoDB see available fields for publish?
https://github.com/mrtnbroder/meteor-snippets/blob/master/snippets/js/Accounts/loginWithGoogle.sublime-snippet
https://developers.google.com/accounts/docs/OAuth2Login#obtaininguserprofileinformation

Resources