useraccounts:flow-routing

v1.15.0Published 3 years ago

Compatibility

Compatible with Meteor 2.4

This repository provides versions for the package useraccounts:flow-routing that are compatible with latest Meteor. This is necessary because the author is not maintaining package anymore.

Changes

  • v1.15.0
    • api.versionsFrom on Package.onUse was changed from 1.0.3 to 2.4.

Flow Router add-on for User Accounts

User 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 and either Blaze Layout or React Layout.

Blaze Configuration

Firstly, please ensure that your app depends upon the Blaze Layout package.

Then, 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">
    {{> Template.dynamic template=nav}}
  </div>

  <div class="content">
    {{> Template.dynamic template=main}}
  </div>

  <footer>
    {{> Template.dynamic template=footer}}
  </footer>

</template>

You would configure this package to use it like this:

1AccountsTemplates.configure({
2    defaultLayoutType: 'blaze', // Optional, the default is 'blaze'
3    defaultTemplate: 'myCustomFullPageAtForm',
4    defaultLayout: 'myLayout',
5    defaultLayoutRegions: {
6        nav: 'myNav',
7        footer: 'myFooter'
8    },
9    defaultContentRegion: 'main'
10});

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});

useraccounts:flow-routing uses the internal useraccounts fullPageAtForm is the built-in template useraccounts uses by default for its forms. 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).

React Configuration

Firstly, please ensure that your app depends upon the React Layout and the Blaze Layout packages. User Accounts currents only renders Blaze templates. In order to use User Accounts with React we rely on the Blaze To React package to render the User Accounts templates.

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 following and you have <Nav /> and <Footer /> as your default nav/footer components:

1MainLayout = React.createClass({
2  render() {
3    return (
4      <div>
5        <header>
6          {this.props.nav || <Nav />}
7        </header>
8        <main>
9          {this.props.main}
10        </main>
11        <footer>
12          {this.props.footer || <Footer />}
13        </footer>
14      </div>
15    );
16  }
17});

You would then configure this package to use it like this:

1AccountsTemplates.configure({
2  defaultLayoutType: 'blaze-to-react',
3  defaultTemplate: 'fullPageAtForm',  // default
4  defaultLayout: MainLayout,
5  defaultLayoutRegions: {
6    nav: <Nav />,
7    footer: <Footer />
8  },
9  defaultContentRegion: 'main'
10});

If you don't have extra content regions (nav, footer, etc) you should pass an empty object to the defaultLayoutRegions key of the config.

1AccountsTemplates.configure({
2	defaultLayoutType: 'blaze-to-react',
3  defaultTemplate: 'myCustomFullPageAtForm',
4  defaultLayout: MainLayout,
5  defaultLayoutRegions: {},
6  defaultContentRegion: 'main'
7});

useraccounts:flow-routing uses fullPageAtForm for the defaultTemplate option. fullPageAtForm is the built-in Blaze template that all UserAccounts themed packages (Bootstrap, Materialize, etc.) use for their forms. You can override it on a per-route basis (see below) or replace it as shown above (templates specified in a 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).

Please note that this template must be a Blaze template. It will be rendered into your React layout using Blaze To React.

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:

Blaze
1// routes.js
2
3AccountsTemplates.configureRoute('signIn', {
4  layoutType: 'blaze',
5  name: 'signin',
6  path: '/login',
7  template: 'myLogin',
8  layoutTemplate: 'myLayout',
9  layoutRegions: {
10    nav: 'customNav',
11    footer: 'customFooter'
12  },
13  contentRegion: 'main',
14  redirect: '/user-profile'
15});
React
1// routes.jsx
2
3AccountsTemplates.configureRoute('signIn', {
4  layoutType: 'blaze-to-react',
5  name: 'signin',
6  path: '/login',
7  template: 'myLogin',
8  layoutTemplate: CustomLayout,
9  layoutRegions: {
10    nav: <CustomNav />,
11    footer: <CustomFooter />
12  },
13  contentRegion: 'main',
14  redirect: '/user-profile'
15});

All options are passed to FlowRouter.route() which then creates a new custom route (see the official Flow Router documentation here 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          Router.go('/user/' + user._id);
6    }
7});

Default values for all fields are as follows:

Actionroute_codeRoute NameRoute PathTemplateRedirect after Timeout
change passwordchangePwdatChangePwd/change-passwordfullPageAtForm
enroll accountenrollAccountatEnrollAccount/enroll-accountfullPageAtFormX
forgot passwordforgotPwdatForgotPwd/forgot-passwordfullPageAtFormX
reset passwordresetPwdatResetPwd/reset-passwordfullPageAtFormX
sign insignInatSignIn/sign-infullPageAtForm
sign upsignUpatSignUp/sign-upfullPageAtForm
verify emailverifyEmailatVerifyEmail/verify-emailfullPageAtFormX
resend verification emailresendVerificationEmailatresendVerificationEmail/send-againfullPageAtForm

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    BlazeLayout.render(...);
5    // or
6    ReactLayout.render(...);
7  }
8});

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]);