Last active
August 29, 2015 14:08
-
-
Save evenfrost/7e0d42807d35198dbad7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Router.configure({ | |
layoutTemplate: 'layout', | |
loadingTemplate: 'loading', | |
notFoundTemplate: 'notFound', | |
waitOn: function () { | |
return [ | |
Meteor.subscribe('users'), | |
Meteor.subscribe('companiesWithUsers') | |
]; | |
} | |
}); | |
Router.onBeforeAction(function () { | |
var currentUser = Meteor.user(); | |
if (!currentUser) { | |
this.redirect('/login'); | |
} else { | |
if (currentUser.roles.indexOf('admin') === -1) { | |
if (currentUser.companyId) { | |
this.redirect('/companies/' + currentUser.companyId); | |
} else { | |
this.render('notFound'); | |
} | |
} | |
} | |
this.next(); | |
}, { | |
except: ['enroll'] | |
}); | |
/** | |
* Main. | |
*/ | |
Router.route('/', function () { | |
this.redirect('/companies'); | |
}); | |
Router.route('/companies', function () { | |
Session.set('companyId', null); | |
this.render('companyList'); | |
}, { | |
name: 'home' | |
/*waitOn: function () { | |
return Meteor.subscribe('companiesWithUsers') | |
}*/ | |
}); | |
Router.route('/companies/:_id', function () { | |
var _id = this.params._id, | |
company = Companies.findOne({ _id: _id }); | |
if (company) { | |
Session.set('companyId', _id); | |
this.render('company', { | |
data: function () { | |
return company; | |
} | |
}); | |
} else { | |
this.render('notFound'); | |
} | |
}, { | |
name: 'company' | |
/*waitOn: function () { | |
return Meteor.subscribe('companiesWithUsers', this.params._id); | |
}*/ | |
}); | |
Router.route('/companies/:_id/new', function () { | |
var companyId = this.params._id; | |
this.render('teamNew', { | |
data: function () { | |
return { | |
companyId: companyId | |
}; | |
} | |
}); | |
}, { | |
name: 'team.new' | |
}); | |
/** | |
* Authentication. | |
*/ | |
Router.route('/login', function () { | |
if (Meteor.userId()) { | |
this.redirect('home'); | |
} | |
this.render('login'); | |
}); | |
Router.route('/logout', function () { | |
Meteor.logout(); | |
}); | |
Router.route('/new', function () { | |
this.render('companyNew'); | |
}, { | |
name: 'company.new' | |
}); | |
/** | |
* Email enrollment. | |
*/ | |
Router.route('/enroll/:token', function () { | |
var token = this.params.token, | |
self = this; | |
Meteor.call('confirmToken', token, function (error, userId) { | |
if (error || !userId) { | |
self.render('notFound'); | |
} else { | |
self.render('passwordSet', { | |
data: function () { | |
return { userId: userId }; | |
} | |
}); | |
} | |
}); | |
}, { name: 'enroll' }); | |
Router.map(function () { | |
this.route('404', { | |
path : '*', | |
onBeforeAction : function () { | |
Router.go('home'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment