I have a SignUp/SignIn Process on my meteor site. I have a form to register new users and a form to login users.
I init users on serverside via
Accounts.createUser({username: "myusername", password: "mypassword"});
and I can login into my dummyusers via my loginform on the client with
Meteor.loginWithPassword(username, password)
I dont know why but for some reason I canĀ“t create new users on clientside. If I call
Accounts.createUser({username: username, password: password});
it returns undefined (safari)
I checked the input strings (username, password) and its not generating the error even pure strings like
Accounts.createUser({username: "test", password: "pass"});
returns undefined on client console.
I published and subscribed my users via:
Server
Meteor.publish('users', function () {
console.log("Server Log: publishing all users");
return Meteor.users.find();
});
Client
Meteor.subscribe("users");
I also can count() users on the client console but I cant use Accounts.createUser() so the createUser() function is not even working in my console.
Why I cant use Accounts.createUser() on client/console?
Accounts.createUser is an asynchronous function on the client because it needs to query the server to know if it actually succeeded (the username or email could already be in use). The return value of an asynchronous function will always be undefined, so the result is expected.
If you start with an empty project and an empty database, you could do:
Accounts.createUser({username: 'dave', password: 'password'});
which will return undefined, however a subsequent call to Meteor.userId() should return a string.
In order to determine if the call succeeded, you will need to supply a callback:
Accounts.createUser({username: 'dave', password: 'password'}, function(err) {
if (err)
console.log(err);
else
console.log('success!');
});
Related
Whenever I try to call "Accounts.createUser" (on the client) I get "EXCEPTION: TypeError: Accounts.createUser is not a function".
Accoridng to the documentation it should work "By default, the Accounts.createUser function provided by accounts-password allows you to create an account with a username, email, or both."https://guide.meteor.com/accounts.html#requiring-username-email
I added
meteor add accounts-base
meteor add accounts-password
My Code:
import {Accounts} from 'meteor/accounts-base';
Accounts.createUser({
username: 'TestUser01',
email: 'TestUser01#test.de',
password: 'string'
}, function (err) {
if (err)
console.log(err);
else
console.log('It worked...');
});
EDIT: As a info, I want to have it client side. Server side works fine for me.
Basically, I'm using Jquery validation package as a way to alert errors to users when creating and registering accounts on Meteor, since the boilerplate interface doesn't work in my case.
Anyway, when a user tries to sign up for an account, I get no response at all on client. The user is just created with no message or redirection like it's supposed to have.
Here is the particular code:
Template.createAccount.onRendered(function(){
var validator = $('.register').validate({
submitHandler: function(){
user = {
email: document.getElementById("email").value,
password: document.getElementById("password").value
};
Accounts.createUser({
email: user.email,
password: user.password,
function(error){
if(error){
if(error.reason == "Email already exists."){
validator.showErrors({
email: "The email belongs to a registered user."
});
}
} else{
console.log("Account successfully created.");
Router.go("starter");
}
}
});
}
});
})
I'm using the same code logic for account logins with the only exception being a different meteor accounts function (Meteor.loginWithPassword() for login and Accounts.createUser() for account creation).
No response at all, so it probably has to do something with the callback function, since the user account is created, but no message displayed on client.
You're including your callback as part of your options object when it should be a separate argument. It should look more like this:
Accounts.createUser({
email: user.email,
password: user.password
}, function(error){
if(error){
if(error.reason == "Email already exists."){
validator.showErrors({
email: "The email belongs to a registered user."
});
}
} else{
console.log("Account successfully created.");
Router.go("starter");
}
});
when using xxx shouldnt my fixture methods be aware of this.userId if I am logged in?
'userId': function () {
console.log("#################");
console.log("################# USERID: ", this.userId);
console.log("#################");
}
This always prints null when called from a step definition.
if you are calling this from a step definition using this.server.call you are making a server to server connection therefore you won't be logged in.
You need to use the client to make a call to be authenticated:
this.client.executeAsync(function() {
Meteor.call('userId') // this will be sent from the client
});
I may be totally off line here, but I'm trying to extend the loginWithPassword method in Meteor to handle only returning users with a few parameters set in their profile.
I'm creating the users fine and once created they login as that user type and all is good, but, when I try and login again I hit a wall.
I've tried implementing my own login handler as follows...
Accounts.registerLoginHandler(function(loginRequest) {
console.log("Got to Accounts.registerLoginHandler");
console.log(loginRequest);
var userId = null;
var user = Meteor.loginWithPassword(loginRequest.email, loginRequest.password, function(error){
if(error !== undefined){
setAlert('error', 'Error in processing login. ' + error.reason + '.');
}
});
var userWithType;
if(user){ // we have the right username and password
console.log("Found a user and logged them in");
userWithType = Meteor.users.findOne({'id': user._id, 'profile.type': loginRequest.type});
}
if(userWithType){
console.log("Found User of that type")
userId = user._id;
}
console.log("UserId", userId);
return {
id: userId
}
});
But am getting an error when I get to this code that says
Got to Accounts.registerLoginHandler
{ email: 'blah2#blah', password: 'blha', type: 'user' }
Exception while invoking method 'login' TypeError: Object #<Object> has no method 'loginWithPassword'
at app/server/login.js:8:23
at tryAllLoginHandlers (app/packages/accounts-base/accounts_server.js:53:18)
at Meteor.methods.login (app/packages/accounts-base/accounts_server.js:73:18)
at maybeAuditArgumentChecks (app/packages/livedata/livedata_server.js:1367:12)
at _.extend.protocol_handlers.method.exception (app/packages/livedata/livedata_server.js:596:20)
at _.extend.withValue (app/packages/meteor/dynamics_nodejs.js:31:17)
at app/packages/livedata/livedata_server.js:595:44
at _.extend.withValue (app/packages/meteor/dynamics_nodejs.js:31:17)
at _.extend.protocol_handlers.method (app/packages/livedata/livedata_server.js:594:48)
at _.extend.processMessage.processNext (app/packages/livedata/livedata_server.js:488:43)
I'm obviously missing a this pointer or something like that, but don't know enough about this framework to know if I'm totally off track here even trying to get this to work.
Ta
P.
I am not too familiar with it but from http://docs.meteor.com, Meteor.loginWithPassword () can only be called on the client. You have written it into the server side code from the tutorial.
That is throwing the error you see. If you move it to the client you will also see that it only returns to the callback function so your variable user will remain undefined.
Meteor.user().profile is available on the client so you can just check the type there in the callback of loginWithPassword to check the information at login.
I am trying to build a basic web application w/ user authentication via email/password registration using Firebase.
My setup right now includes a main.js file that consists of the following:
var dbRef = new Firebase('https://url.firebaseIO.com');
var authClient = new FirebaseAuthClient(dbRef, function(error, user) {
if (error) {
// an error occurred while attempting login
console.log(error);
} else if (user) {
// user authenticated with Firebase
console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
} else {
// user is logged out
console.log('logged out!');
}
});
function next(){
window.location = 'index.html';
}
function test(){
authClient.login('password', {
email: email,
password: password,
rememberMe: true
},next());
// window.location = 'index.html';
}
I obtain email/password values from a form and login. That works. But as soon as I include a callback function to then redirect them to a new authenticated page, it no longer works. In fact, most of the time I get an "UNKOWN ERROR" response.
When I get to the next page, I am no longer logged in. If I remove the next() function and stay on the same page, it works - even if I then trigger the next function from the console. Is there a different way you are supposed to proceed to another page?
I'm pretty sure there is some sort of communication issue (possibly the login does not get a return before the page is switched?) because if I add a 1s timeout before the next function, it then works. But surely this is not best practice?
Thanks!
Per https://www.firebase.com/docs/security/simple-login-email-password.html, the authClient.login() method does not actually accept a callback, so the problem you're seeing is likely the result of navigating away from the current page before the callback is returned, as you suggested.
I would recommend doing the redirect in the callback you're passing during the instantiation of the auth client. (new FirebaseAuthClient(ref, callback)) and redirect if you detect a logged-in user. This callback will be invoked once upon instantiation with the current authentication state of the user, and then again any time the user's authentication state changes (such as on login or logout).