Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router.onBeforeAction AccountsTemplates.ensureSignedIn #278

Closed
doubletaketech opened this issue Feb 10, 2015 · 23 comments
Closed

Router.onBeforeAction AccountsTemplates.ensureSignedIn #278

doubletaketech opened this issue Feb 10, 2015 · 23 comments
Labels

Comments

@doubletaketech
Copy link

My home page is protected with ensureSignedIn. If the user is already logged in and goes to the home page, the login screen flashes before the home loads. How do I prevent this? I don't want to see the login screen at all if the user is already logged in.


Router.onBeforeAction(AccountsTemplates.ensureSignedIn, {
    except: ['app-storage','resetPwd','atResetPwd']
});
Router.configure({
    layoutTemplate: 'masterLayout',
    loadingTemplate: 'loading',
    notFoundTemplate: 'pageNotFound',
    yieldTemplates: {
        navbar: {to: 'navbar'},
        footer: {to: 'footer'},
        leftMenu: {to: 'leftMenu'},
        rightMenu: {to: 'rightMenu'}
    }
});


Router.map(function() {

    this.route('home', {
        path: '/',
        template: 'grid'
    });
@splendido
Copy link
Member

Yeah, I can reproduce this, that's a bug :(

Might it be that this line should also check Meteor.loggingIn?

@splendido splendido added the bug label Feb 10, 2015
@doubletaketech
Copy link
Author

Glad you were able to reproduce. Thanks!
When do you think a fix would be included in the Meteor package?

Don

@splendido
Copy link
Member

I'd be glad if you could play a bit with this and create a PR.
I just have very little spare time and I already have a number of things to test for the next release.
But I'll see what I can do...

Tnx!

@doubletaketech
Copy link
Author

I'd like to try. What do I do in order to make the change and how do I get an version of the package to test it in my app? I'm not familiar with working on community projects like this. But, I want to help where I can.

Thanks,
Don

@doubletaketech
Copy link
Author

When I click New Pull Request, the green button is grayed out. How to I request client.js to edit this line?

@splendido
Copy link
Member

Hei @doubletaketech,
lets have a look at Working Locally
Let me know if you need more help.

@splendido
Copy link
Member

I think I've found a little trick to manage this...

In any case you'd better add another hook like this before the one using ensureSignedIn:

Router.onBeforeAction(function () {
  if (Meteor.loggingIn()) {
    this.render('loading');
    this.renderRegions();
  } else {
    this.next();
  }
}, {
  only: ['home']
});

Tnx!

@micahalcorn
Copy link

@splendido's solution works great for me.

@splendido
Copy link
Member

@micahalcorn do you mean the fix committed or the additional onBeforeAction hook?

@doubletaketech
Copy link
Author

Thank you. Is this new version available via meteor update?

@splendido
Copy link
Member

Yep! Version 1.7.0

@doubletaketech
Copy link
Author

I've updated to 1.7.1 and I still see the login screen flash before home page loads.

If I add your extra hook, with loading, then it works fine.

Thank you! Appreciate your great work!

Don

@splendido
Copy link
Member

...ok, I'll try to debug this a little more.

Tnx!

@splendido
Copy link
Member

...I've eventually did it:

see e9ee6d2 and Content Protection

@doubletaketech
Copy link
Author

I updated to 1.8 and replaced my previous code with

Router.plugin('ensureSignedIn', {
    except: ['app-storage', 'atSignIn', 'atSignUp', 'atForgotPassword']
});

but I get this error ... ^
Error: No plugin found named "ensureSignedIn"
at Function.Router.plugin (packages/iron:router/lib/router.js:394:1)
at app/routes.js:36:8

when attempting to run the app.

Thanks,
Don

@splendido
Copy link
Member

mmmm, I've just realized I defined it for the client only...

Are you by any chance setting it on the server side?

@doubletaketech
Copy link
Author

my routes.js is available to both.
I wrapped it with isClient and it works great! No more login flash. Great job!

Thank you!

if(Meteor.isClient){
    Router.plugin('ensureSignedIn', {
        except: ['app-storage', 'atSignIn', 'atSignUp', 'atForgotPassword']
    });
}

@splendido
Copy link
Member

I'll move the definition to be available on both sides...
I'm glad it's working properly now :)

Thank you for the heads up!

@splendido
Copy link
Member

May I ask if you could make some animated gifs comparing the before and after the plugin?
...it might be a good idea to post this comparison somewhere within some post ;-)

@splendido
Copy link
Member

...reasoning a bit more, there's no point to have the same plugin working 'server-side' where Meteor.loggingIn() and meteor.user() are nto available.

Do you think a fake definition on the server-side would be enough to get no complains and permit to define routing on both sides?

@doubletaketech
Copy link
Author

From my learning experience with Meteor last year, they tended to recommend that one routes.js be available to both client and server. Especially since Iron-Router has the "where" to designate something as a server route.

So, anything you can do to make it "idiot proof" for people like me, the less issues will be posted. :) If faking doesn't cost you anything, then do it. But, you may be able to just handle this in your docs reminding people that is client-side only plugin.

So, you are wanting to compare before and after. Do you mean before, when the login prompt used to appear ... and after, when it no longer appears?

Don

@splendido
Copy link
Member

Don't be silly with yourself: you're not an idiot for sure! ;-)

You're right about the good practice to declare routes both sides, this is why I think we should fix this issue.
The problem is that the ensureSignedIn logic works good on the client but the same code cannot run on the server because of some missing part of UserAccounts which are shipped to the client only.
...for example there's no AccountsTemplates.state on the server side.

At the same time, there's no point for a plugin like this to be executed on the server side, since there's no way to check which user is requesting a particular route.

So my proposal is to create an empty plugin for the server side so to get the code running there as well (without getting any error...) hence permitting IR stuff definition on both sides.

I'll try to publish a patch tomorrow.

...and yep, the request for the comparison was for before/after the plugin ;-)

@splendido
Copy link
Member

I've just answered this question by @fardeemmunir on SO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants