Guest User

Untitled

a guest
Oct 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. class User < ApplicationRecord
  2. # Include default devise modules. Others available are:
  3. # :confirmable, :lockable, :timeoutable and :omniauthable
  4. devise :database_authenticatable, :registerable,
  5. :recoverable, :rememberable, :trackable, :validatable,
  6. :omniauthable, omniauth_providers: [:facebook]
  7.  
  8. def self.from_omniauth(auth)
  9. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  10. user.email = auth.info.email
  11. user.provider = auth.provider
  12. user.uid = auth.uid
  13. user.password = Devise.friendly_token[0,20]
  14. end
  15. end
  16. end
  17.  
  18. config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'],
  19. scope: 'email, public_profile', info_fields: 'email, first_name, last_name'
  20.  
  21. Rails.application.routes.draw do
  22.  
  23. devise_for :users, controllers: {omniauth_callbacks: 'users/omniauth_callbacks' }
  24.  
  25. authenticated :user do
  26. root 'home#index', as: 'authenticated_root'
  27. end
  28.  
  29. devise_scope :user do
  30. root 'devise/sessions#new'
  31. end
  32.  
  33. end
  34.  
  35. class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  36.  
  37. def facebook
  38. @user = User.from_omniauth(request.env["omniauth.auth"])
  39. sign_in_and_redirect @user
  40. end
  41. end
Add Comment
Please, Sign In to add comment