Allow user to manage social media accounts on settings page

This commit is contained in:
Ahmad Saleem 2017-08-25 00:57:59 +05:00
parent 9daaad1f5c
commit f3b2956e6e
5 changed files with 47 additions and 8 deletions

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -62,12 +62,6 @@
<div class="card">
<div class="card-content">
<h4>Identity check (required)</h4>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm any changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="center">
<div class="actions">
<%= f.submit "Update your account information", class: 'btn blue' %>
@ -76,5 +70,20 @@
</div>
</div>
</div>
<div class="card">
<div class="card-content">
<h4>Social Media Accounts</h4>
<%- 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 %>
<br />
<% end %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%= link_to "Connect with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %><br />
<% end -%>
<% end -%>
</div>
</div>
<% end %>
</div>
</div>

View File

@ -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