Skip to content

Instantly share code, notes, and snippets.

@themeteorchef
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save themeteorchef/38be3aa2ceb14b84365b to your computer and use it in GitHub Desktop.

Select an option

Save themeteorchef/38be3aa2ceb14b84365b to your computer and use it in GitHub Desktop.
Auth in Flow Router?
Template.applicationLayout.helpers({
loggingIn() {
return Meteor.loggingIn();
},
authenticated() {
if ( Meteor.user() ) {
console.log( "We have a user!" );
App.routes.handleUser();
} else {
console.log( "We DO NOT have a user!" );
App.routes.handleNoUser();
}
}
});
<template name="applicationLayout">
{{> bertAlert }}
{{> header }}
<div class="container">
{{#if loggingIn}}
<p>Logging in...</p>
{{else}}
{{#if authenticated}}
{{> Template.dynamic template=main }}
{{else}}
{{> Template.dynamic template=main }}
{{/if}}
{{/if}}
</div>
</template>
App = {
routes: {
public: [
"login",
"signup",
"recover-password",
"reset-password"
],
authenticated: [
"index",
"login",
"signup",
"recover-password",
"reset-password"
],
handleUser() {
// "Do this only on the pages in our authenticated array."
if ( App.routes.authenticated.indexOf( FlowRouter.current().route.name ) > -1 ) {
FlowRouter.go( '/howdy' );
// Redirect to main auth'd page. e.g. /howdy
} else {
FlowRouter.go( FlowRouter.current().path );
}
},
handleNoUser() {
// "Do this on every page BUT the ones in our public array."
if ( App.routes.public.indexOf( FlowRouter.current().route.name ) !== -1 ) {
FlowRouter.go( FlowRouter.current().path );
} else {
// Go to the login page.
FlowRouter.go( '/login' );
}
}
}
};
@themeteorchef

Copy link
Copy Markdown
Author

This does work but seems like a lot of heavy lifting. Any thoughts? Improvements?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment