diff --git a/Gemfile b/Gemfile index 26019d5..c97ba0f 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,8 @@ gem 'rspec-rails' gem 'rails-controller-testing' +gem 'devise' + # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem 'rails', '~> 7.0.8' diff --git a/Gemfile.lock b/Gemfile.lock index c9860c6..71e0957 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -70,6 +70,7 @@ GEM public_suffix (>= 2.0.2, < 6.0) ast (2.4.2) base64 (0.1.1) + bcrypt (3.1.19) bindex (0.8.1) bootsnap (1.16.0) msgpack (~> 1.2) @@ -89,6 +90,12 @@ GEM debug (1.8.0) irb (>= 1.5.0) reline (>= 0.3.1) + devise (4.9.2) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0) + responders + warden (~> 1.2.3) diff-lcs (1.5.0) erubi (1.12.0) globalid (1.2.1) @@ -133,6 +140,7 @@ GEM nio4r (2.5.9) nokogiri (1.15.4-x64-mingw-ucrt) racc (~> 1.4) + orm_adapter (0.5.0) parallel (1.23.0) parser (3.2.2.3) ast (~> 2.4.1) @@ -186,6 +194,9 @@ GEM regexp_parser (2.8.1) reline (0.3.8) io-console (~> 0.5) + responders (3.1.0) + actionpack (>= 5.2) + railties (>= 5.2) rexml (3.2.6) rspec-core (3.12.2) rspec-support (~> 3.12.0) @@ -245,6 +256,8 @@ GEM tzinfo-data (1.2023.3) tzinfo (>= 1.0.0) unicode-display_width (2.4.2) + warden (1.2.9) + rack (>= 2.0.9) web-console (4.2.1) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -265,6 +278,7 @@ DEPENDENCIES bootsnap capybara debug + devise importmap-rails jbuilder pg (~> 1.1) diff --git a/README.md b/README.md index 6c50e60..f519bc0 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ Blog-app is a fully functioning website that show the list of posts and empower - **Users and posts views** - **Creating Forms** - **Intergration specs and fix N+1** +- **Add-devise**
@@ -118,7 +119,6 @@ it will install the required gemfile for running the project ## 🔭 Future Features - - Add Devise. - Add authorization rules. - Add API endpoints. - API documentation. @@ -147,7 +147,7 @@ If you like this project you can share this project to your friend ## 🙏 Acknowledgments -I would like to thank Microverse for this project +We would like to thank Microverse for this project diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 29b02f6..5af61a6 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -217,3 +217,45 @@ a { width: 150px; text-align: center; } + +.form_container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + width: 100%; +} + +.devise-head { + text-align: center; +} + +.field-dev { + margin-block: 10px; + width: 80%; + font-size: 25px; +} + +.dev-links { + display: flex; + flex-direction: column; + font-size: 25px; + justify-content: center; + align-items: center; + margin-top: 20px; +} + +.actions-dev input[type="submit"] { + font-size: 25px; + background-color: brown; + color: white; + width: 20rem; +} + +.field-dev input[type="text"], +.field-dev input[type="email"], +.field-dev input[type="password"], +.field-dev textarea { + height: 40px; + width: 20rem; +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e13f93b..95b01e7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,22 @@ class ApplicationController < ActionController::Base - def current_user - @current_user = User.first + # Set up user authentication + before_action :authenticate_user! + # Add addtional parameters + before_action :configure_permitted_parameters, if: :devise_controller? + # Customize redirect hooks + def after_sign_in_path_for(_resource) + users_url + end + # Permit addtional parameters (@lazy) + + protected + + def configure_permitted_parameters + devise_parameter_sanitizer.permit( + :sign_up, keys: %i[name bio email photo password confirm_password] + ) + devise_parameter_sanitizer.permit( + :sign_in, keys: %i[email password] + ) end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index b95f671..6451c80 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -19,6 +19,7 @@ def create @post = @user.posts.build(post_params) if @post.save + @user.increment!(:posts_counter) redirect_to user_post_url(@user, @post) else diff --git a/app/models/user.rb b/app/models/user.rb index 4110ff5..0cfeefb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,8 @@ class User < ApplicationRecord + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable + devise :confirmable, :database_authenticatable, :registerable, + :recoverable, :rememberable, :validatable has_many :posts, foreign_key: 'author_id' has_many :comments, foreign_key: 'user_id' has_many :likes, foreign_key: 'user_id' @@ -6,6 +10,12 @@ class User < ApplicationRecord validates :name, presence: true validates :posts_counter, numericality: { only_integer: true, greater_than_or_equal_to: 0 } + after_initialize :set_defaults + + def set_defaults + self.posts_counter ||= 0 + end + def recent_posts(limit = 3) posts.order(created_at: :desc).limit(limit) end diff --git a/app/views/devise/confirmations/new.html.erb b/app/views/devise/confirmations/new.html.erb new file mode 100644 index 0000000..ac0b995 --- /dev/null +++ b/app/views/devise/confirmations/new.html.erb @@ -0,0 +1,20 @@ +Welcome <%= @email %>!
+ +You can confirm your account email through the link below:
+ +<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %>
diff --git a/app/views/devise/mailer/email_changed.html.erb b/app/views/devise/mailer/email_changed.html.erb new file mode 100644 index 0000000..32f4ba8 --- /dev/null +++ b/app/views/devise/mailer/email_changed.html.erb @@ -0,0 +1,7 @@ +Hello <%= @email %>!
+ +<% if @resource.try(:unconfirmed_email?) %> +We're contacting you to notify you that your email is being changed to <%= @resource.unconfirmed_email %>.
+<% else %> +We're contacting you to notify you that your email has been changed to <%= @resource.email %>.
+<% end %> diff --git a/app/views/devise/mailer/password_change.html.erb b/app/views/devise/mailer/password_change.html.erb new file mode 100644 index 0000000..b41daf4 --- /dev/null +++ b/app/views/devise/mailer/password_change.html.erb @@ -0,0 +1,3 @@ +Hello <%= @resource.email %>!
+ +We're contacting you to notify you that your password has been changed.
diff --git a/app/views/devise/mailer/reset_password_instructions.html.erb b/app/views/devise/mailer/reset_password_instructions.html.erb new file mode 100644 index 0000000..f667dc1 --- /dev/null +++ b/app/views/devise/mailer/reset_password_instructions.html.erb @@ -0,0 +1,8 @@ +Hello <%= @resource.email %>!
+ +Someone has requested a link to change your password. You can do this through the link below.
+ +<%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %>
+ +If you didn't request this, please ignore this email.
+Your password won't change until you access the link above and create a new one.
diff --git a/app/views/devise/mailer/unlock_instructions.html.erb b/app/views/devise/mailer/unlock_instructions.html.erb new file mode 100644 index 0000000..41e148b --- /dev/null +++ b/app/views/devise/mailer/unlock_instructions.html.erb @@ -0,0 +1,7 @@ +Hello <%= @resource.email %>!
+ +Your account has been locked due to an excessive number of unsuccessful sign in attempts.
+ +Click the link below to unlock your account:
+ +<%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %>
diff --git a/app/views/devise/passwords/edit.html.erb b/app/views/devise/passwords/edit.html.erb new file mode 100644 index 0000000..5fbb9ff --- /dev/null +++ b/app/views/devise/passwords/edit.html.erb @@ -0,0 +1,25 @@ +<%= notice %>
+<%= alert %>
<%= yield %>