Devise: confirm user email

This commit is contained in:
simon lehericey 2018-05-14 14:21:03 +02:00 committed by Paul Chavard
parent 2b9721f1ee
commit 5bd589344e
10 changed files with 53 additions and 4 deletions

View file

@ -15,6 +15,7 @@ class FranceConnect::ParticulierController < ApplicationController
if fci.user.nil?
user = User.find_or_create_by(email: fci.email_france_connect) do |new_user|
new_user.password = Devise.friendly_token[0, 20]
new_user.confirmed_at = DateTime.now
end
fci.update_attribute('user_id', user.id)

View file

@ -89,7 +89,7 @@ module NewGestionnaire
if gestionnaire.save
user = User.find_by(email: email)
if user.blank?
user = User.create(email: email, password: password)
user = User.create(email: email, password: password, confirmed_at: DateTime.now)
end
sign_in(user)

View file

@ -0,0 +1,6 @@
class DeviseUserMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views
layout 'mailers/layout'
end

View file

@ -10,7 +10,7 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :dossiers, dependent: :destroy
has_many :invites, dependent: :destroy

View file

@ -0,0 +1,10 @@
- content_for(:title, 'Activez votre compte')
%p Bonjour #{@user.email},
%p
Pour activer votre compte sur demarches-simplifiees.fr, veuillez cliquer sur le lien suivant :
= link_to(confirmation_url(@user, confirmation_token: @token), confirmation_url(@user, confirmation_token: @token))
%p Bonne journée,
%p L'équipe demarches-simplifiees.fr

View file

@ -13,7 +13,7 @@ Devise.setup do |config|
config.mailer_sender = "demarches-simplifiees.fr <#{I18n.t('dynamics.contact_email')}>"
# Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer'
config.mailer = 'DeviseUserMailer'
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default) and
@ -119,7 +119,7 @@ Devise.setup do |config|
# initial account confirmation) to be applied. Requires additional unconfirmed_email
# db field (see migrations). Until confirmed, new email is stored in
# unconfirmed_email column, and copied to email column on successful confirmation.
config.reconfirmable = true
config.reconfirmable = false
# Defines which key will be used when confirming an account
# config.confirmation_keys = [ :email ]

View file

@ -0,0 +1,18 @@
class AddConfirmableToUserDevise < ActiveRecord::Migration[5.2]
def up
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
add_index :users, :confirmation_token, unique: true
# User.reset_column_information # Need for some types of updates, but not for update_all.
# To avoid a short time window between running the migration and updating all existing
# users as confirmed, do the following
User.all.update_all confirmed_at: DateTime.now
# All existing user accounts should be able to log in after this.
end
def down
remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
# remove_columns :users, :unconfirmed_email # Only if using reconfirmable
end
end

View file

@ -571,6 +571,10 @@ ActiveRecord::Schema.define(version: 2018_05_16_155238) do
t.datetime "updated_at"
t.string "siret"
t.string "loged_in_with_france_connect", default: "false"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

View file

@ -3,5 +3,6 @@ FactoryBot.define do
factory :user do
email { generate(:user_email) }
password 'password'
confirmed_at DateTime.now
end
end

View file

@ -0,0 +1,9 @@
class DeviseUserMailerPreview < ActionMailer::Preview
def confirmation_instructions
DeviseUserMailer.confirmation_instructions(User.first, "faketoken", {})
end
def reset_password_instructions
DeviseUserMailer.reset_password_instructions(User.first, "faketoken", {})
end
end