Skip to content

Commit faf679a

Browse files
author
minhyeok92
committed
oauth로 최초 로그인 시 닉네임을 입력받습니다(단, 트위터는 제외- 기존의 nickname 필드를 읽어올 수 있음)
1 parent 84e6faa commit faf679a

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

app/controllers/application_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ class ApplicationController < ActionController::Base
55

66
before_action :configure_permitted_parameters, if: :devise_controller?
77
before_action :authenticate_user!
8-
# migration for new field nickname which is required field.
9-
before_action :is_nickname_not_empty?
10-
11-
@my_logger ||= Logger.new("#{Rails.root}/log/my.log")
128

139
protected
1410
def configure_permitted_parameters

app/controllers/users/omniauth_callbacks_controller.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ def facebook
1313
def google_oauth2
1414
@user = User.find_for_oauth2(request.env["omniauth.auth"])
1515

16+
session["omniauth"] = request.env["omniauth.auth"]
17+
1618
if @user.nil?
17-
session["omniauth"] = request.env["omniauth.auth"]
18-
redirect_to users_merge_path
19+
render users_nickname_new_path
20+
elsif @user == "dup"
21+
redirect_to users_merge_path
1922
else
2023
sign_in_and_redirect @user, :event => :authentication
2124
end

app/controllers/users_controller.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ def merge_callback
2020
end
2121
end
2222

23+
def nickname_new
24+
end
25+
26+
def nickname_new_callback
27+
auth = session["omniauth"]
28+
29+
@user = User.create!(provider:auth["provider"],
30+
uid: auth["uid"],
31+
email: auth["info"]["email"],
32+
password: Devise.friendly_token[0,20],
33+
nickname: params[:nickname])
34+
sign_in_and_redirect @user, :event => :authentication
35+
end
36+
2337
def sign_up_from_twitter
2438
end
2539

app/models/user.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,9 @@ def self.find_for_oauth2(access_token)
5151

5252
unless user
5353
if User.where(email: data["email"]).first
54-
nil
54+
"dup"
5555
else
56-
user = User.create!(provider:access_token.provider,
57-
uid:access_token.uid,
58-
email: data["email"],
59-
password: Devise.friendly_token[0,20],
60-
nickname: data["name"])
56+
nil
6157
end
6258
end
6359
user
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
<%= form_tag({controller: "/users", action: "nickname_new_callback"}, method: "post") do %>
3+
4+
<p>처음이십니다. 새로운 닉네임을 입력하세요</p>
5+
<%= text_field_tag(:nickname) %>
6+
<%= submit_tag("Submit", class: "btn btn-primary") %>
7+
<% end %>
8+

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
get 'users/merge', action: 'merge', as: 'users_merge'
1313
get 'users/merge/callback', action: 'merge_callback', as: 'users_merge_callback'
14+
15+
get 'users/nickname/new', action: 'nickname_new'
16+
post 'users/nickname/new/callback', action: 'nickname_new_callback'
1417
end
1518

1619
get '/users/(/:nickname)', to: 'users#show', as: 'show_user'

0 commit comments

Comments
 (0)