Selaa lähdekoodia

Few more tweaks

Chris Oliver 6 vuotta sitten
vanhempi
commit
21855e1ed4
2 muutettua tiedostoa jossa 81 lisäystä ja 68 poistoa
  1. 64 62
      app/controllers/users/omniauth_callbacks_controller.rb
  2. 17 6
      template.rb

+ 64 - 62
app/controllers/users/omniauth_callbacks_controller.rb

@@ -1,78 +1,80 @@
-class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
-  before_action :set_service
-  before_action :set_user
+module Users
+  class OmniauthCallbacksController < Devise::OmniauthCallbacksController
+    before_action :set_service
+    before_action :set_user
 
-  attr_reader :service, :user
+    attr_reader :service, :user
 
-  def facebook
-    handle_auth "Facebook"
-  end
+    def facebook
+      handle_auth "Facebook"
+    end
 
-  def twitter
-    handle_auth "Twitter"
-  end
+    def twitter
+      handle_auth "Twitter"
+    end
 
-  def github
-    handle_auth "Github"
-  end
+    def github
+      handle_auth "Github"
+    end
 
-  private
+    private
 
-  def handle_auth(kind)
-    if service.present?
-      service.update(service_attrs)
-    else
-      user.services.create(service_attrs)
+    def handle_auth(kind)
+      if service.present?
+        service.update(service_attrs)
+      else
+        user.services.create(service_attrs)
+      end
+
+      if user_signed_in?
+        flash[:notice] = "Your #{kind} account was connected."
+        redirect_to edit_user_registration_path
+      else
+        sign_in_and_redirect user, event: :authentication
+        set_flash_message :notice, :success, kind: kind
+      end
     end
 
-    if user_signed_in?
-      flash[:notice] = "Your #{kind} account was connected."
-      redirect_to edit_user_registration_path
-    else
-      sign_in_and_redirect user, event: :authentication
-      set_flash_message :notice, :success, kind: kind
+    def auth
+      request.env['omniauth.auth']
     end
-  end
 
-  def auth
-    request.env['omniauth.auth']
-  end
+    def set_service
+      @service = Service.where(provider: auth.provider, uid: auth.uid).first
+    end
 
-  def set_service
-    @service = Service.where(provider: auth.provider, uid: auth.uid).first
-  end
+    def set_user
+      if user_signed_in?
+        @user = current_user
+      elsif service.present?
+        @user = service.user
+      elsif User.where(email: auth.info.email).any?
+        # 5. User is logged out and they login to a new account which doesn't match their old one
+        flash[:alert] = "An account with this email already exists. Please sign in with that account before connecting your #{auth.provider.titleize} account."
+        redirect_to new_user_session_path
+      else
+        @user = create_user
+      end
+    end
 
-  def set_user
-    if user_signed_in?
-      @user = current_user
-    elsif service.present?
-      @user = service.user
-    elsif User.where(email: auth.info.email).any?
-      # 5. User is logged out and they login to a new account which doesn't match their old one
-      flash[:alert] = "An account with this email already exists. Please sign in with that account before connecting your #{auth.provider.titleize} account."
-      redirect_to new_user_session_path
-    else
-      @user = create_user
+    def service_attrs
+      expires_at = auth.credentials.expires_at.present? ? Time.at(auth.credentials.expires_at) : nil
+      {
+          provider: auth.provider,
+          uid: auth.uid,
+          expires_at: expires_at,
+          access_token: auth.credentials.token,
+          access_token_secret: auth.credentials.secret,
+      }
     end
-  end
 
-  def service_attrs
-    expires_at = auth.credentials.expires_at.present? ? Time.at(auth.credentials.expires_at) : nil
-    {
-        provider: auth.provider,
-        uid: auth.uid,
-        expires_at: expires_at,
-        access_token: auth.credentials.token,
-        access_token_secret: auth.credentials.secret,
-    }
-  end
+    def create_user
+      User.create(
+        email: auth.info.email,
+        #name: auth.info.name,
+        password: Devise.friendly_token[0,20]
+      )
+    end
 
-  def create_user
-    User.create(
-      email: auth.info.email,
-      #name: auth.info.name,
-      password: Devise.friendly_token[0,20]
-    )
   end
-
-end
+end

+ 17 - 6
template.rb

@@ -48,12 +48,12 @@ def add_users
 
   if requirement.satisfied_by? rails_version
     gsub_file "config/initializers/devise.rb",
-      /  # config.secret_key =/,
+      /  # config.secret_key = .+/,
       "  config.secret_key = Rails.application.credentials.secret_key_base"
   end
 
   # Add Devise masqueradable to users
-  inject_into_file("app/models/user.rb", "masqueradable, :", :after => "devise :")
+  inject_into_file("app/models/user.rb", "omniauthable, :masqueradable, :", after: "devise :")
 end
 
 def add_bootstrap
@@ -125,10 +125,21 @@ def add_multiple_authentication
 
     generate "model Service user:references provider uid access_token access_token_secret refresh_token expires_at:datetime auth:text"
 
-    insert_into_file "config/initializers/devise.rb",
-    "  if Rails.application.secrets.facebook_app_id.present? && Rails.application.secrets.facebook_app_secret.present? then (config.omniauth :facebook, Rails.application.secrets.facebook_app_id, Rails.application.secrets.facebook_app_secret, scope: 'email,user_posts') end \n
-    if Rails.application.secrets.twitter_app_id.present? && Rails.application.secrets.twitter_app_secret.present? then (config.omniauth :twitter, Rails.application.secrets.twitter_app_id, Rails.application.secrets.twitter_app_secret) end \n
-    if Rails.application.secrets.github_app_id.present? && Rails.application.secrets.github_app_secret.present? then (config.omniauth :github, Rails.application.secrets.github_app_id, Rails.application.secrets.github_app_secret) end\n",
+    template = """
+  if Rails.application.secrets.facebook_app_id.present? && Rails.application.secrets.facebook_app_secret.present?
+    config.omniauth :facebook, Rails.application.secrets.facebook_app_id, Rails.application.secrets.facebook_app_secret, scope: 'email,user_posts'
+  end
+
+  if Rails.application.secrets.twitter_app_id.present? && Rails.application.secrets.twitter_app_secret.present?
+    config.omniauth :twitter, Rails.application.secrets.twitter_app_id, Rails.application.secrets.twitter_app_secret
+  end
+
+  if Rails.application.secrets.github_app_id.present? && Rails.application.secrets.github_app_secret.present?
+    config.omniauth :github, Rails.application.secrets.github_app_id, Rails.application.secrets.github_app_secret
+  end
+    """.strip
+
+    insert_into_file "config/initializers/devise.rb", "  " + template + "\n\n",
           before: "  # ==> Warden configuration"
 end