Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ApplicationRecord
- # Include default devise modules. Others available are:
- # :confirmable, :lockable, :timeoutable and :omniauthable
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable,
- :omniauthable, omniauth_providers: [:facebook]
- def self.from_omniauth(auth)
- where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
- user.email = auth.info.email
- user.provider = auth.provider
- user.uid = auth.uid
- user.password = Devise.friendly_token[0,20]
- end
- end
- end
- config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'],
- scope: 'email, public_profile', info_fields: 'email, first_name, last_name'
- Rails.application.routes.draw do
- devise_for :users, controllers: {omniauth_callbacks: 'users/omniauth_callbacks' }
- authenticated :user do
- root 'home#index', as: 'authenticated_root'
- end
- devise_scope :user do
- root 'devise/sessions#new'
- end
- end
- class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
- def facebook
- @user = User.from_omniauth(request.env["omniauth.auth"])
- sign_in_and_redirect @user
- end
- end
Add Comment
Please, Sign In to add comment