Accounts.onLogin throws session not found error - meteor

The LoggedInUser works well as it is suppose but whenever the app starts and the URL is queried like trying to login or navigate to any other URL, the Accounts.onLogin throws the below error. I don't know what could be the reason.
var LoggedInUser = FlowRouter.group({
name: 'currentUser', triggersEnter: [function () {
if (!Meteor.loggingIn() || !Meteor.userId()) {
var currentRoute = FlowRouter.current();
if (!currentRoute.route.name === 'home') {
console.log(currentRoute.path);
Session.set('redirectAfterLogin', currentRoute.path);
}
FlowRouter.go('home');
}
}]
});
Accounts.onLogin(function () {
let redirect = Session.get('redirectAfterLogin');
if (redirect) {
if (redirect != 'home') {
FlowRouter.go(redirect);
}
}
});
Error on cmd console
I20171003-18:28:44.913(1)? Exception in onLogin callback: ReferenceError: Session is not defined
I20171003-18:28:44.919(1)? at lib/routes/routes.js:30:18
I20171003-18:28:44.921(1)? at runAndHandleExceptions (packages\callback-hook.js:152:24)
I20171003-18:28:44.926(1)? at packages\callback-hook.js:159:12
I20171003-18:28:44.931(1)? at packages/accounts-base/accounts_server.js:164:5
I20171003-18:28:44.934(1)? at [object Object]._.extend.each (packages\callback-hook.js:128:15)
I20171003-18:28:44.938(1)? at AccountsServer.Ap._successfulLogin (packages/accounts-base/accounts_server.js:163:21)
I20171003-18:28:44.943(1)? at AccountsServer.Ap._attemptLogin (packages/accounts-base/accounts_server.js:353:10)
I20171003-18:28:44.946(1)? at [object Object].methods.login (packages/accounts-base/accounts_server.js:530:21)
I20171003-18:28:44.949(1)? at packages\check.js:128:16
I20171003-18:28:44.953(1)? at [object Object].EVp.withValue (packages\meteor.js:1135:15)

The onLogin function you are using needs the Session package according to the following line :
let redirect = Session.get('redirectAfterLogin');
The error in the console states than the Session package can not be found. Please make sure of the following:
Meteor Session is installed. If not please install it with:
meteor add Session
in your terminal in your project folder.
Session is imported in the file you want to use it, if not please add at the top of your file :
import { Session } from 'meteor/session'

Related

Meteor: callLoginMethod not found error

I'm having difficulty invoking a login method, it follows
$ meteor list
Accounts-base 1.2.14 A user account system
Ecmascript 0.6.1 Compiler plugin that supports ES2015 + in all .js files
Meteor-base 1.0.4 Packages that every Meteor app needs
React 15.0.1 Everything you need to use React with Meteor.
Static-html 1.1.13 Defines static page content in .html files
/server/main.js
import { Accounts } from 'meteor/accounts-base'
Accounts.registerLoginHandler('simples', (ttt) => {
console.log(ttt);
});
/client/main.js
autenticar(){
Accounts.callLoginMethod({
methodName: 'simples',
methodArguments: [{ tipo : 'simples' }],
validateResult: function (result) {
console.log('result', result);
},
userCallback: function(error) {
if (error) {
console.log('error', error);
}
}
})
}
When calling authenticar(), I get this error:
errorClass
  Details: undefined
  Error: 404
  ErrorType: "Meteor.Error"
  Message: "Method 'simples' not found [404]"
  Reason: "Method 'simples' not found"
Where is the error?
I've never used this API personally, but from a quick glance through the Meteor internals, I see a couple issues.
Accounts.registerLoginHandler only adds an additional handler to an array of built-in handlers which are called as part of the default Meteor login process.
If you are trying to plug in an additional handler into the existing process, you should call Accounts.callLoginMethod without the methodName key.
Calling Accounts.callLoginMethod with methodName will bypass the built-in handlers completely and replace them with your custom method, however this method needs to be declared separately by you with Meteor.methods, not registerLoginHandler.
So, that's probably your error -- you need to define your simples method with Meteor.methods. Also, you should check the code for the requirements of this method, see the comments in the code here:
https://github.com/meteor/meteor/blob/devel/packages/accounts-base/accounts_client.js
Only to complement and keep as a referral for someone else to get here. That way it's working
client.js
Accounts.callLoginMethod({
methodArguments: [{tipo: 'simples'}],
validateResult: (result) => {
console.log('success', result);
},
userCallback: function(error) {
if (error) {
console.log('error', error);
}
}
});
server.js
Meteor.startup(function () {
var config = Accounts.loginServiceConfiguration.findOne({
service : 'simples'
});
if (!config) {
Accounts.loginServiceConfiguration.insert({ service: 'simples' });
}
});
Accounts.registerLoginHandler((opts) => {
if(opts.tipo === 'simples'){
return Accounts.updateOrCreateUserFromExternalService ('simples', {
id: 0 // need define something
}, {
options : 'optional'
})
}
});

Meteor Account.createUser TypeError: Cannot read property 'accessToken' of undefined

I try to create User Registration. I have install
accounts-base 1.2.2* A user account system
accounts-password 1.1.4* Password support for accounts
On client side :
var userNew = {
password: textPassword,
username: textUserName,
profile: {
address: textAddress
}
};
Accounts.createUser(userNew, function (err) {
if (err) {
alert(err.message);
} else {
Router.go('/');
}
});
But show error :
I20160423-17:47:07.299(7)? Exception while invoking method 'createUser' TypeError: Cannot read property 'accessToken' of undefined
Also i have set on server side :
Accounts.config({
forbidClientAccountCreation : false
});
I done with update & recreate PROJECT. I dont figure whats the problems. but recreate project solved this problems.
Hopelly its helps others

"Object is not a function" in a Meteor route

I just created two routes that work just fine, but I'm getting an odd error in the console that I would like to fix.
Exception in callback of async function: TypeError: object is not a function
at OnBeforeActions.loginRequired (http://localhost:3000/client/router/config.js?8cea1a53d7ab131377c2c4f91d534123cba79b70:12:20)
This error shows up every time I visit the same page.
This is my config.js file:
Router.configure({
layoutTemplate: "uMain"
});
var OnBeforeActions = {
loginRequired: function (pause) {
"use strict";
if (!Meteor.userId()) {
this.render("uLogin");
return pause();
} else {
this.next();
}
}
};
Router.onBeforeAction(OnBeforeActions.loginRequired, {
except: ["uLogin"]
});
The idea is to redirected all user who are not logged in to "uLogin".
It works (or I haven't found any bugs so far).
What am I doing wrong?
You can see the line where you have the error in developers console when you click on link http://localhost:3000/client/router/config.js?8cea1a53d7ab131377c2c4f91d534123cba79b70:12:20 in your console.
Your problem is that new Iron Router does not use pause() anymore. Remove pause from your onBeforeAction.
Developers console is your good friend. Learn how to use it.

access data from iron-router in rendered function

I'm trying to access data passed from iron router in the javascript function
router.js
this.route('editOrganization', {
path: '/editOrganization',
waitOn: function() {
return [
Meteor.subscribe('organization', this.userId)
];
},
data: function() {
return Organizations.findOne();
}
});
now if I wanted to access a property of organization in html (editCompany.html) I can do the following
{{name}}
but how do I access that same property in the js file
Template.editOrganization.rendered = function() {
//how do I access name?
}
UPDATE:
so if I click a link to edit organization I can get the value via
this.data.name
However, if I reload the page (same url) it throws an error saying data is null.
It is accessible through the rendered function context.
Template.editOrganization.rendered = function() {
var name = this.data && this.data.name;
};
This is confusing for many people but you need to configure the router to actually wait for the subscriptions you returned with waitOn.
Router.onBeforeAction('loading')
You can read the author's explanation here:
https://github.com/EventedMind/iron-router/issues/554#issuecomment-39002306

Extending the Meteor loginWithPassword method

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.

Resources