From 66241dff91641c99a5dae62811a689b9d4abc71c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cla=CC=81udio=20Sa=CC=81=20de=20Castro?= Date: Thu, 4 Jul 2019 14:44:23 -0300 Subject: [PATCH 1/2] Solution to allow for the implementation of two-factor authentication using useraccounts. The changes include: the ability to specify the states where custom input fields should be visible (new 'visible' field definition property); and a way to use a custom function for processing login (new 'loginFunc' config property). --- lib/client.js | 4 +++- lib/core.js | 2 ++ lib/templates_helpers/at_pwd_form.js | 10 +++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/client.js b/lib/client.js index 31c9db7..90331fb 100644 --- a/lib/client.js +++ b/lib/client.js @@ -280,7 +280,9 @@ AT.prototype._init = function() { } break; default: - field.visible = ["signUp"]; + if (!field.visible) { + field.visible = ["signUp"]; + } } // Validation diff --git a/lib/core.js b/lib/core.js index 1c7bc07..224318c 100644 --- a/lib/core.js +++ b/lib/core.js @@ -99,6 +99,7 @@ CONFIG_PAT = { overrideLoginErrors: Match.Optional(Boolean), sendVerificationEmail: Match.Optional(Boolean), socialLoginStyle: Match.Optional(Match.OneOf("popup", "redirect")), + loginFunc: Match.Optional(Function), // Appearance defaultLayout: Match.Optional(String), @@ -170,6 +171,7 @@ FIELD_PAT = { re: Match.Optional(RegExp), func: Match.Optional(Match.Where(_.isFunction)), errStr: Match.Optional(String), + visible: Match.Optional([String]), // Client-side Validation continuousValidation: Match.Optional(Boolean), diff --git a/lib/templates_helpers/at_pwd_form.js b/lib/templates_helpers/at_pwd_form.js index 2f8d53c..960bb86 100644 --- a/lib/templates_helpers/at_pwd_form.js +++ b/lib/templates_helpers/at_pwd_form.js @@ -169,10 +169,14 @@ AT.prototype.atPwdFormEvents = { return; } - - return Meteor.loginWithPassword(loginSelector, password, function(error) { + if (AccountsTemplates.options.loginFunc) { + return AccountsTemplates.options.loginFunc(loginSelector, password, formData, state); + } + else { + return Meteor.loginWithPassword(loginSelector, password, function (error) { AccountsTemplates.submitCallback(error, state); - }); + }); + } } // ------- From f4f72caad3bbb2bd1726262202d2a6ac112126a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cla=CC=81udio=20Sa=CC=81=20de=20Castro?= Date: Wed, 7 Aug 2019 15:29:16 -0300 Subject: [PATCH 2/2] Enhancements to solution to allow for the implementation of two-factor authentication using useraccounts: simplification of loginFunc prototype (removed 'state' param); added support for custom reset password function (resetPasswordFunc) so two-factor authentication can also be used when reseting the password. --- lib/core.js | 1 + lib/templates_helpers/at_pwd_form.js | 30 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/core.js b/lib/core.js index 224318c..a78ab4e 100644 --- a/lib/core.js +++ b/lib/core.js @@ -100,6 +100,7 @@ CONFIG_PAT = { sendVerificationEmail: Match.Optional(Boolean), socialLoginStyle: Match.Optional(Match.OneOf("popup", "redirect")), loginFunc: Match.Optional(Function), + resetPasswordFunc: Match.Optional(Function), // Appearance defaultLayout: Match.Optional(String), diff --git a/lib/templates_helpers/at_pwd_form.js b/lib/templates_helpers/at_pwd_form.js index 960bb86..f93a812 100644 --- a/lib/templates_helpers/at_pwd_form.js +++ b/lib/templates_helpers/at_pwd_form.js @@ -170,7 +170,7 @@ AT.prototype.atPwdFormEvents = { } if (AccountsTemplates.options.loginFunc) { - return AccountsTemplates.options.loginFunc(loginSelector, password, formData, state); + return AccountsTemplates.options.loginFunc(loginSelector, password, formData); } else { return Meteor.loginWithPassword(loginSelector, password, function (error) { @@ -289,18 +289,24 @@ AT.prototype.atPwdFormEvents = { //-------------------------------- if (state === "resetPwd" || state === "enrollAccount") { var paramToken = AccountsTemplates.getparamToken(); - return Accounts.resetPassword(paramToken, password, function(error) { - AccountsTemplates.submitCallback(error, state, function(){ - var pwd_field_id; - if (state === "resetPwd") - AccountsTemplates.state.form.set("result", AccountsTemplates.texts.info.pwdReset); - else // Enroll Account - AccountsTemplates.state.form.set("result", AccountsTemplates.texts.info.pwdSet); - t.$("#at-field-password").val(""); - if (AccountsTemplates.options.confirmPassword) - t.$("#at-field-password_again").val(""); + + if (state === "resetPwd" && AccountsTemplates.options.resetPasswordFunc) { + return AccountsTemplates.options.resetPasswordFunc(paramToken, password, formData); + } + else { + return Accounts.resetPassword(paramToken, password, function(error) { + AccountsTemplates.submitCallback(error, state, function(){ + var pwd_field_id; + if (state === "resetPwd") + AccountsTemplates.state.form.set("result", AccountsTemplates.texts.info.pwdReset); + else // Enroll Account + AccountsTemplates.state.form.set("result", AccountsTemplates.texts.info.pwdSet); + t.$("#at-field-password").val(""); + if (AccountsTemplates.options.confirmPassword) + t.$("#at-field-password_again").val(""); + }); }); - }); + } } //----------------