Search code examples
javascripttemplatesmeteoraccountiron-router

Render a template upon sign in?


I'm using meteor with iron-router and I want the app to automatically go to the 'dashboard' template when a user successfully signs in. How do I do this? Here is my code:

javascript:

// Sign In Template
Template.signIn.events({
    'submit #signInForm': function(e, t) {
        e.preventDefault();

        var signInForm = $(e.currentTarget),
            email = trimInput(signInForm.find('.email').val().toLowerCase()),
            password = signInForm.find('.password').val();

        if (isNotEmpty(email) && isEmail(email) && isNotEmpty(password) && isValidPassword(password)) {
            Meteor.loginWithPassword(email, password, function(err) {
                if (err) {
                    Session.set('alert', 'We\'re sorry but these credentials are not valid.');
                } else {
                    Sesson.set('alert', 'Welcome back New Meteorite!');


                }
            });
        }
        return false;
    },
});

Solution

  • The Accounts Entry package from atmosphere can help you do that and more in a visually appealing and seamless way.

    As the documentation describes with an example:

    Meteor.startup(function() {
      return AccountsEntry.config({
        logo: 'logo.png',
        privacyUrl: '/privacy-policy',
        termsUrl: '/terms-of-use',
        homeRoute: '/',
        dashboardRoute: '/dashboard', // you have an autoconfigured dashboard route
        profileRoute: 'profile',
        passwordSignupFields: 'EMAIL_ONLY',
        showSignupCode: true
      });
    });
    

    And in order to protect your routes, you simply configure:

    Route.map(function() {
      this.route('createPayment', {
        path: '/create/payment',
        before: function() {
          return AccountsEntry.signInRequired(this);
        }
      });
    });