-
Notifications
You must be signed in to change notification settings - Fork 177
How To: Simple Devise User Management with Password Reset
Sam Pohlenz edited this page May 5, 2020
·
2 revisions
Use case: Let's assume you successfully configured Trestle and secured it with Devise and would now like to:
- Create new and update existing users
- Reset the password
Create a Trestle resource app/admin/users_admin.rb
:
Trestle.resource(:users) do
menu do
group :user_management, priority: :first do
item :users, icon: "fa fa-users"
end
end
table do
column :id
column :email
column :first_name
column :last_name
column :display_name
end
# show 2 tabs:
# * Profile: Edit user/profile details
# * Password Reset: Just update the password
form do |resource|
tab :profile do
text_field :email
text_field :first_name
text_field :last_name
text_field :display_name
text_field :display_image
end
tab :password_reset do
password_field :password
password_field :password_confirmation
end
end
# Remove the password & confirmation from the attributes if they were left blank
update_instance do |instance, attrs|
if attrs[:password].blank?
attrs.delete(:password)
attrs.delete(:password_confirmation) if attrs[:password_confirmation].blank?
end
instance.assign_attributes(attrs)
end
# Log back in (may be required after a password update) if the current user was updated
after_action on: :update do
if Devise.sign_in_after_reset_password && instance == current_user
login!(instance)
end
end
end