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
Related
I got a validation on my model to allow only some emails to log in with GoogleOauth but for some reasons, it does not redirect me to the root path
I got my admin Model :
class Admin < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :confirmable, :lockable, :registerable, :rememberable, :recoverable, :timeoutable,
:trackable, :validatable
devise :omniauthable, omniauth_providers: [:google_oauth2]
validates :email, :test_email
def self.from_omniauth(auth)
admin = Admin.find_or_initialize_by(email: auth.info.email)
admin.password = Devise.friendly_token.first(12)
admin.skip_confirmation!
admin.save!
admin
end
private
def test_email
errors.add(:email, message: 'Email is not correct') unless email.match?(/\A[a-z.\-]+(\+\d+)?#test\.fr?\z/)
end
end
And my OmniauthCallbacksController :
def google_oauth2
#admin = Admin.from_omniauth(request.env['omniauth.auth'])
if #admin.persisted?
flash[:notice] = 'success'
sign_in_and_redirect #admin
else
session['devise.google_data'] = request.env['omniauth.auth'].except('extra')
flash[:error] = #admin.errors.full_messages.join("\n")
redirect_to root_path
end
end
end
validates expects a boolean return. Just adding an error is not enough.
Try this:
def test_email
return true unless email.match?(/\A[a-z.\-]+(\+\d+)?#test\.fr?\z/)
errors.add(:email, message: 'Email is not correct')
false
end
I'm using oauth2 with devise and trying to login with instagram. But, when I login, it gives me this error:
You must include a valid client_id, response_type, and redirect_uri parameters
I have placed the client id in my application.yml file but its not working
instagram.rb
Instagram.configure do |config|
config.client_id = ENV['CLIENT_ID']
config.client_secret = ENV['CLIENT_SECRET']
end
callbacks_controller.rb
class CallbacksController < Devise::OmniauthCallbacksController
def instagram
#user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect #user
end
end
application.yml
CLIENT_ID : "221ea35dc7c1489c946e2f3062e8984a"
CLIENT_SECRET: "e8a100e6f1d44842a26f6bf7c4c5a063"
REDIRECT_URI : "http://localhost:3000/users/auth/instagram/callback"
routes.rb
Rails.application.routes.draw do devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
root 'products#index'
end
user.rb
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, :omniauth_providers => [:instagram]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.password = Devise.friendly_token[0,20]
end
end
...
end
After spending 30+ hours trying to get a solution to this and searching all corners of the earth, I am posting my first question here ever.
No matter what I try, I get
'The parameter app_id is required'
I've followed the docs to a T from https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
Here are the pieces I thought were most important...any ideas what I could be missing?
config/initializers/devise.rb
config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_APP_SECRET"], scope: 'user'
I've checked that the env params are correct on my system.
user.rb
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, omniauth_providers: [:facebook]
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
# user.name = auth.info.name # assuming the user model has a name
# user.image = auth.info.image # assuming the user model has an image
end
end
end
Try this instead :
config.omniauth :facebook, "xxxxx", "xxxx"
Using the actual values inside config/initializers/devise.rb worked for me.
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 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.