Guest User

Untitled

a guest
Nov 15th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. Rails.application.routes.draw do
  2. root 'pages#home'
  3.  
  4. devise_for :users,
  5. path: '',
  6. path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
  7. contollers: {:omniauth_callbacks => 'users/omniauth_callbacks'}
  8.  
  9. class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  10.  
  11. if @user.persisted?
  12. sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
  13. set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
  14. else
  15. session["devise.facebook_data"] = request.env["omniauth.auth"]
  16. redirect_to new_user_registration_url
  17. end
  18. end
  19.  
  20. def failure
  21. redirect_to root_path
  22. end
  23.  
  24. class User < ApplicationRecord
  25. # Include default devise modules. Others available are:
  26. # :confirmable, :lockable, :timeoutable and :omniauthable
  27. devise :database_authenticatable, :registerable,
  28. :recoverable, :rememberable, :trackable, :validatable,
  29. :confirmable, :omniauthable, :omniauth_providers => [:facebook]
  30.  
  31. validates :fullname, presence: true, length: {maximum: 50}
  32.  
  33. def self.from_omniauth(auth)
  34. user = User.where(email: auth.info.email).first
  35.  
  36. if user
  37. return user
  38. else
  39. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  40. user.email = auth.info.email
  41. user.password = Devise.friendly_token[0,20]
  42. user.fullname = auth.info.name # assuming the user model has a name
  43. user.image = auth.info.image # assuming the user model has an image
  44. user.uid = auth.uid
  45. user.provider = auth.provider
  46.  
  47. # If you are using confirmable and the provider(s) you use validate emails,
  48. # uncomment the line below to skip the confirmation emails.
  49. user.skip_confirmation!
  50. end
  51. end
  52. end
  53. end
Add Comment
Please, Sign In to add comment