Skip to content

Commit 445f1e2

Browse files
author
minhyeok92
committed
#41 OAuth 로그인 시 패스워드 필요하지않아도 사용자 정보 변경될수 있도록한다 (#64 MERGE 이후 작업 가능)
1 parent d8b049b commit 445f1e2

File tree

4 files changed

+71
-61
lines changed

4 files changed

+71
-61
lines changed

app/controllers/application_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class ApplicationController < ActionController::Base
88
# migration for new field nickname which is required field.
99
before_action :is_nickname_not_empty?
1010

11-
12-
11+
@my_logger ||= Logger.new("#{Rails.root}/log/my.log")
12+
1313
protected
1414
def configure_permitted_parameters
1515
devise_parameter_sanitizer.for(:sign_up) << :nickname
@@ -22,5 +22,5 @@ def is_nickname_not_empty?
2222
redirect_to edit_user_registration_path
2323
end
2424
end
25-
25+
2626
end

app/controllers/users/omniauth_callbacks_controller.rb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
11
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
2-
# def facebook
3-
# # You need to implement the method below in your model (e.g. app/models/user.rb)
4-
# @user = User.from_omniauth(request.env["omniauth.auth"])
5-
6-
# if @user.persisted? and @user.uid != nil
7-
# sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
8-
# set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
9-
# elsif @user.persisted? and @user.uid == nil
10-
11-
12-
# session["devise.facebook_data"] = request.env["omniauth.auth"]
13-
# redirect_to users_merge_path(@user.id, 'facebook_data')
14-
# else
15-
# session["devise.facebook_data"] = request.env["omniauth.auth"]
16-
# redirect_to new_user_registration_url
17-
# end
18-
# end
19-
202
def facebook
213
# You need to implement the method below in your model (e.g. app/models/user.rb)
22-
@user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
4+
@user = User.find_for_oauth2(request.env["omniauth.auth"], current_user)
235

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

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

4224
# OAuth 성공
4325
if @user.persisted? and @user.uid != nil
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
class Users::RegistrationsController < Devise::RegistrationsController
22
skip_before_action :is_nickname_not_empty?
3-
end
3+
4+
def update
5+
if current_user.provider == nil
6+
super
7+
else
8+
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
9+
10+
# required for settings form to submit when password is left blank
11+
if account_update_params[:password].blank?
12+
account_update_params.delete("password")
13+
account_update_params.delete("password_confirmation")
14+
account_update_params.delete("current_password")
15+
end
16+
17+
@user = User.find(current_user.id)
18+
if @user.update_attributes(account_update_params)
19+
set_flash_message :notice, :updated
20+
# Sign in the user bypassing validation in case their password changed
21+
sign_in @user, :bypass => true
22+
redirect_to after_update_path_for(@user)
23+
else
24+
render "edit"
25+
end
26+
end
27+
end
28+
end

app/views/devise/registrations/edit.html.erb

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,46 @@
44
<h2>Edit <%= resource_name.to_s.humanize %></h2>
55

66
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
7-
<%= devise_error_messages! %>
8-
9-
<div class="form-group">
10-
<%= f.label :email %>
11-
<%= f.email_field :email, autofocus: true, class: "form-control" %>
12-
</div>
13-
14-
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
15-
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
16-
<% end %>
17-
18-
<div class="form-group">
19-
<%= f.label :nickname %>
20-
<%= f.text_field :nickname, class: "form-control" %>
21-
</div>
22-
23-
<div class="form-group">
24-
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i>
25-
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
26-
</div>
27-
28-
<div class="form-group">
29-
<%= f.label :password_confirmation %>
30-
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
31-
</div>
32-
33-
<div class="form-group">
34-
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i>
35-
<%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
36-
</div>
37-
38-
<div class="form-buttons">
39-
<%= link_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete, class: "btn btn-default" %>
40-
<%= link_to "Back", :back, class: "btn btn-default" %>
41-
<%= f.submit "Update", class: "btn btn-default" %>
42-
</div>
43-
7+
<%= devise_error_messages! %>
8+
9+
<div class="form-group">
10+
<%= f.label :email %>
11+
<%= f.email_field :email, autofocus: true, class: "form-control" %>
12+
</div>
13+
14+
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
15+
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
16+
<% end %>
17+
18+
<div class="form-group">
19+
<%= f.label :nickname %>
20+
<%= f.text_field :nickname, class: "form-control" %>
21+
</div>
22+
23+
<% if current_user.provider == nil %>
24+
<div class="form-group">
25+
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i>
26+
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
27+
</div>
28+
29+
<div class="form-group">
30+
<%= f.label :password_confirmation %>
31+
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
32+
</div>
33+
34+
35+
<div class="form-group"> <i>수정하기 위해선 비밀번호를 입력해야 합니다</i>
36+
<%= f.label :current_password %>
37+
<%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
38+
</div>
39+
<% end %>
40+
41+
<div class="form-buttons">
42+
<%= link_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete, class: "btn btn-default" %>
43+
<%= link_to "Back", :back, class: "btn btn-default" %>
44+
<%= f.submit "Update", class: "btn btn-default" %>
45+
</div>
46+
4447
<% end %>
4548

4649
</div>

0 commit comments

Comments
 (0)