Skip to content

Commit

Permalink
Enable and setup email confirmation for db auth method
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-hank committed Jun 24, 2024
1 parent 566fe9e commit 394378d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ DATABASE_DB_NAME=timetracker_development
DATABASE_HOST=postgres
RAILS_ENV=development
VITE_ENV=development
DEVISE_AUTH_TYPE=db
14 changes: 13 additions & 1 deletion app/assets/stylesheets/sessions.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
body.sessions,
body.registrations,
body.passwords {
body.passwords,
body.confirmations {
width: 100vw;
height: 100vh;
background-color: #F7F9FB;
Expand Down Expand Up @@ -32,12 +33,23 @@ body.passwords {

a {
color: #000;
border-top: 1px solid #e5e5e5;
display: block;
padding: 0.2rem 0;

&:first-of-type {
border-top: none;
}
}

.session-logo {
img {
border-radius: 4px;
}
}

br {
display: none;
}
}
}
6 changes: 6 additions & 0 deletions app/controllers/front_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ class FrontController < ApplicationController
def index
render file: 'public/index_f.html', layout: false
end

def authenticate_user!
redirect_to new_user_session_path and return if current_user.nil?

super
end
end
9 changes: 9 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RegistrationsController < Devise::RegistrationsController
def after_inactive_sign_up_path_for(resource_or_scope)
new_user_session_path
end

def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end
end
8 changes: 7 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class User < ActiveRecord::Base
end

if Rails.application.config.devise_auth_type == 'db'
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable
devise_modules = [:database_authenticatable, :registerable, :recoverable, :rememberable, :validatable]
devise_modules << :confirmable if ENV['DEVISE_CONFIRMABLE'] == 'true'
devise(*devise_modules)
end

def user_timesheets(only_admin: false)
Expand Down Expand Up @@ -55,4 +57,8 @@ def match_existing_user

self.password = SecureRandom.base64(15) unless self.password.present?
end

def root_path
new_user_session_path
end
end
22 changes: 22 additions & 0 deletions app/views/devise/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="session-logo">
<%= image_tag("logo.png") %>
</div>

<h4 class="is-size-4 mb-2">Resend confirmation instructions</h4>

<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>

<div class="field">
<%= f.label :email, class: 'label' %>
<div class="control">
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: 'input', value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
</div>
</div>

<div class="actions">
<button class="button is-rounded" type="submit">Resend confirmation instructions</button>
</div>
<% end %>

<%= render "devise/shared/links" %>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
devise_for :users
devise_for :users, controllers: { registrations: 'registrations' }
root to: 'front#index'

get :ping, to: 'application#ping'
Expand Down

0 comments on commit 394378d

Please sign in to comment.