Last active
August 29, 2015 14:26
-
-
Save themeteorchef/38be3aa2ceb14b84365b to your computer and use it in GitHub Desktop.
Auth in Flow Router?
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
| 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(); | |
| } | |
| } | |
| }); |
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
| <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> |
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
| 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' ); | |
| } | |
| } | |
| } | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does work but seems like a lot of heavy lifting. Any thoughts? Improvements?