Skip to content

Commit c2b3f62

Browse files
author
minhyeok92
committed
트위터 로그인 리팩토링
1 parent 41996a7 commit c2b3f62

File tree

5 files changed

+22
-28
lines changed

5 files changed

+22
-28
lines changed

app/controllers/users/omniauth_callbacks_controller.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ def facebook
33
@user = User.find_for_oauth2(request.env["omniauth.auth"])
44

55
if @user.persisted? and @user.uid != nil
6-
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
76
sign_in_and_redirect @user, :event => :authentication
87
elsif @user.persisted? and @user.uid == nil
98
session["devise.facebook_data"] = request.env["omniauth.auth"]
@@ -15,23 +14,22 @@ def google_oauth2
1514
@user = User.find_for_oauth2(request.env["omniauth.auth"])
1615

1716
if @user.persisted? and @user.uid != nil
18-
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
1917
sign_in_and_redirect @user, :event => :authentication
2018
elsif @user.persisted? and @user.uid == nil
2119
session["devise.google_data"] = request.env["omniauth.auth"]
2220
redirect_to users_merge_path(@user.id, 'google_data')
2321
end
2422
end
2523

26-
def twitter
24+
def twitter
2725
@user = User.find_for_twitter_oauth(request.env["omniauth.auth"])
28-
time = [@user.created_at, @user.updated_at]
2926

30-
if @user.persisted? and time[0] != time[1]
31-
sign_in_and_redirect @user, :event => :authentication
32-
set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format?
27+
if @user.nil?
28+
@user = User.new
29+
session["devise.twitter_data"] = request.env["omniauth.auth"]
30+
render sign_up_from_twitter_path
3331
else
34-
redirect_to sign_up_from_twitter_path(@user.id)
32+
sign_in_and_redirect @user, :event => :authentication
3533
end
3634
end
3735
end

app/controllers/users_controller.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ def show
55
end
66

77
def merge
8-
9-
108
@user = User.find(params[:id])
119
@provider = params[:provider]
1210

@@ -20,13 +18,19 @@ def merge
2018
end
2119

2220
def sign_up_from_twitter
23-
@user = User.find(params[:id])
21+
end
2422

25-
if request.post?
26-
params = request.params[:user]
27-
@user.update_from_twitter(@user.id, params[:email])
23+
def sign_up_from_twitter_callback
24+
auth = session["devise.twitter_data"]
2825

29-
redirect_to root_path
30-
end
26+
@user = User.new(provider:auth["provider"],
27+
uid:auth["uid"],
28+
nickname: auth["extra"]["raw_info"]["screen_name"],
29+
password: Devise.friendly_token[0,20])
30+
31+
@user.email = params[:user]["email"]
32+
33+
@user.save!
34+
sign_in_and_redirect @user, :event => :authentication
3135
end
3236
end

app/models/user.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ def self.find_for_oauth2(access_token)
6363

6464
def self.find_for_twitter_oauth(auth)
6565
user = User.where(:uid => auth.uid, :provider => auth.provider).first
66-
67-
unless user
68-
user = User.create!(provider:auth.provider,
69-
uid:auth.uid,
70-
email: "temp." + Devise.friendly_token[0,7] + "@todo.nut",
71-
nickname: auth.extra.raw_info.screen_name,
72-
password:Devise.friendly_token[0,20])
73-
end
74-
user
7566
end
7667

7768
def merge(id, provider, uid)

app/views/users/sign_up_from_twitter.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<h1>새 이메일을 입력하세요 - Twitter</h1>
2+
23
<div class="form-container">
3-
<%= form_for @user, url: {action: "sign_up_from_twitter"}, method: "post", :role => 'form' do |f|%>
4+
<%= form_for @user, url: {action: "sign_up_from_twitter_callback", controller: "/users"}, method: "post", :role => 'form' do |f|%>
45

56

67
<div class="form-group">

config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Rails.application.routes.draw do
2-
get 'users/sign_up_from_twitter/:id', to: 'users#sign_up_from_twitter', as: 'sign_up_from_twitter'
3-
post 'users/sign_up_from_twitter/:id', to: 'users#sign_up_from_twitter', as: 'sign_up_from_twitter_update'
2+
get 'users/sign_up_from_twitter', to: 'users#sign_up_from_twitter', as: 'sign_up_from_twitter'
3+
post 'users/sign_up_from_twitter_callback', to: 'users#sign_up_from_twitter_callback', as: 'sign_up_from_twitter_callback'
44

55
get '/users/merge/:id/:provider(/:callback)', to: 'users#merge', as: 'users_merge'
66

0 commit comments

Comments
 (0)