Created
December 9, 2013 19:42
-
-
Save dylanerichards/7879468 to your computer and use it in GitHub Desktop.
I am trying to incorporate Devise, Omniauth, and Facebook into my Rails 4 application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Devise.setup do |config| | |
require "omniauth-facebook" | |
config.omniauth :facebook, "186855861506807", "d35a2cab845ada8ff2036c6f7b3281cd" | |
config.secret_key = '1a1e48f23c2756567caf48b37bd659a87f28fa18c8a9a5eb47d7b595e222c82b57787e66be3d51a515f7c8d43a2bbeb721a412830d6247ba144a32ad14755610' | |
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' | |
require 'devise/orm/active_record' | |
config.case_insensitive_keys = [ :email ] | |
config.strip_whitespace_keys = [ :email ] | |
config.skip_session_storage = [:http_auth] | |
config.stretches = Rails.env.test? ? 1 : 10 | |
config.reconfirmable = true | |
config.reset_password_within = 6.hours | |
config.sign_out_via = :delete | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def facebook | |
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) | |
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 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Stynyl::Application.routes.draw do | |
devise_for :admin_users, ActiveAdmin::Devise.config | |
ActiveAdmin.routes(self) | |
resources :things | |
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" } | |
resources :users, only: [:show, :new, :create] do | |
member do | |
get :following, :followers | |
end | |
end | |
resources :relationships, only: [:create, :destroy] | |
get '/about', to: 'pages#about' | |
get '/myfeed', to: 'pages#myfeed' | |
root 'things#index' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
# Include default devise modules. Others available are: | |
# :confirmable, :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, #:recoverable, | |
:rememberable, :trackable, :validatable, :omniauthable | |
has_many :things | |
validates :name, presence: true, length: { minimum: 2, maximum: 20} | |
validates :username, presence: true, length: { minimum: 2, maximum: 20} | |
validates :username, uniqueness: true | |
validates :email, presence: true | |
validates :email, uniqueness: true | |
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" , :nav => "25x25"} | |
extend FriendlyId | |
friendly_id :username | |
def show | |
end | |
#follow features | |
has_many :followed_users, through: :relationships, source: :followed | |
has_many :relationships, foreign_key: "follower_id", dependent: :destroy | |
has_many :followed_users, through: :relationships, source: :followed | |
def following?(other_user) | |
relationships.find_by(followed_id: other_user.id) | |
end | |
def follow!(other_user) | |
relationships.create!(followed_id: other_user.id) | |
end | |
def unfollow!(other_user) | |
relationships.find_by(followed_id: other_user.id).destroy! | |
end | |
def feed | |
Thing.from_users_followed_by(self) | |
end | |
has_many :reverse_relationships, foreign_key: "followed_id", | |
class_name: "Relationship", | |
dependent: :destroy | |
has_many :followers, through: :reverse_relationships, source: :follower | |
def self.find_for_facebook_oauth(auth, signed_in_resource=nil) | |
user = User.where(:provider => auth.provider, :uid => auth.uid).first | |
if user | |
return user | |
else | |
registered_user = User.where(:email => auth.info.email).first | |
if registered_user | |
return registered_user | |
else | |
user = User.create(name:auth.extra.raw_info.name, | |
provider:auth.provider, | |
uid:auth.uid, | |
email:auth.info.email, | |
password:Devise.friendly_token[0,20], | |
) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment