From dd8f5c454a8f1a407f42746fbe1a3535e5542627 Mon Sep 17 00:00:00 2001 From: YOUR NAME Date: Fri, 13 Oct 2017 15:54:52 +1100 Subject: [PATCH] facebook authenticate --- .../users/omniauth_callbacks_controller.rb | 18 ++++++++++++++++++ app/views/devise/shared/_links.html.erb | 7 ++++--- app/views/homepages/index.html.erb | 6 +++++- config/database.yml | 8 ++++---- config/initializers/devise.rb | 2 +- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index e69de29..722e319 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -0,0 +1,18 @@ +class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController + def facebook + # You need to implement the method below in your model (e.g. app/models/user.rb) + @user = User.from_omniauth(request.env["omniauth.auth"]) + + if @user.persisted? + sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated + set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? + else + session["devise.facebook_data"] = request.env["omniauth.auth"] + redirect_to new_user_registration_url + end + end + + def failure + redirect_to root_path + end +end diff --git a/app/views/devise/shared/_links.html.erb b/app/views/devise/shared/_links.html.erb index e6a3e41..6f727e9 100644 --- a/app/views/devise/shared/_links.html.erb +++ b/app/views/devise/shared/_links.html.erb @@ -19,7 +19,8 @@ <% end -%> <%- if devise_mapping.omniauthable? %> - <%- resource_class.omniauth_providers.each do |provider| %> - <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %>
- <% end -%> +<%- resource_class.omniauth_providers.each do |provider| %> + <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %>
+ <% end -%> + <% end -%> diff --git a/app/views/homepages/index.html.erb b/app/views/homepages/index.html.erb index 024f4d7..ddb3c5b 100644 --- a/app/views/homepages/index.html.erb +++ b/app/views/homepages/index.html.erb @@ -10,6 +10,8 @@ + + diff --git a/config/database.yml b/config/database.yml index cb10711..0d02f24 100644 --- a/config/database.yml +++ b/config/database.yml @@ -5,21 +5,21 @@ # gem 'sqlite3' # default: &default - adapter: postgresql + adapter: sqlite3 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> timeout: 5000 development: <<: *default - database: db/development.postgresql + database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: db/test.postgresql + database: db/test.sqlite3 production: <<: *default - database: db/production.postgresql + database: db/production.sqlite3 diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index d6e3c97..d9f755c 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -251,7 +251,7 @@ Devise.setup do |config| # Add a new OmniAuth provider. Check the wiki for more information on setting # up on your models and hooks. # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' - config.omniauth :facebook, "APP_ID", "APP_SECRET" + config.omniauth :facebook, "137009576938168", "5fb495fe5da4aad7d7e8f3863018c527" # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or # change the failure app, you can configure them inside the config.warden block.