meteor - iron:router beforeAction login AND data: function? -


i'm using standard iron:router pattern ensuring user authenticated before accessing route:

authenticatedcontroller = routecontroller.extend({   onbeforeaction: function(){     if ( meteor.user() ){       var route = router.current().route.getname();       this.next();     } else this.render('login'); }) 

this works unparameterized routes example:

router.route("profile",{   name: "profile",   controller: 'authenticatedcontroller' }); 

when try extend pattern parameterized route, example:

router.route('/foo/:id',{   name: 'foo',   controller: 'authenticatedcontroller',   data: function(){ return mycollection.findone({ _id: this.params.id }); } } }); 
  1. it works if user logged in
  2. i 404 page if user not logged in

it seems beforeaction runs after data function. since mycollection doesn't publish documents until user logged in iron:router decides route doesn't exist.

the time want 404 if collection search doesn't return anything.

i use following pattern works me:

//i create hook authentication var userauthhook = function() {     if (meteor.userid()) {         this.next();     } else {         this.redirect('/login');     } };  //i apply hook routes except login router.onbeforeaction(userauthhook, {     except: ['login'] });  //if want 404 error, put in onafteraction callback router.route('/message/:_id', {     name: 'message',     waiton: function() {         return [...];     },     onafteraction: function() {         //we wait 'waiton' data ready         if(this.ready()) {             var message = messages.findone({_id: this.params._id});             if(!message) {                 //if there no corresponding message render 404 template                 this.render('error404');             }         }     } }); 

i hope work too.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -