Commit 07bf996d by Mykhailo Makohin

fix facebook registration

parent 3723122c
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
/tmp/* /tmp/*
!/log/.keep !/log/.keep
!/tmp/.keep !/tmp/.keep
/vendor/*
# Ignore Byebug command history file. # Ignore Byebug command history file.
.byebug_history .byebug_history
...@@ -3,8 +3,8 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController ...@@ -3,8 +3,8 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
@user = User.from_omniauth(request.env["omniauth.auth"]) @user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted? if @user.persisted?
sign_in_and_redirect @user, :event => :authentication 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? set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
else else
session["devise.facebook_data"] = request.env["omniauth.auth"] session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url redirect_to new_user_registration_url
...@@ -12,6 +12,6 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController ...@@ -12,6 +12,6 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
end end
def failure def failure
redirect_to templates_path redirect_to new_template_path
end end
end end
\ No newline at end of file
class User < ApplicationRecord class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable :recoverable, :rememberable, :omniauthable, omniauth_providers: [:facebook]
devise :omniauthable, :omniauth_providers => [:facebook]
def self.new_with_session(params, session) def self.new_with_session(params, session)
...@@ -16,10 +14,13 @@ end ...@@ -16,10 +14,13 @@ end
def self.from_omniauth(auth) def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user| where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email # user.email = auth.info.email
user.password = Devise.friendly_token[0,20] user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image user.image = auth.info.image # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end end
end end
end end
...@@ -27,4 +27,7 @@ ...@@ -27,4 +27,7 @@
<br> <br>
<%= link_to 'New Template', new_template_path %> <%= link_to 'New Template', new_template_path %>
<% unless current_user %> <%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %><% else %> <%= current_user[:name] %> <%= link_to "Logout", destroy_user_session_path, method: :delete %><% end %> <% unless current_user %> <%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %><% else %> <%= current_user[:name] %> <%= link_to "Logout", destroy_user_session_path, method: :delete %><% end %>
\ No newline at end of file
<%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %>
\ No newline at end of file
...@@ -256,47 +256,20 @@ Devise.setup do |config| ...@@ -256,47 +256,20 @@ Devise.setup do |config|
# The default HTTP method used to sign out a resource. Default is :delete. # The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :delete config.sign_out_via = :delete
# ==> OmniAuth # config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET'],
# Add a new OmniAuth provider. Check the wiki for more information on setting # scope: 'public_profile,email',
# up on your models and hooks. # info_fields: 'email,first_name,last_name,gender,birthday,location,picture',
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' # client_options: {
# site: 'https://graph.facebook.com/v2.11',
# ==> Warden configuration # authorize_url: "https://www.facebook.com/v2.11/dialog/oauth"
# 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.
# config.omniauth :facebook, "2429624190692901", "03da24c1517b0bfa4acc70852f61fb60",
# config.warden do |manager| callback_url: "http://localhost:3000/users/auth/facebook/callback",
# manager.intercept_401 = false scope: 'public_profile,email',
# manager.default_strategies(scope: :user).unshift :some_external_strategy info_fields: 'email,first_name,last_name,gender,birthday,location,picture',
# end client_options: {
site: 'https://graph.facebook.com/v2.11',
# ==> Mountable engine configurations authorize_url: "https://www.facebook.com/v2.11/dialog/oauth"
# When using Devise inside an engine, let's call it `MyEngine`, and this engine }
# is mountable, there are some extra configurations to be taken into account.
# The following options are available, assuming the engine is mounted as:
#
# mount MyEngine, at: '/my_engine'
#
# The router that invoked `devise_for`, in the example above, would be:
# config.router_name = :my_engine
#
# When using OmniAuth, Devise cannot automatically set OmniAuth path,
# so you need to do it manually. For the users scope, it would be:
# config.omniauth_path_prefix = '/my_engine/users/auth'
# ==> Turbolinks configuration
# If your app is using Turbolinks, Turbolinks::Controller needs to be included to make redirection work correctly:
#
# ActiveSupport.on_load(:devise_failure_app) do
# include Turbolinks::Controller
# end
# ==> Configuration for :registerable
# When set to false, does not sign a user in automatically after their password is
# changed. Defaults to true, so a user is signed in automatically after changing a password.
# config.sign_in_after_change_password = true
config.omniauth :facebook, "2429624190692901", "03da24c1517b0bfa4acc70852f61fb60", token_params: { parse: :json }
callback_url: "http://localhost:3000/users/auth/facebook/callback"
# config.omniauth :facebook, "APP_ID", "APP_SECRET", token_params: { parse: :json }
end end
Rails.application.routes.draw do Rails.application.routes.draw do
resources :templates resources :templates
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment