Skip to content

Commit

Permalink
#41 OAuth 로그인 시 패스워드 필요하지않아도 사용자 정보 변경될수 있도록한다 (#64 MERGE 이후 작업 가능)
Browse files Browse the repository at this point in the history
  • Loading branch information
minhyeok92 committed Aug 20, 2014
1 parent d8b049b commit 445f1e2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 61 deletions.
6 changes: 3 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ApplicationController < ActionController::Base
# migration for new field nickname which is required field.
before_action :is_nickname_not_empty?


@my_logger ||= Logger.new("#{Rails.root}/log/my.log")

protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :nickname
Expand All @@ -22,5 +22,5 @@ def is_nickname_not_empty?
redirect_to edit_user_registration_path
end
end

end
22 changes: 2 additions & 20 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
# def facebook
# # You need to implement the method below in your model (e.g. app/models/user.rb)
# @user = User.from_omniauth(request.env["omniauth.auth"])

# if @user.persisted? and @user.uid != nil
# sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
# set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
# elsif @user.persisted? and @user.uid == nil


# session["devise.facebook_data"] = request.env["omniauth.auth"]
# redirect_to users_merge_path(@user.id, 'facebook_data')
# else
# session["devise.facebook_data"] = request.env["omniauth.auth"]
# redirect_to new_user_registration_url
# end
# end

def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
@user = User.find_for_oauth2(request.env["omniauth.auth"], current_user)

# OAuth 성공
if @user.persisted? and @user.uid != nil
Expand All @@ -37,7 +19,7 @@ def facebook

def google_oauth2
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
@user = User.find_for_oauth2(request.env["omniauth.auth"], current_user)

# OAuth 성공
if @user.persisted? and @user.uid != nil
Expand Down
27 changes: 26 additions & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
class Users::RegistrationsController < Devise::RegistrationsController
skip_before_action :is_nickname_not_empty?
end

def update
if current_user.provider == nil
super
else
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)

# required for settings form to submit when password is left blank
if account_update_params[:password].blank?
account_update_params.delete("password")
account_update_params.delete("password_confirmation")
account_update_params.delete("current_password")
end

@user = User.find(current_user.id)
if @user.update_attributes(account_update_params)
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case their password changed
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
end
end
77 changes: 40 additions & 37 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,46 @@
<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>

<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>

<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>

<div class="form-group">
<%= f.label :nickname %>
<%= f.text_field :nickname, class: "form-control" %>
</div>

<div class="form-group">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i>
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
</div>

<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
</div>

<div class="form-group">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i>
<%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
</div>

<div class="form-buttons">
<%= link_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete, class: "btn btn-default" %>
<%= link_to "Back", :back, class: "btn btn-default" %>
<%= f.submit "Update", class: "btn btn-default" %>
</div>

<%= devise_error_messages! %>

<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>

<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>

<div class="form-group">
<%= f.label :nickname %>
<%= f.text_field :nickname, class: "form-control" %>
</div>

<% if current_user.provider == nil %>
<div class="form-group">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i>
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
</div>

<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
</div>


<div class="form-group"> <i>수정하기 위해선 비밀번호를 입력해야 합니다</i>
<%= f.label :current_password %>
<%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
</div>
<% end %>

<div class="form-buttons">
<%= link_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete, class: "btn btn-default" %>
<%= link_to "Back", :back, class: "btn btn-default" %>
<%= f.submit "Update", class: "btn btn-default" %>
</div>

<% end %>

</div>
Expand Down

1 comment on commit 445f1e2

@minhyeok4dev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#41

Please sign in to comment.