Created
April 3, 2016 03:27
-
-
Save mike-casas/0be1cd185b86b2da91c13cae95e7f1d2 to your computer and use it in GitHub Desktop.
Happy_seed omniauth with devise
This file contains hidden or 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 FormUser < User | |
attr_accessor :current_password | |
validates_presence_of :email, if: :email_required? | |
validates_uniqueness_of :email, allow_blank: true, if: :email_changed? | |
validates_format_of :email, with: Devise.email_regexp, allow_blank: true, if: :email_changed? | |
validates_presence_of :password, if: :password_required? | |
validates_confirmation_of :password, if: :password_required? | |
validates_length_of :password, within: Devise.password_length, allow_blank: true | |
def password_required? | |
return false if email.blank? | |
!persisted? || !password.nil? || !password_confirmation.nil? | |
end | |
def email_required? | |
true | |
end | |
end |
This file contains hidden or 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 Identity < ActiveRecord::Base | |
belongs_to :user | |
validates_presence_of :uid, :provider | |
validates_uniqueness_of :uid, :scope => :provider | |
validates_uniqueness_of :handler | |
def self.find_for_oauth(auth) | |
identity = find_by(provider: auth.provider, uid: auth.uid) | |
unless auth.info.email.nil? | |
identity = create(uid: auth.uid, provider: auth.provider) if identity.nil? | |
identity.oauth_token = auth.credentials.token | |
identity.oauth_expires_at = Time.at(auth.credentials.expires_at) | |
identity.name = auth.info.name | |
identity.email = auth.info.email | |
#identity.handler = auth.extra.raw_info.first_name.downcase | |
identity.image = auth.info.image | |
identity.age = Identity.year - Date.strptime(auth.extra.raw_info.birthday,'%m/%d/%Y').year.to_i | |
identity.birth_day = auth.extra.raw_info.birthday | |
identity.save | |
identity | |
end | |
end | |
private | |
def self.year | |
Date.today.year.to_i | |
end | |
end |
This file contains hidden or 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 | |
generic_callback('facebook') | |
end | |
# Other providers goes here. | |
# | |
# def twitter | |
# generic_callback('twitter') | |
# end | |
def generic_callback(provider) | |
@identity = Identity.find_for_oauth env["omniauth.auth"] | |
@user = @identity.user || current_user | |
if @user.nil? | |
@user = User.create(email: @identity.email || "") | |
@identity.update_attribute(:user_id, @user.id) | |
end | |
if @user.email.blank? && @identity.email | |
@user.update_attribute(:email, @identity.email) | |
end | |
if @user.persisted? | |
@identity.update_attribute(:user_id, @user.id) | |
@user = FormUser.find(@user.id) | |
session[:user_id] = @user.id | |
sign_in_and_redirect @user, event: :authentication | |
set_flash_message(:notice, :success, kind: provider.capitalize) if is_navigational_format? | |
else | |
session["devise.#{provider}_data"] = env["omniauth.auth"] | |
redirect_to new_user_registration_url | |
end | |
end | |
end |
This file contains hidden or 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 RegistrationsController < Devise::RegistrationsController | |
def update_resource(resource, params) | |
if resource.encrypted_password.blank? # || params[:password].blank? | |
resource.email = params[:email] if params[:email] | |
if !params[:password].blank? && params[:password] == params[:password_confirmation] | |
logger.info "updating password" | |
resource.password = params[:password] | |
resource.save | |
end | |
if resource.valid? | |
resource.update_without_password(params) | |
end | |
else | |
resource.update_with_password(params) | |
end | |
end | |
def destroy | |
@user = current_user | |
@identity = @user.identity | |
if @identity.destroy && @user.destroy | |
redirect_to root_url, notice: "User deleted :(" | |
end | |
end | |
end |
This file contains hidden or 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
Rails.application.routes.draw do | |
devise_for :users, class_name: 'FormUser', :controllers => { omniauth_callbacks: "omniauth_callbacks", registrations: "registrations"} | |
root to: 'home#index' | |
end |
This file contains hidden or 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 | |
devise :omniauthable, :database_authenticatable, | |
:recoverable, :rememberable, :trackable | |
# Change to (has_many :identities) for adding multible providers. | |
has_one :identity | |
def facebook | |
identities.where(:provider => "facebook").first | |
end | |
def facebook_client | |
@facebook_client ||= Facebook.client(access_token: facebook.access_token) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment