From 3daf5ce67f3ff3844129c78b9ad18bc6f09a5e49 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 3 Dec 2018 22:58:39 +0500 Subject: [PATCH] Enable Devise confirmable --- Gemfile | 4 +++ Gemfile.lock | 3 ++ .../users/confirmations_controller.rb | 33 +++++++++++++++++++ app/helpers/application_helper.rb | 12 +++++++ app/models/user.rb | 2 +- app/views/application/_flash.html.erb | 10 ++++++ app/views/layouts/application.html.erb | 1 + app/views/users/confirmations/new.html.erb | 22 +++++++++++++ config/environments/development.rb | 9 +++-- config/environments/test.rb | 4 +-- config/initializers/devise.rb | 6 ++-- config/routes.rb | 1 + factories/users.rb | 4 +++ 13 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 app/controllers/users/confirmations_controller.rb create mode 100644 app/views/application/_flash.html.erb create mode 100644 app/views/users/confirmations/new.html.erb diff --git a/Gemfile b/Gemfile index 83cab7c..b5ee020 100644 --- a/Gemfile +++ b/Gemfile @@ -141,6 +141,10 @@ group :development do # YARD is a documentation generation tool for the Ruby programming language. gem 'yard', '~> 0.9' + + # When mail is sent from your application, + # Letter Opener will open a preview in the browser instead of sending. + gem 'letter_opener', '~> 1.6' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index c5a7380..4191f68 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -163,6 +163,8 @@ GEM json (2.1.0) launchy (2.4.3) addressable (~> 2.3) + letter_opener (1.6.0) + launchy (~> 2.2) libv8 (6.7.288.46.1) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) @@ -384,6 +386,7 @@ DEPENDENCIES font-awesome-sass (~> 5.5.0) interactor (~> 3.1) jquery-rails (~> 4.3) + letter_opener (~> 1.6) listen (>= 3.0.5, < 3.2) mini_racer pg (>= 0.18, < 2.0) diff --git a/app/controllers/users/confirmations_controller.rb b/app/controllers/users/confirmations_controller.rb new file mode 100644 index 0000000..a883648 --- /dev/null +++ b/app/controllers/users/confirmations_controller.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +class Users::ConfirmationsController < Devise::ConfirmationsController + skip_after_action :verify_authorized + skip_after_action :verify_policy_scoped + + # GET /resource/confirmation/new + # def new + # super + # end + + # POST /resource/confirmation + # def create + # super + # end + + # GET /resource/confirmation?confirmation_token=abcdef + # def show + # super + # end + + # protected + + # The path used after resending confirmation instructions. + # def after_resending_confirmation_instructions_path_for(resource_name) + # super(resource_name) + # end + + # The path used after confirmation. + # def after_confirmation_path_for(resource_name, resource) + # super(resource_name, resource) + # end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 15b06f0..2dcab44 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,16 @@ # frozen_string_literal: true module ApplicationHelper + def bootstrap_class_for_flash(flash_type) + case flash_type + when 'success' + 'alert-success' + when 'error' + 'alert-danger' + when 'alert' + 'alert-warning' + else + 'alert-info' + end + end end diff --git a/app/models/user.rb b/app/models/user.rb index 7ed7fa0..ab1d2fc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,7 +2,7 @@ class User < ApplicationRecord devise( - # :confirmable, + :confirmable, :database_authenticatable, # :lockable, # :omniauthable, diff --git a/app/views/application/_flash.html.erb b/app/views/application/_flash.html.erb new file mode 100644 index 0000000..deb8914 --- /dev/null +++ b/app/views/application/_flash.html.erb @@ -0,0 +1,10 @@ +<% unless flash.empty? %> +
+ <% flash.each do |type, msg| %> +
+ <%= msg %> + +
+ <% end %> +
+<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 29266c0..a1d36d4 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -16,6 +16,7 @@ <%= render 'navbar' %> + <%= render partial: 'flash', flash: flash %> <%= yield :top %>
<%= yield %> diff --git a/app/views/users/confirmations/new.html.erb b/app/views/users/confirmations/new.html.erb new file mode 100644 index 0000000..3bf916d --- /dev/null +++ b/app/views/users/confirmations/new.html.erb @@ -0,0 +1,22 @@ +
+

<%= translate '.resend_confirmation_instructions' %>

+ + <%= simple_form_for resource, as: resource_name, url: confirmation_path(resource_name) do |f| %> + <%= f.error_notification %> + <%= f.full_error :confirmation_token %> + +
+ <%= f.input :email, + required: true, + autofocus: true, + value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email), + input_html: { autocomplete: 'email' } %> +
+ +
+ <%= f.button :submit, translate('.resend_confirmation_instructions') %> +
+ <% end %> + + <%= render 'users/shared/links' %> +
diff --git a/config/environments/development.rb b/config/environments/development.rb index 8803494..a7e5631 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -34,9 +34,12 @@ Rails.application.configure do # (see config/storage.yml for options). config.active_storage.service = :local - # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false - + # When mail is sent from your application, + # Letter Opener will open a preview in the browser instead of sending. + config.action_mailer.delivery_method = :letter_opener + config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } + config.action_mailer.perform_deliveries = true + config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_caching = false # Print deprecation notices to the Rails logger. diff --git a/config/environments/test.rb b/config/environments/test.rb index 2f3f972..7fdac4c 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -34,12 +34,12 @@ Rails.application.configure do # Store uploaded files on the local file system in a temporary directory config.active_storage.service = :test - config.action_mailer.perform_caching = false - # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test + config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } + config.action_mailer.perform_caching = false # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 8fc378c..249c016 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -132,7 +132,7 @@ Devise.setup do |config| # able to access the website for two days without confirming their account, # access will be blocked just in the third day. Default is 0.days, meaning # the user cannot access the website without confirming their account. - # config.allow_unconfirmed_access_for = 2.days + config.allow_unconfirmed_access_for = 0.days # A period that the user is allowed to confirm their account before their # token becomes invalid. For example, if set to 3.days, the user can confirm @@ -140,7 +140,7 @@ Devise.setup do |config| # their account can't be confirmed with the token any more. # Default is nil, meaning there is no restriction on how long a user can take # before confirming their account. - # config.confirm_within = 3.days + config.confirm_within = 3.days # If true, requires any email changes to be confirmed (exactly the same way as # initial account confirmation) to be applied. Requires additional unconfirmed_email @@ -149,7 +149,7 @@ Devise.setup do |config| config.reconfirmable = true # Defines which key will be used when confirming an account - # config.confirmation_keys = [:email] + config.confirmation_keys = [:email] # ==> Configuration for :rememberable # The time the user will be remembered without asking for credentials again. diff --git a/config/routes.rb b/config/routes.rb index a3ac3c2..38d1afb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,6 +24,7 @@ Rails.application.routes.draw do devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations', + confirmations: 'users/confirmations', } resources :membership_applications, only: %i[new create] diff --git a/factories/users.rb b/factories/users.rb index 6c98ac9..dc14bcc 100644 --- a/factories/users.rb +++ b/factories/users.rb @@ -5,5 +5,9 @@ FactoryBot.define do email { Faker::Internet.email } password { Faker::Internet.password } password_confirmation { password } + + after :create do |user, _evaluator| + user.confirm + end end end