feat: added possibility to edit your password and your email on your profile

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2023-04-06 19:58:34 +02:00
parent 86f5e7d088
commit 09c9feeeef
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
10 changed files with 103 additions and 16 deletions

View File

@ -1,6 +0,0 @@
class Profile::HomeController < ApplicationController
before_action :authenticate_user!
def index
end
end

View File

@ -0,0 +1,27 @@
class ProfileController < ApplicationController
before_action :authenticate_user!
def edit
end
def update_email
if current_user.valid_password?(params["current_password"]) && current_user.update(email: params["email"])
flash[:notice] = I18n.translate("flashes.profile.update.success")
redirect_to profile_path
else
flash[:alert] = I18n.translate("flashes.profile.update.fail")
redirect_to profile_path
end
end
def update_password
if !params["password"].blank? && current_user.valid_password?(params["current_password"]) && params["password"] == params["password_confirmation"] && current_user.update(password: params["password"])
bypass_sign_in(current_user)
flash[:notice] = I18n.translate("flashes.profile.update.success")
redirect_to profile_path
else
flash[:alert] = I18n.translate("flashes.profile.update.fail")
redirect_to profile_path
end
end
end

View File

@ -19,7 +19,7 @@
<li><%= link_to t("fields.home"), root_path, class: "nav-link px-2 #{current_page?(root_path) ? "text-secondary" : "text-white"}" %></li>
<% if current_user %>
<li><%= link_to t("fields.count"), count_index_path, class: "nav-link px-2 #{current_page?(count_index_path) ? "text-secondary" : "text-white"}" %></li>
<li><%= link_to t("fields.profile"), profile_root_path, class: "nav-link px-2 #{current_page?(profile_root_path) ? "text-secondary" : "text-white"}" %></li>
<li><%= link_to t("fields.profile"), profile_path, class: "nav-link px-2 #{current_page?(profile_path) ? "text-secondary" : "text-white"}" %></li>
<% end %>
</ul>

View File

@ -0,0 +1,46 @@
<div class="mt-2 mb-4">
<div class="mt-2 mb-4">
<h3><%= t("fields.update_email") %></h3>
<%= form_tag profile_update_email_path, method: :post do %>
<div class="col-2 mt-2">
<%= label_tag :email, t("fields.email") %>
<%= email_field_tag :email, current_user.email, class: "form-control" %>
</div>
<div class="col-2 mt-2">
<%= label_tag :current_password, t("fields.current_password") %>
<%= password_field_tag :current_password, nil, class: "form-control" %>
</div>
<%= submit_tag t("fields.save"), class: "btn btn-primary mt-2" %>
<% end %>
</div>
<div class="mt-2 mb-4">
<h3><%= t("fields.update_password") %></h3>
<%= form_tag profile_update_password_path, method: :post do %>
<div class="col-2 mt-2">
<%= label_tag :email, t("fields.email") %>
<%= email_field_tag :email, current_user.email, class: "form-control" %>
</div>
<div class="col-2 mt-2">
<%= label_tag :password, t("fields.new_password") %>
<%= password_field_tag :password, nil, class: "form-control" %>
</div>
<div class="col-2 mt-2">
<%= label_tag :password_confirmation, t("fields.new_password_confirmation") %>
<%= password_field_tag :password_confirmation, nil, class: "form-control" %>
</div>
<div class="col-2 mt-2">
<%= label_tag :current_password, t("fields.current_password") %>
<%= password_field_tag :current_password, nil, class: "form-control" %>
</div>
<%= submit_tag t("fields.save"), class: "btn btn-primary mt-2" %>
<% end %>
</div>
</div>

View File

@ -1,2 +0,0 @@
<h1>Profile::Home#index</h1>
<p>Find me in app/views/profile/home/index.html.erb</p>

View File

@ -9,3 +9,10 @@ en:
new: "New"
name: "Name"
save: "Save"
email: "E-mail"
password: "Password"
current_password: "Current password"
update_email: "Update e-mail"
new_password: "New password"
new_password_confirmation: "Confirm the new password"
update_password: "Update password"

View File

@ -10,3 +10,7 @@ en:
destroy:
fail: "Count deletion has failed."
success: "Count has been deleted successfully."
profile:
update:
success: "Profile updated successfully."
fail: "Updating the profile failed."

View File

@ -9,3 +9,10 @@ fr:
new: "Nouveau"
name: "Nom"
save: "Sauvegarder"
email: "Courriel"
password: "Mot de passe"
current_password: "Mot de passe actuel"
update_email: "Mettre à jour le courriel"
new_password: "Nouveau mot de passe"
new_password_confirmation: "Confirmez le nouveau mot de passe"
update_password: "Mettre à jour de mot de passe"

View File

@ -10,3 +10,7 @@ fr:
destroy:
fail: "La suppression du compte a échoué."
success: "Le compte a été supprimé avec succès."
profile:
update:
success: "Le profil a bien été mis à jour."
fail: "La mise à jour du profil a échoué."

View File

@ -1,11 +1,11 @@
Rails.application.routes.draw do
namespace :profile do
root "home#index"
end
resources :count, except: [:show]
get "profile", to: "profile#edit"
post "profile/update_email", to: "profile#update_email"
post "profile/update_password", to: "profile#update_password"
root "home#index"
devise_for :users, controllers: {