2016-03-29 01:28:53 +02:00
|
|
|
import { Accounts } from 'meteor/accounts-base';
|
2016-12-18 20:53:19 +01:00
|
|
|
import {
|
|
|
|
redirect,
|
|
|
|
validatePassword,
|
|
|
|
validateEmail,
|
|
|
|
validateUsername,
|
|
|
|
} from './helpers.js';
|
2016-03-28 22:34:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Accounts UI
|
|
|
|
* @namespace
|
|
|
|
* @memberOf Accounts
|
|
|
|
*/
|
|
|
|
Accounts.ui = {};
|
|
|
|
|
|
|
|
Accounts.ui._options = {
|
2016-03-31 12:06:46 +02:00
|
|
|
requestPermissions: [],
|
2016-03-28 22:34:50 +02:00
|
|
|
requestOfflineToken: {},
|
|
|
|
forceApprovalPrompt: {},
|
2016-04-05 01:38:13 +02:00
|
|
|
requireEmailVerification: false,
|
2016-04-03 17:37:39 +02:00
|
|
|
passwordSignupFields: 'EMAIL_ONLY_NO_PASSWORD',
|
2016-04-02 13:04:26 +02:00
|
|
|
minimumPasswordLength: 7,
|
2016-03-31 19:29:06 +02:00
|
|
|
loginPath: '/',
|
|
|
|
signUpPath: null,
|
|
|
|
resetPasswordPath: null,
|
2016-04-02 15:34:30 +02:00
|
|
|
profilePath: '/',
|
2016-03-31 19:29:06 +02:00
|
|
|
changePasswordPath: null,
|
2016-03-31 00:10:45 +02:00
|
|
|
homeRoutePath: '/',
|
2016-03-29 04:03:32 +02:00
|
|
|
onSubmitHook: () => {},
|
2016-03-31 19:29:06 +02:00
|
|
|
onPreSignUpHook: () => new Promise(resolve => resolve()),
|
|
|
|
onPostSignUpHook: () => {},
|
2016-03-28 22:34:50 +02:00
|
|
|
onEnrollAccountHook: () => redirect(`${Accounts.ui._options.loginPath}`),
|
2017-01-07 11:10:52 +01:00
|
|
|
onResetPasswordHook: () => redirect(`${Accounts.ui._options.resetPasswordPath}`),
|
2016-04-02 15:34:30 +02:00
|
|
|
onVerifyEmailHook: () => redirect(`${Accounts.ui._options.profilePath}`),
|
2016-11-24 08:25:29 +00:00
|
|
|
onSignedInHook: () => redirect(`${Accounts.ui._options.homeRoutePath}`),
|
2016-12-18 20:53:19 +01:00
|
|
|
onSignedOutHook: () => redirect(`${Accounts.ui._options.homeRoutePath}`),
|
|
|
|
emailPattern: new RegExp('[^@]+@[^@\.]{2,}\.[^\.@]+'),
|
2016-03-28 22:34:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-03-31 00:10:45 +02:00
|
|
|
* @summary Configure the behavior of [`<Accounts.ui.LoginForm />`](#react-accounts-ui).
|
|
|
|
* @anywhere
|
2016-03-28 22:34:50 +02:00
|
|
|
* @param {Object} options
|
|
|
|
* @param {Object} options.requestPermissions Which [permissions](#requestpermissions) to request from the user for each external service.
|
|
|
|
* @param {Object} options.requestOfflineToken To ask the user for permission to act on their behalf when offline, map the relevant external service to `true`. Currently only supported with Google. See [Meteor.loginWithExternalService](#meteor_loginwithexternalservice) for more details.
|
|
|
|
* @param {Object} options.forceApprovalPrompt If true, forces the user to approve the app's permissions, even if previously approved. Currently only supported with Google.
|
2016-03-31 00:10:45 +02:00
|
|
|
* @param {String} options.passwordSignupFields Which fields to display in the user creation form. One of '`USERNAME_AND_EMAIL`', '`USERNAME_AND_OPTIONAL_EMAIL`', '`USERNAME_ONLY`', '`EMAIL_ONLY`', or '`NO_PASSWORD`' (default).
|
2016-03-28 22:34:50 +02:00
|
|
|
*/
|
|
|
|
Accounts.ui.config = function(options) {
|
|
|
|
// validate options keys
|
|
|
|
const VALID_KEYS = [
|
|
|
|
'passwordSignupFields',
|
|
|
|
'requestPermissions',
|
|
|
|
'requestOfflineToken',
|
2016-03-29 04:37:10 +02:00
|
|
|
'forbidClientAccountCreation',
|
2016-04-05 01:38:13 +02:00
|
|
|
'requireEmailVerification',
|
2016-04-02 14:20:43 +02:00
|
|
|
'minimumPasswordLength',
|
2016-03-28 22:34:50 +02:00
|
|
|
'loginPath',
|
2016-03-31 19:29:06 +02:00
|
|
|
'signUpPath',
|
|
|
|
'resetPasswordPath',
|
|
|
|
'profilePath',
|
|
|
|
'changePasswordPath',
|
|
|
|
'homeRoutePath',
|
2016-03-28 22:34:50 +02:00
|
|
|
'onSubmitHook',
|
2016-03-31 19:29:06 +02:00
|
|
|
'onPreSignUpHook',
|
|
|
|
'onPostSignUpHook',
|
2016-03-28 22:34:50 +02:00
|
|
|
'onEnrollAccountHook',
|
|
|
|
'onResetPasswordHook',
|
|
|
|
'onVerifyEmailHook',
|
|
|
|
'onSignedInHook',
|
2016-12-18 20:53:19 +01:00
|
|
|
'onSignedOutHook',
|
|
|
|
'validateField',
|
|
|
|
'emailPattern',
|
2016-03-28 22:34:50 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
_.each(_.keys(options), function (key) {
|
|
|
|
if (!_.contains(VALID_KEYS, key))
|
|
|
|
throw new Error("Accounts.ui.config: Invalid key: " + key);
|
|
|
|
});
|
|
|
|
|
2016-12-18 20:53:19 +01:00
|
|
|
// Deal with `passwordSignupFields`
|
2016-03-28 22:34:50 +02:00
|
|
|
if (options.passwordSignupFields) {
|
|
|
|
if (_.contains([
|
|
|
|
"USERNAME_AND_EMAIL",
|
|
|
|
"USERNAME_AND_OPTIONAL_EMAIL",
|
|
|
|
"USERNAME_ONLY",
|
2016-03-29 00:34:28 +02:00
|
|
|
"EMAIL_ONLY",
|
2016-03-31 15:57:28 +02:00
|
|
|
"EMAIL_ONLY_NO_PASSWORD",
|
|
|
|
"USERNAME_AND_EMAIL_NO_PASSWORD"
|
2016-03-28 22:34:50 +02:00
|
|
|
], options.passwordSignupFields)) {
|
2016-03-31 01:01:56 +02:00
|
|
|
Accounts.ui._options.passwordSignupFields = options.passwordSignupFields;
|
2016-03-28 22:34:50 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error("Accounts.ui.config: Invalid option for `passwordSignupFields`: " + options.passwordSignupFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:53:19 +01:00
|
|
|
// Deal with `requestPermissions`
|
2016-03-28 22:34:50 +02:00
|
|
|
if (options.requestPermissions) {
|
|
|
|
_.each(options.requestPermissions, function (scope, service) {
|
|
|
|
if (Accounts.ui._options.requestPermissions[service]) {
|
|
|
|
throw new Error("Accounts.ui.config: Can't set `requestPermissions` more than once for " + service);
|
|
|
|
}
|
|
|
|
else if (!(scope instanceof Array)) {
|
|
|
|
throw new Error("Accounts.ui.config: Value for `requestPermissions` must be an array");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Accounts.ui._options.requestPermissions[service] = scope;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:53:19 +01:00
|
|
|
// Deal with `requestOfflineToken`
|
2016-03-28 22:34:50 +02:00
|
|
|
if (options.requestOfflineToken) {
|
|
|
|
_.each(options.requestOfflineToken, function (value, service) {
|
|
|
|
if (service !== 'google')
|
|
|
|
throw new Error("Accounts.ui.config: `requestOfflineToken` only supported for Google login at the moment.");
|
|
|
|
|
|
|
|
if (Accounts.ui._options.requestOfflineToken[service]) {
|
|
|
|
throw new Error("Accounts.ui.config: Can't set `requestOfflineToken` more than once for " + service);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Accounts.ui._options.requestOfflineToken[service] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:53:19 +01:00
|
|
|
// Deal with `forceApprovalPrompt`
|
2016-03-28 22:34:50 +02:00
|
|
|
if (options.forceApprovalPrompt) {
|
|
|
|
_.each(options.forceApprovalPrompt, function (value, service) {
|
|
|
|
if (service !== 'google')
|
|
|
|
throw new Error("Accounts.ui.config: `forceApprovalPrompt` only supported for Google login at the moment.");
|
|
|
|
|
|
|
|
if (Accounts.ui._options.forceApprovalPrompt[service]) {
|
|
|
|
throw new Error("Accounts.ui.config: Can't set `forceApprovalPrompt` more than once for " + service);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Accounts.ui._options.forceApprovalPrompt[service] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:53:19 +01:00
|
|
|
// Deal with `requireEmailVerification`
|
2016-04-05 01:38:13 +02:00
|
|
|
if (options.requireEmailVerification) {
|
|
|
|
if (typeof options.requireEmailVerification != 'boolean') {
|
|
|
|
throw new Error(`Accounts.ui.config: "requireEmailVerification" not a boolean`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Accounts.ui._options.requireEmailVerification = options.requireEmailVerification;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:53:19 +01:00
|
|
|
// Deal with `minimumPasswordLength`
|
2016-04-02 14:20:43 +02:00
|
|
|
if (options.minimumPasswordLength) {
|
|
|
|
if (typeof options.minimumPasswordLength != 'number') {
|
|
|
|
throw new Error(`Accounts.ui.config: "minimumPasswordLength" not a number`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Accounts.ui._options.minimumPasswordLength = options.minimumPasswordLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:53:19 +01:00
|
|
|
// Deal with the hooks.
|
|
|
|
for (let hook of [
|
|
|
|
'onSubmitHook',
|
|
|
|
'onPreSignUpHook',
|
|
|
|
'onPostSignUpHook',
|
|
|
|
]) {
|
2016-03-28 22:34:50 +02:00
|
|
|
if (options[hook]) {
|
|
|
|
if (typeof options[hook] != 'function') {
|
|
|
|
throw new Error(`Accounts.ui.config: "${hook}" not a function`);
|
|
|
|
}
|
|
|
|
else {
|
2016-03-29 04:03:32 +02:00
|
|
|
Accounts.ui._options[hook] = options[hook];
|
2016-03-28 22:34:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:53:19 +01:00
|
|
|
// Deal with pattern.
|
|
|
|
for (let hook of [
|
|
|
|
'emailPattern',
|
|
|
|
]) {
|
|
|
|
if (options[hook]) {
|
|
|
|
if (!(options[hook] instanceof RegExp)) {
|
|
|
|
throw new Error(`Accounts.ui.config: "${hook}" not a Regular Expression`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Accounts.ui._options[hook] = options[hook];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 14:20:43 +02:00
|
|
|
// deal with the paths.
|
|
|
|
for (let path of [
|
|
|
|
'loginPath',
|
|
|
|
'signUpPath',
|
|
|
|
'resetPasswordPath',
|
|
|
|
'profilePath',
|
|
|
|
'changePasswordPath',
|
|
|
|
'homeRoutePath'
|
|
|
|
]) {
|
2016-12-18 15:13:14 +01:00
|
|
|
if (typeof options[path] !== 'undefined') {
|
|
|
|
if (options[path] !== null && typeof options[path] !== 'string') {
|
|
|
|
throw new Error(`Accounts.ui.config: ${path} is not a string or null`);
|
2016-04-02 14:20:43 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Accounts.ui._options[path] = options[path];
|
|
|
|
}
|
2016-03-28 22:34:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deal with redirect hooks.
|
|
|
|
for (let hook of [
|
|
|
|
'onEnrollAccountHook',
|
|
|
|
'onResetPasswordHook',
|
|
|
|
'onVerifyEmailHook',
|
|
|
|
'onSignedInHook',
|
|
|
|
'onSignedOutHook']) {
|
|
|
|
if (options[hook]) {
|
|
|
|
if (typeof options[hook] == 'function') {
|
|
|
|
Accounts.ui._options[hook] = options[hook];
|
|
|
|
}
|
|
|
|
else if (typeof options[hook] == 'string') {
|
2016-03-29 04:03:32 +02:00
|
|
|
Accounts.ui._options[hook] = () => redirect(options[hook]);
|
2016-03-28 22:34:50 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`Accounts.ui.config: "${hook}" not a function or an absolute or relative path`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Accounts;
|