问题
I use onBeforeActions to check if users are logged in, if their profile is incomplete or disabled. This seems to work correctly.
But now the problem is whenever I directly go to a page within the app, I get redirected to startPage as well. I debugged and discovered 'user' is undefined, although I'm logged in. I use accounts-fb & -tw and account-ui packages.
How can I make sure the user is logged in? I don't understand the correct timing of the functions..
edit: Locally I don't always seem to have this problem. user is often defined. But on my production server (I just uploaded it with a debugger statement, yes I have no testserver :/) it always seems to be undefined and forwarding me to the startPage..
router.js:
if (Meteor.isClient) {
// only for client, else the serverside router will try to run this onBeforeAction
var onBeforeActions;
onBeforeActions = {
loginRequired: function() {
var user = Meteor.user();
if (!user) {
Router.go('startPage');
} else {
if (user && user.profile && user.profile.incomplete) {
Router.go('completeSignup');
}
if (user && user.profile && user.profile.disable) {
Router.go('profileEdit');
}
}
this.next();
}
};
Router.onBeforeAction(onBeforeActions.loginRequired);
Router.configure({
notFoundTemplate: 'notFound',
layoutTemplate: 'layoutMain',
loadingTemplate: 'loading',
trackPageView: true
});
Router.map ({});
}
回答1:
Is the user in the middle of logging in? What does Meteor.loggingIn() return? Here is my login check using Iron Router in Coffeescript.
requireLogin = ->
if not Meteor.user()
if Meteor.loggingIn()
this.render this.loadingTemplate
else
this.render 'accessDenied'
else
this.next()
Try something simple like this, and then add the little bits of what you want to do after. But I'm fairly sure Meteor.loggingIn() is what you're looking for here.
Here is the documentation on it: http://docs.meteor.com/#/full/meteor_loggingin
EDIT: It probably works locally because there isn't any latency so if you're logged in it can run everything immediately. If that makes sense.
来源:https://stackoverflow.com/questions/31504487/iron-router-onbeforeactions-is-redirecting-to-startpage