I followed the devise official wiki to integrate devise with facebook through omniauth.
I am able to login with my facebook account but somehow I am unable to save additional data from facebook's omniauth callback.
Note that I literally copied everything on this guide
Please look at my current user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
:fb_raw
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:email => data.email).first
user
else # Create a user with a stub password.
User.create!(:email => data.email, :password => Devise.friendly_token[0,20])
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && \
session["devise.facebook_data"]["extra"]["raw_info"]
# Here you can save all the info you want including networks and education
user.email = data["email"]
user.fb_raw = data['raw_info']
end
end
end
end
The problem here is that self.new_with_session is never invoked. I placed a throw exception clause right before super.tab to prove that my hypothesis.
Does anyone know a work around this?
The new_with_session is call on DeviseRegistrationController so it's not in OmniauthCallbacksController so it's normal to never be called during the login by Facebook.
Related
I'm using rails 5.2 and devise 4.4.3, I have confirmable working, however, I'm trying to have it so when the user confirms their account they get sent another email welcoming them to the website.
On sign up the confirmation email sends and confirmation can take place by clicking the link in the email but the welcome email that I want to be sent after this currently doesn't send.
The code I have is like so, I have generated a UserMailer and done the following
user.rb model
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_one_attached :avatar
def confirm!
welcome_email
super
end
protected
def welcome_email
UserMailer.welcome_email(self).deliver
end
end
views/user_mailer/welcome_email.html.erb
<h2>Welcome <%= #user.email %></h2>
mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def welcome_email(user)
#user = user
mail(to: #user.email, subject: "Welcome! You are awesome!")
end
end
You can use the after_confirmation callback instead of trying to override confirm.
def after_confirmation
UserMailer.welcome_email(self).deliver
end
def after_confirmation
UserMailer.with(user: #user, password: #user.password).welcome_email.deliver_later
end
I am trying to implement reddit login in rails application using omniauth-reddit
login is succesfull but i don't no where its going wrong
My OmniauthCallbacksController.rb file
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def reddit_oauth2
#user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect root_path
end
My User Model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:reddit]
def self.from_omniauth(auth)
where(provider: auth.provider,uid: auth.id).first_or_create do |user|
user.provider = auth.provider
user.id = auth.id
user.password = Devise.friendly_token[0,20]
end
end
end
can anybody suggest me scope or solution ?? Thanks in Advance
i have created a web app with rails4 and authentication system is developed with devise_ldap_authenticable gem.
where i am using username for login not email. but i want to store email in my users table.
My user model is
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
##attr_accessible :email, :password, :password_confirmation
# not required for LDAP :recoverable, :registerable, :validatable
devise :ldap_authenticatable, :rememberable, :trackable
validates_uniqueness_of :email, :allow_blank => true
before_save :get_ldap_email
def get_ldap_email
self.email = Devise::LDAP::Adapter.get_ldap_param(self.username, "mail")
end
end
But in users table of email field its storing data like
`email` = '--- !ruby/array:Net::BER::BerIdentifiedArray\ninternal:\n- !ruby/string:Net::BER::BerIdentifiedString\n str: !binary |-\n cy5naG9zaEBzYW1zdW5nLmNvbQ==\n ber_identifier: 4\nivars:\n :#ber_identifier: 49\n'
My log says
LDAP: Requested param mail has value ["s.ghosh#example.com"]
How i will store this value to my users table. where im doing wrong? please help me out.
Just need to add the following then its totally ok
def get_ldap_email
self.email = Devise::LDAP::Adapter.get_ldap_param(self.username, "mail").first
end
In my Rails app, I have user and authorization tables to handle users and auth data. I set up both Devise and Omniauth to use Twitter to sign up, it redirects to Twitter, but after returning to my app, it gives an error like:
NoMethodError at /users/auth/twitter/callback
undefined method `authorizations' for #<Class:0xbdc8100>
In which side, did I go wrong and how can I fix this issue?
Here are related parts: omniauth_callbacks_controller.rb:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.authorizations.from_auth(auth_hash)
if user.persisted?
flash.notice = "Signed in!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :twitter, :all
protected
def auth_hash
request.env['omniauth.auth']
end
end
authorization.rb:
class Authorization < ActiveRecord::Base
attr_accessible :uid, :provider
belongs_to :user
def self.from_auth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
end
end
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:twitter, :facebook]
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name
# attr_accessible :title, :body
has_many :authorizations, dependent: :destroy
end
Your issue is in this line...
user = User.authorizations.from_auth(auth_hash)
You call authorizations on the class User, but as an attribute it needs to be called on an instance of the User class, i.e. a specific user.
I can find recommendations for testing devise user controllers and views in RSpec. I've also seen suggestions that the devise gem code is already tested so it's not useful to spend a lot of time reinventing the wheel.
However, my user model has other fields that I need validated when the user signs up. I'm using standard validates... statements in the user.rb model. For example:
validates_presence_of :nickname
I'm trying to use simple validation testing in my user_spec.rb, but when I try to create the user like this:
record = Factory.create(:user)
I get this error:
undefined method `encode!' for "Confirmation":String
The encode! method is not coming from my code, it must be one of the gems that devise is using, but I haven't been able to find it, yet.
I've tried creating the user using User.new and Factory Girl. I get the same error either way. This spec was passing until I did an update of all my gems. Unfortunately I didn't keep a note of everything that got updated at the time. I've tried rolling devise back to previous versions but still get the same error.
Rails 3, RSpec2
Thanks for any advice.
It seems to be fine, may be, my testing code helps you out:
user_spec.rb
require 'spec_helper'
describe User do
before :each do
#user = Factory.build(:user)
end
it "should not be valid without a first_name" do
#user.first_name = nil
#user.should_not be_valid
end
end
user.rb (Model)
class User < ActiveRecord::Base
validates_presence_of :first_name
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, :lockable,
:recoverable, :rememberable, :trackable
# Setup accessible (or protected) attributes for your model
attr_accessible :login, :first_name, :email, :password, :password_confirmation, :remember_me
attr_accessor :login
devise :database_authenticatable, :recoverable, :validatable
protected
def password_required?
!persisted? || password.present? || password_confirmation.present?
end
end