From f3b2956e6e20fc49a941f5c0435ac7caac2d9fc5 Mon Sep 17 00:00:00 2001 From: Ahmad Saleem Date: Fri, 25 Aug 2017 00:57:59 +0500 Subject: [PATCH] Allow user to manage social media accounts on settings page --- .../omniauth_accounts_controller.rb | 12 ++++++++++ app/controllers/registrations_controller.rb | 6 ++++- app/models/user.rb | 12 ++++++++++ app/views/devise/registrations/edit.html.erb | 23 +++++++++++++------ config/routes.rb | 2 ++ 5 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 app/controllers/omniauth_accounts_controller.rb diff --git a/app/controllers/omniauth_accounts_controller.rb b/app/controllers/omniauth_accounts_controller.rb new file mode 100644 index 00000000..5f89905c --- /dev/null +++ b/app/controllers/omniauth_accounts_controller.rb @@ -0,0 +1,12 @@ +class OmniauthAccountsController < ApplicationController + before_action :authenticate_user! + + def destroy + if current_user.omniauth_users.find(params[:id]).destroy + flash[:notice] = 'The account was successfully disconnected.' + else + flash[:alert] = 'Something went wrong.' + end + redirect_to root_path + end +end diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index d9f422b4..e7df9fb4 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -15,11 +15,15 @@ class RegistrationsController < Devise::RegistrationsController end def account_update_params - params.require(:user).permit(:name, :email, :password, :password_confirmation, :current_password, :email_updates, :fluid_preference) + params.require(:user).permit(:name, :email, :password, :password_confirmation, :email_updates, :fluid_preference) end protected + def update_resource(resource, params) + resource.update_without_password(params) + end + def add_account # If the user was created in the last 60 seconds, report it to Slack if resource.persisted? diff --git a/app/models/user.rb b/app/models/user.rb index 511c663f..3f620d55 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -113,6 +113,18 @@ class User < ActiveRecord::Base update secure_code: SecureRandom.uuid unless secure_code.present? end + + def update_without_password(params, *options) + if params[:password].blank? + params.delete(:password) + params.delete(:password_confirmation) if params[:password_confirmation].blank? + end + + result = update_attributes(params, *options) + clean_up_passwords + result + end + private # Attributes that are non-public, and should be blacklisted from any public diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 7a7a4aa7..7c29d054 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -62,12 +62,6 @@
-

Identity check (required)

-
- <%= f.label :current_password %> (we need your current password to confirm any changes)
- <%= f.password_field :current_password, autocomplete: "off" %> -
-
<%= f.submit "Update your account information", class: 'btn blue' %> @@ -76,5 +70,20 @@
+ +
+
+

Social Media Accounts

+ <%- if devise_mapping.omniauthable? %> + <% resource.omniauth_users.each do |omniauth_account| %> + <%= link_to "Disconnect #{omniauth_account.provider} (#{omniauth_account.email})", omniauth_account_path(omniauth_account.id), method: :delete %> +
+ <% end %> + <%- resource_class.omniauth_providers.each do |provider| %> + <%= link_to "Connect with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %>
+ <% end -%> + <% end -%> +
+
<% end %> -
\ No newline at end of file + diff --git a/config/routes.rb b/config/routes.rb index bf2b8649..84971224 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -213,6 +213,8 @@ Rails.application.routes.draw do scope '/market' do get '/', to: 'main#comingsoon' end + + resources :omniauth_accounts, only: [:destroy] end # rubocop:enable LineLength