Flow Router add-on for Community Packages Accounts
Community Packages Accounts is a suite of packages for the Meteor.js platform. It provides highly customizable user accounts UI templates for many different front-end frameworks. At the moment it includes forms for sign in, sign up, forgot password, reset password, change password, enroll account, and link or remove of many 3rd party services.
This package is an optional add-on for integration with Flow Router Extra. It uses FlowRouter's built-in this.render() for layout rendering.
Prerequisites
meteor add communitypackages:flow-routing ostrio:flow-router-extra
Configuration
Before you configure routes for User Accounts with Flow Router, you will need to make sure you have set a few default configuration items.
Assuming you have a main layout that looks like this:
<template name="myLayout"> <div class="nav"> </div> <div class="content"> </div> <footer> </footer> </template>
You would configure this package to use it like this:
1AccountsTemplates.configure({ 2 defaultTemplate: 'myCustomFullPageAtForm', 3 defaultLayout: 'myLayout', 4 defaultLayoutRegions: { 5 nav: 'myNav', 6 footer: 'myFooter' 7 }, 8 defaultContentRegion: 'main' 9});
If you don't have extra content regions (nav, footer, etc) you should pass an empty object to defaultLayoutRegions key of the config.
1AccountsTemplates.configure({ 2 defaultLayout: 'myLayout', 3 defaultLayoutRegions: {}, 4 defaultContentRegion: 'main' 5});
communitypackages:flow-routing uses the internal fullPageAtForm built-in template for its forms by default. You can override it on a per-route basis (see below) or replace it with defaultTemplate: field as above (templates specified in route config will still take precedence). Omit defaultTemplate (or set to an empty string) to use the fullPageAtForm template built-in to your useraccounts UI package (ex material).
NOTE: The above configs must load BEFORE your AccountsTemplates routes are defined (next section).
Routes
There are no routes provided by default, but you can easily configure routes for sign in, sign up, forgot password, reset password, change password, enroll account using AccountsTemplates.configureRoute.
The simplest way is to make the call passing in only a route code (available route codes are: signIn, signUp, changePwd, forgotPwd, resetPwd, enrollAccount).
This will set up the sign in route with a full-page form at /sign-in:
1AccountsTemplates.configureRoute('signIn');
You can also pass in more options to adapt it to your needs with:
1AccountsTemplates.configureRoute(route_code, options);
The following is a complete example of a custom route configuration:
1// routes.js 2 3AccountsTemplates.configureRoute('signIn', { 4 name: 'signin', 5 path: '/login', 6 template: 'myLogin', 7 layoutTemplate: 'myLayout', 8 layoutRegions: { 9 nav: 'customNav', 10 footer: 'customFooter' 11 }, 12 contentRegion: 'main', 13 redirect: '/user-profile' 14});
All options are passed to FlowRouter.route() which then creates a new custom route (see the official Flow Router Extra documentation for more details).
The redirect field permits to specify where to redirect the user after successful form submit. Actually, redirect can be a function so that, for example, the following:
1AccountsTemplates.configureRoute('signIn', { 2 redirect: function(){ 3 var user = Meteor.user(); 4 if (user) 5 FlowRouter.go('/user/' + user._id); 6 } 7});
Default values for all fields are as follows:
| Action | route_code | Route Name | Route Path | Template | Redirect after Timeout |
|---|---|---|---|---|---|
| change password | changePwd | atChangePwd | /change-password | fullPageAtForm | |
| enroll account | enrollAccount | atEnrollAccount | /enroll-account | fullPageAtForm | X |
| forgot password | forgotPwd | atForgotPwd | /forgot-password | fullPageAtForm | X |
| reset password | resetPwd | atResetPwd | /reset-password | fullPageAtForm | X |
| sign in | signIn | atSignIn | /sign-in | fullPageAtForm | |
| sign up | signUp | atSignUp | /sign-up | fullPageAtForm | |
| verify email | verifyEmail | atVerifyEmail | /verify-email | fullPageAtForm | X |
| resend verification email | resendVerificationEmail | atResendVerificationEmail | /send-again | fullPageAtForm |
Content Protection
If you want to protect a route by making sure a user is signed in, you can add the AccountsTemplates.ensureSignedIn check in your route's enter triggers like this:
1FlowRouter.route('/private', { 2 triggersEnter: [AccountsTemplates.ensureSignedIn], 3 action: function() { 4 this.render(...); 5 } 6});
If the user isn't signed in, they will be redirected to your sign in route and then redirected back to the original route after they have successfully signed in.
Or if you want to protect ALL routes in your app:
1FlowRouter.triggers.enter([AccountsTemplates.ensureSignedIn]);
Migration from useraccounts:flow-routing
If you are migrating from useraccounts:flow-routing v1.x, the following changes are required:
-
Replace Meteor packages:
meteor remove useraccounts:flow-routing kadira:flow-router kadira:blaze-layout meteor add communitypackages:flow-routing ostrio:flow-router-extra -
No code changes are needed for route configuration -- the API is identical.