From 9ad226aaa80bcccf1790029f5532dd352ac3e92b Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 19 Jan 2016 16:53:46 -0500 Subject: [PATCH] FEATURE: add email query param to login, signup, and password-reset URLs to prefill form --- .../discourse/controllers/forgot-password.js.es6 | 6 ++++++ .../javascripts/discourse/views/create-account.js.es6 | 9 +++++++-- app/assets/javascripts/discourse/views/login.js.es6 | 9 +++++++-- app/controllers/static_controller.rb | 8 +++++++- app/views/static/signup.html.erb | 0 config/routes.rb | 2 +- 6 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 app/views/static/signup.html.erb diff --git a/app/assets/javascripts/discourse/controllers/forgot-password.js.es6 b/app/assets/javascripts/discourse/controllers/forgot-password.js.es6 index 64aac27c9c5..fa958a84d09 100644 --- a/app/assets/javascripts/discourse/controllers/forgot-password.js.es6 +++ b/app/assets/javascripts/discourse/controllers/forgot-password.js.es6 @@ -7,6 +7,12 @@ export default Ember.Controller.extend(ModalFunctionality, { return Ember.isEmpty((this.get('accountEmailOrUsername') || '').trim()) || this.get('disabled'); }.property('accountEmailOrUsername', 'disabled'), + onShow: function() { + if ($.cookie('email')) { + this.set('accountEmailOrUsername', $.cookie('email')); + } + }, + actions: { submit: function() { var self = this; diff --git a/app/assets/javascripts/discourse/views/create-account.js.es6 b/app/assets/javascripts/discourse/views/create-account.js.es6 index da0e2815821..964c8f8ec5d 100644 --- a/app/assets/javascripts/discourse/views/create-account.js.es6 +++ b/app/assets/javascripts/discourse/views/create-account.js.es6 @@ -6,9 +6,14 @@ export default ModalBodyView.extend({ classNames: ['create-account'], _setup: function() { - // allows the submission the form when pressing 'ENTER' on *any* text input field - // but only when the submit button is enabled + // Allows submitting the form when pressing 'ENTER' on *any* text input field + // but only when the submit button is enabled. const createAccountController = this.get('controller'); + + if ($.cookie('email')) { + createAccountController.set('accountEmail', $.cookie('email')); + } + Em.run.schedule('afterRender', function() { $("input[type='text'], input[type='password']").keydown(function(e) { if (createAccountController.get('submitDisabled') === false && e.keyCode === 13) { diff --git a/app/assets/javascripts/discourse/views/login.js.es6 b/app/assets/javascripts/discourse/views/login.js.es6 index f2b61efb5a2..656c476bada 100644 --- a/app/assets/javascripts/discourse/views/login.js.es6 +++ b/app/assets/javascripts/discourse/views/login.js.es6 @@ -15,8 +15,13 @@ export default ModalBodyView.extend({ // Get username and password from the browser's password manager, // if it filled the hidden static login form: - loginController.set('loginName', $('#hidden-login-form input[name=username]').val()); - loginController.set('loginPassword', $('#hidden-login-form input[name=password]').val()); + var prefillUsername = $('#hidden-login-form input[name=username]').val(); + if (prefillUsername) { + loginController.set('loginName', prefillUsername); + loginController.set('loginPassword', $('#hidden-login-form input[name=password]').val()); + } else if ($.cookie('email')) { + loginController.set('loginName', $.cookie('email')); + } Em.run.schedule('afterRender', function() { $('#login-account-password, #login-account-name').keydown(function(e) { diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index c2d4d6f1761..fc2bbe09f4c 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -6,8 +6,10 @@ class StaticController < ApplicationController skip_before_filter :check_xhr, :redirect_to_login_if_required skip_before_filter :verify_authenticity_token, only: [:cdn_asset, :enter, :favicon] + PAGES_WITH_EMAIL_PARAM = ['login', 'password_reset', 'signup'] + def show - return redirect_to(path '/') if current_user && params[:id] == 'login' + return redirect_to(path '/') if current_user && (params[:id] == 'login' || params[:id] == 'signup') map = { "faq" => {redirect: "faq_url", topic_id: "guidelines_topic_id"}, @@ -44,6 +46,10 @@ class StaticController < ApplicationController return end + if PAGES_WITH_EMAIL_PARAM.include?(@page) && params[:email] + cookies[:email] = { value: params[:email], expires: 1.day.from_now } + end + file = "static/#{@page}.#{I18n.locale}" file = "static/#{@page}.en" if lookup_context.find_all("#{file}.html").empty? file = "static/#{@page}" if lookup_context.find_all("#{file}.html").empty? diff --git a/app/views/static/signup.html.erb b/app/views/static/signup.html.erb new file mode 100644 index 00000000000..e69de29bb2d diff --git a/config/routes.rb b/config/routes.rb index 270f749c3d6..db779bd73a5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -255,7 +255,7 @@ Discourse::Application.routes.draw do get "guidelines" => "static#show", id: "guidelines", as: 'guidelines' get "tos" => "static#show", id: "tos", as: 'tos' get "privacy" => "static#show", id: "privacy", as: 'privacy' - get "signup" => "list#latest" + get "signup" => "static#show", id: "signup" get "login-preferences" => "static#show", id: "login" get "users/admin-login" => "users#admin_login"