From 03b3e57a44da228bca7296bd752e26447956e1d6 Mon Sep 17 00:00:00 2001 From: Erick Guan Date: Thu, 20 Apr 2017 17:17:24 +0200 Subject: [PATCH] FEATURE: login by a link from email Co-authored-by: tgxworld --- .../controllers/forgot-password.js.es6 | 80 +++++----- .../templates/modal/forgot-password.hbs | 14 +- app/controllers/session_controller.rb | 85 ++++++++--- app/controllers/users_controller.rb | 38 ++++- app/mailers/user_notifications.rb | 71 ++++++--- app/views/session/email_login.html.erb | 17 +++ config/locales/client.en.yml | 10 ++ config/locales/server.en.yml | 17 +++ config/routes.rb | 2 + config/site_settings.yml | 5 +- ...enable_local_logins_via_email_validator.rb | 14 ++ ...e_local_logins_via_email_validator_spec.rb | 47 ++++++ spec/controllers/session_controller_spec.rb | 27 ++-- spec/mailers/user_notifications_spec.rb | 22 +++ spec/requests/session_controller_spec.rb | 141 ++++++++++++++++++ spec/requests/users_controller_spec.rb | 61 ++++++++ .../acceptance/forgot-password-test.js.es6 | 90 +++++++++++ 17 files changed, 640 insertions(+), 101 deletions(-) create mode 100644 app/views/session/email_login.html.erb create mode 100644 lib/validators/enable_local_logins_via_email_validator.rb create mode 100644 spec/components/validators/enable_local_logins_via_email_validator_spec.rb create mode 100644 spec/requests/session_controller_spec.rb create mode 100644 test/javascripts/acceptance/forgot-password-test.js.es6 diff --git a/app/assets/javascripts/discourse/controllers/forgot-password.js.es6 b/app/assets/javascripts/discourse/controllers/forgot-password.js.es6 index 4311368186c..7e385fb3022 100644 --- a/app/assets/javascripts/discourse/controllers/forgot-password.js.es6 +++ b/app/assets/javascripts/discourse/controllers/forgot-password.js.es6 @@ -20,48 +20,54 @@ export default Ember.Controller.extend(ModalFunctionality, { }, actions: { - submit() { - if (this.get('submitDisabled')) return false; - - this.set('disabled', true); - - ajax('/session/forgot_password', { - data: { login: this.get('accountEmailOrUsername').trim() }, - type: 'POST' - }).then(data => { - const escaped = escapeExpression(this.get('accountEmailOrUsername')); - const isEmail = this.get('accountEmailOrUsername').match(/@/); - let key = 'forgot_password.complete_' + (isEmail ? 'email' : 'username'); - let extraClass; - - if (data.user_found === true) { - key += '_found'; - this.set('accountEmailOrUsername', ''); - this.set('offerHelp', I18n.t(key, {email: escaped, username: escaped})); - } else { - if (data.user_found === false) { - key += '_not_found'; - extraClass = 'error'; - } - - this.flash(I18n.t(key, {email: escaped, username: escaped}), extraClass); - } - }).catch(e => { - this.flash(extractError(e), 'error'); - }).finally(() => { - setTimeout(() => this.set('disabled', false), 1000); - }); - - return false; - }, - ok() { this.send('closeModal'); }, help() { this.setProperties({ offerHelp: I18n.t('forgot_password.help'), helpSeen: true }); - } - } + }, + resetPassword() { + return this._submit('/session/forgot_password', 'forgot_password.complete'); + }, + + emailLogin() { + return this._submit('/u/email-login', 'email_login.complete'); + } + }, + + _submit(route, translationKey) { + if (this.get('submitDisabled')) return false; + this.set('disabled', true); + + ajax(route, { + data: { login: this.get('accountEmailOrUsername').trim() }, + type: 'POST' + }).then(data => { + const escaped = escapeExpression(this.get('accountEmailOrUsername')); + const isEmail = this.get('accountEmailOrUsername').match(/@/); + let key = `${translationKey}_${isEmail ? 'email' : 'username'}`; + let extraClass; + + if (data.user_found === true) { + key += '_found'; + this.set('accountEmailOrUsername', ''); + this.set('offerHelp', I18n.t(key, { email: escaped, username: escaped })); + } else { + if (data.user_found === false) { + key += '_not_found'; + extraClass = 'error'; + } + + this.flash(I18n.t(key, { email: escaped, username: escaped }), extraClass); + } + }).catch(e => { + this.flash(extractError(e), 'error'); + }).finally(() => { + this.set('disabled', false); + }); + + return false; + }, }); diff --git a/app/assets/javascripts/discourse/templates/modal/forgot-password.hbs b/app/assets/javascripts/discourse/templates/modal/forgot-password.hbs index c739f771979..8b42f88739d 100644 --- a/app/assets/javascripts/discourse/templates/modal/forgot-password.hbs +++ b/app/assets/javascripts/discourse/templates/modal/forgot-password.hbs @@ -9,10 +9,16 @@ {{/d-modal-body}}