Skip to content

Commit

Permalink
#76 다양한 예외사항 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
minhyeok committed Aug 28, 2014
1 parent ab02179 commit 9166136
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 95 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ gem 'rabl-rails'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
gem 'omniauth-twitter'
gem 'dynamic_form'

gem 'quiet_assets', group: :development

group :production do
gem 'rails_12factor'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ GEM
railties (>= 3.2.6, < 5)
thread_safe (~> 0.1)
warden (~> 1.2.3)
dynamic_form (1.1.4)
erubis (2.7.0)
execjs (2.2.1)
faraday (0.9.0)
Expand Down Expand Up @@ -116,6 +117,8 @@ GEM
orm_adapter (0.5.0)
pg (0.17.1)
polyglot (0.3.5)
quiet_assets (1.0.3)
railties (>= 3.1, < 5.0)
rabl-rails (0.3.4)
activesupport (>= 3.1)
railties (>= 3.1)
Expand Down Expand Up @@ -198,6 +201,7 @@ DEPENDENCIES
cloudinary
coffee-rails (~> 4.0.0)
devise
dynamic_form
gon
jbuilder (~> 2.0)
jquery-rails
Expand All @@ -207,6 +211,7 @@ DEPENDENCIES
omniauth-google-oauth2
omniauth-twitter
pg
quiet_assets
rabl-rails
rails (= 4.1.4)
rails_12factor
Expand Down
91 changes: 49 additions & 42 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def self.provides_callback_for(provider)
class_eval %Q{
def #{provider}
@provider = "#{provider}"
@uid = request.env["omniauth.auth"].uid
result = User.find_for_oauth(@provider, request.env["omniauth.auth"])
case @provider
when "twitter"
@nickname = request.env["omniauth.auth"]["extra"]["raw_info"].screen_name
case result[:status]
when :success
@user = result[:data]
sign_in_and_redirect @user, :event => :authentication
when :first_login
@user = User.new
render sign_up_from_twitter_users_path
end
else
@email = request.env["omniauth.auth"]["info"].email
case result[:status]
when :success
@user = result[:data]
sign_in_and_redirect @user, :event => :authentication
when :first_login
@user = User.new
render nickname_new_users_path
when :duplicated
@user = User.find_by_email(@email)
render merge_users_path
end
end
def authenticate(provider)
@user = User.new(
provider: provider,
uid: request.env["omniauth.auth"].uid)

result = User.find_for_oauth(provider, request.env["omniauth.auth"])

case provider
when :twitter
@user.nickname = request.env["omniauth.auth"]["extra"]["raw_info"].screen_name

case result[:status]
when :success
@user = result[:data]
sign_in_and_redirect @user, :event => :authentication

when :first_login
render sign_up_from_twitter_users_path
end

else
@user.email = request.env["omniauth.auth"]["info"].email

case result[:status]
when :success
@user = result[:data]
sign_in_and_redirect @user, :event => :authentication

when :first_login
render nickname_new_users_path

when :duplicated
@user = User.find_by_email(@user.email)
render merge_users_path

when :duplicated_by_oauth
flash[:notice] = result[:data] + " 서비스로 이미 회원가입 되어있습니다. 해당 서비스로 로그인해주세요"
redirect_to new_user_session_path
end
}
end
end

def twitter
authenticate(:twitter)
end

def facebook
authenticate(:facebook)
end

[:twitter, :facebook, :google_oauth2].each do |provider|
provides_callback_for provider
def google_oauth2
authenticate(:google_oauth2)
end
end
52 changes: 24 additions & 28 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,45 @@ def merge
end

def merge_callback
@user = User.find_by_email(params[:email])

if @user.valid_password?(params[:password])
@user.merge(@user.id, params[:provider], params[:uid])

sign_in_and_redirect @user, :event => :authentication
@user = User.find_by_email(params[:user].permit![:email])

if @user.valid_password?(params[:user].permit![:password])
if @user.merge(@user.id, params[:user].permit![:provider], params[:user].permit![:uid])
sign_in_and_redirect @user, :event => :authentication
else
render merge_users_path
end
else
redirect_to root_path
end
flash[:notice] = "Password is not valid!"
render merge_users_path
end
end

def nickname_new
end

def nickname_new_callback
@user = User.create!(
provider: params[:provider],
uid: params[:uid],
email: params[:email],
nickname: params[:nickname]
)

sign_in_and_redirect @user, :event => :authentication
@user = User.new(params[:user].permit!)

if @user.save
sign_in_and_redirect @user, :event => :authentication
else
render nickname_new_users_path
end

end

def sign_up_from_twitter
end

def sign_up_from_twitter_callback
if User.find_by_email(params[:user][:email])
@user = User.new
render sign_up_from_twitter_users_path
else
@user = User.new(
provider: params[:user][:provider],
uid: params[:user][:uid],
nickname: params[:user][:nickname]
)
@user = User.new(params[:user].permit!)

@user.email = params[:user][:email]

@user.save!
if @user.save
sign_in_and_redirect @user, :event => :authentication
end
else
render sign_up_from_twitter_users_path
end
end
end
8 changes: 6 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ def self.find_for_oauth(provider, access_token)

if user = User.where(uid: access_token.uid, provider: access_token.provider).first
return {data: user, status: :success}
elsif User.find_by_email(email)
return {data: nil, status: :duplicated}
elsif user = User.find_by_email(email)
if user.provider.nil?
return {data: nil, status: :duplicated}
else
return {data: user.provider, status: :duplicated_by_oauth}
end
else
return {data: nil, status: :first_login}
end
Expand Down
11 changes: 7 additions & 4 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
<%= 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>
Expand All @@ -21,6 +18,12 @@
</div>

<% if current_user.provider == nil %>

<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, 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" %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
<div class="col-md-6">
<h2>Sign in</h2>



<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>



<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
Expand Down Expand Up @@ -34,5 +39,9 @@
<div class="col-md-6">
<%= render "devise/shared/links" %>
</div>

<% if flash[:notice] %>
<p class="notice"><%= flash[:notice] %></p>
<% end %>
</div>
</div>
28 changes: 20 additions & 8 deletions app/views/users/merge.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@
<h1>중복되는 이메일이 있어요 => <%= @user.email %></h1>
<p>이 아이디와 통합하려면 기존 계정의 패스워드를 입력하세요</p>

<%= form_tag(merge_callback_users_path, method: "post") do %>
<%= password_field_tag(:password) %>

<%= hidden_field_tag(:provider, @provider) %>
<%= hidden_field_tag(:uid, @uid) %>
<%= hidden_field_tag(:email, @email) %>
<div class="form-container">
<%= form_for @user, url: merge_callback_users_path, method: "post" do |f| %>

<%= submit_tag("Submit", class: "btn btn-primary") %>
<%= f.error_messages %>

<% if flash[:notice] %>
<p class="notice"><%= flash[:notice] %></p>
<% end %>

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

<%= f.hidden_field :provider, value: @user.provider %>
<%= f.hidden_field :uid, value: @user.uid %>
<%= f.hidden_field :email, value: @user.email %>

<% end %>
<%= f.submit "Submit", class: "btn btn-primary" %>

<% end %>
</div>
24 changes: 16 additions & 8 deletions app/views/users/nickname_new.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@

<%= form_tag(nickname_new_callback_users_path, method: "post") do %>
<div class="form-container">
<%= form_for @user, url: nickname_new_callback_users_path, method: "post" do |f| %>

<p>처음이십니다. 새로운 닉네임을 입력하세요</p>

<%= text_field_tag(:nickname) %>
<%= f.error_messages %>

<% if flash[:notice] %>
<p class="notice"><%= flash[:notice] %></p>
<% end %>

<%= hidden_field_tag(:provider, @provider) %>
<%= hidden_field_tag(:uid, @uid) %>
<%= hidden_field_tag(:email, @email) %>
<div class="form-group">
<%= f.label :nickname %>
<%= f.text_field :nickname, autofocus: true, class: "form-control" %>
</div>

<%= f.hidden_field :provider, value: @user.provider %>
<%= f.hidden_field :uid, value: @user.uid %>
<%= f.hidden_field :email, value: @user.email %>

<%= f.submit "Submit", class: "btn btn-primary" %>

<%= submit_tag("Submit", class: "btn btn-primary") %>
<% end %>

</div>
13 changes: 10 additions & 3 deletions app/views/users/sign_up_from_twitter.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<h1>새 이메일을 입력하세요 - Twitter</h1>

<div class="form-container">
<%= form_for @user, url: sign_up_from_twitter_callback_users_path, method: "post" do |f| %>


<%= f.error_messages %>

<% if flash[:notice] %>
<p class="notice"><%= flash[:notice] %></p>
<% end %>

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

<%= f.hidden_field :provider, value: @provider %>
<%= f.hidden_field :uid, value: @uid %>
<%= f.hidden_field :nickname, value: @nickname %>
<%= f.hidden_field :provider, value: @user.provider %>
<%= f.hidden_field :uid, value: @user.uid %>
<%= f.hidden_field :nickname, value: @user.nickname %>

<%= f.submit :class => 'btn btn-success'%>

Expand Down

0 comments on commit 9166136

Please sign in to comment.