javascriptmeteoriron-router

Meteor and iron router, wait on login


I have a login route. I made this hook on before the action :

onBeforeAction: function() {

    console.log(Meteor.user())
    console.log(this.route.name)

    // If the user is not logged and if we are not on the login page
    // Redirect the user to the login page
    if( !Meteor.user() && this.route.name !== 'login' ) {
        Router.go('login');
    };

    // If the user is logged, take him out of the login page
    if( Meteor.user() && this.route.name === 'login' ) {
        Router.go('newsletters.index');
    };
}

But what about the login in time ?

I would like to make the router wait if Meteor.loggingIn() is true. The waitOn is about the subscriptions and did not work.

I'm pretty sure some else did that before :)


Solution

  • I use...
    Iron-Router ($meteor add cmather:iron-router) and
    Accounts-entry ($ meteor add joshowens:accounts-entry).

    You can check in Git Hub
    https://github.com/Differential/accounts-entry
    https://github.com/EventedMind/iron-router

    For configuration, you must create in the client folder one archive, for example, config.js

    Meteor.startup(function () {
        AccountsEntry.config({
          logo: 'logo.png',                  // if set displays logo above sign-in options
          homeRoute: '/',                 // mandatory - path to redirect to after sign-out
          dashboardRoute: '/dashboard',      // mandatory - path to redirect to after successful sign-in
          profileRoute: 'profile',
          passwordSignupFields: 'USERNAME_AND_EMAIL',
          language: 'en',
          showOtherLoginServices: true,     // Set to false to hide oauth login buttons on the signin/signup pages. Useful if you are using something like accounts-meld or want to oauth for api access
          extraSignUpFields: [{             // Add extra signup fields on the signup page
            field: "name",                           // The database property you want to store the data in
            name: "This Will Be The Initial Value",  // An initial value for the field, if you want one
            label: "Full Name",                      // The html lable for the field
            placeholder: "John Doe",                 // A placeholder for the field
            type: "text",                            // The type of field you want
            required: true                           // Adds html 5 required property if true
           }]
        });
      });
    

    after that you must add in the folder lib/router.js add this code below,

    var mustBeSignedIn = function(pause) {
        AccountsEntry.signInRequired(this);
    };
    
    Router.onBeforeAction(mustBeSignedIn, {
        except: ['entrySignIn', 'entrySignUp', 'entryForgotPassword', 'layout', 'home']
    });
    

    This is la solution that I use in this moment.