Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Meteor enroll template

In my application I want to seed the database with users and send them an enrollment link to activate their account (and choose a password). I also want them to verify/change some profile data.

On the server I seed the database like this:

Meteor.startup(function () {
  if(Meteor.users.find().count() === 0) {
    var user_id = Accounts.createUser({ email: '[email protected]', profile: { some: 'profile' } });
    Accounts.sendEnrollmentEmail(user_id);
  }
})

The enrollment link is sent as expected, but I want to create a custom template for when the url in the email is clicked. Preferably handled by iron-router. (Not using the accounts-ui package).

I tried things like redirecting the user to a custom route like this:

var doneCallback, token;

Accounts.onEnrollmentLink(function (token, done) {
  doneCallback = done;
  token = token;
  Router.go('MemberEnroll')
});

which is not working (it changes the url but not rendering my template)

I also tried to change the enroll URL on the server like this:

Accounts.urls.enrollAccount = function (token) {
  return Meteor.absoluteUrl('members/enroll/' + token);
};

But when I do this, the Accounts.onEnrollmentLink callback does not fire. Also, changing the URL is not documented so I'm not sure its a good practice at all.

Any help is appreciated.

like image 200
Sander Garretsen Avatar asked Dec 11 '22 02:12

Sander Garretsen


1 Answers

In my application I'm doing like this

        this.route('enroll', {
            path: '/enroll-account/:token',
            template: 'enroll_page',
            onBeforeAction: function() {
                Meteor.logout();
                Session.set('_resetPasswordToken', this.params.token);
                this.subscribe('enrolledUser', this.params.token).wait();
            },
            data: function() {
                if(this.ready()){
                    return {
                        enrolledUser: Meteor.users.findOne()
                    }
                }
            }
       })

As enrollment url is like this

http://www.yoursite.com/enroll-account/hkhk32434kh42hjkhk43

when users click on the link they will redirect to this template and you can render your template

In my publication

Meteor.publish('enrolledUser', function(token) {
    return Meteor.users.find({"services.password.reset.token": token});
});

After taking the password from the user

Accounts.resetPassword(token, creds.password,function(e,r){
  if(e){
    alert("Sorry we could not reset your password. Please try again.");
  }else{
    alert("Logged In");
    Router.go('/');
   }
 })

enroll link

Accounts.urls.enrollAccount = function (token) {
  return Meteor.absoluteUrl('enroll-account/' + token);
};
like image 130
Sasikanth Avatar answered Jan 08 '23 16:01

Sasikanth