Multiple, Simultaneous Oauth in a Rails Application? - ruby-on-rails

My end goal is for users to have multiple 3rd party authentications at the same time.
Right now, I am using Devise to create users. Users can sign up via email or facebook or google and it works. But now, after they have already signed up, I need them to also verify with, say, youtube or soundcloud. So the user was created with devise, but I also need them to verify with other things.
Since Devise hogs omniauth for it's own purposes, I can't use omniauth on the side.
As I see it I have three options:
Try to monkeypatch devise and get it to allow multiple authentications at the same time on one user
Do oauth by hand on the side adjacent to current Devise implementation
Scrap Devise and do something different
I would greatly appreciate any advice or other options

I think this may be what you need: http://blog.joshsoftware.com/2010/12/16/multiple-applications-with-devise-omniauth-and-single-sign-on/
They open sourced their code too!
Provider: https://github.com/joshsoftware/sso-devise-omniauth-provider
Client: https://github.com/joshsoftware/sso-devise-omniauth-client
Or even better, check out this: http://communityguides.heroku.com/articles/16

Try to monkeypatch devise and get it to allow multiple authentications at the same time on one use
You don't need to monkeypatch devise --- you can have your own oauth controller the has
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# handle if already a twitter user
# handle if a new user
# use the `sign_in user` to sign_in the user
end
def twitter
# handle if already a facebook user
# handle if a new user
end
end
and use it in routes
devise_for :user,
:controllers => {
:omniauth_callbacks => "users/omniauth_callbacks"
}

Related

Rails Devise redirect after signup

I have rails app that uses Devise with confirmable by email. Emails are send thru gmail. When user fill registration form then is redirected to "/user", but this path doesn't exists hence app crashes. Is there anything that I can do, to force redirect after submit to index?
Yes I've already tried Devise Wiki
thanks
Before you ask such questions please take some time and search for the solution by yourself.
It took me 30 seconds to find the Solution on a Wiki page of Devise.
Just create a RegistrationsController which inherits from Devise::RegistrationsController and add a #after_sign_up_path_for method to it.
Then just add devise_for :users, controllers: { registrations: "registrations" } to your routes if you have users as resource.

How shopify_api work with devise

I am designing a shopify app which enables customers buying product through mobile phones. The scenario is customers need to be able to sign up with omniauth and then they can get the products info from the app. However, before any customer can get products info from a shop, my shopify app should first authenticate with the shop owner using omniauth.
The problem now is devise will modify omniauth default authenticate strategy. If I use shopify_api, I auth through path auth/shopify, it can work without devise installed. If devise is installed, it will redirect auth/shopify to omniauth/shopify. I find this path is generated by devise. How can I skip devise and use the original omniauth path? Thanks.
Devsie has a good tutorial on how to separate omniauthable out of your devise model. This will allow you to configure your own omniauth settings.
Once you've set-up a provider for both providers in config/initializers/omniauth.rb and removed devise.omniauth from conifg/initializers/devise.rb, you'll need to set-up your routes to handle the response from OAuth differently.
routes.rb
devise_scope :user do
get "/auth/:action/callback", to: 'users/omniauth_callbacks', constraints: { action: /facebook/ }
end
get 'auth/:action/callback' => 'another_controller', constraints: { action: /shopify/ } # connections
Then in another_controller.rb
class AnotherController
def shopify
auth_hash = request.env['omniauth.auth']
shop = auth_hash[:uid]
token = auth_hash[:credentials][:token]
ShopifyAPI::Session.temp("#{shop}.myshopify.com", token) do
current_shop = ShopifyAPI::Shop.current
...
end
end
end
Hope this helps.

With Omniauth, How to record all requests for authentication?

with omniauth in my app, to have a user use Google oAuth2 to authenticate I redirect the user to:
/users/auth/google_oauth2
If the users approves the request, then the AuthenticationsController#create is called.
With AuthenticationsController#create - I can add event tracking to record the # of users who approve google auth. What I don't have is the number that I sent to approve meaning I don't have a conversion rate.
How can I track the # of people who hit the URL around making requests to connect.
A nasty solution would be to build a filter around the method Strategy#request_call and do the tracking there.
Inside an initializer:
OmniAuth::Strategy.class_eval do
def request_call_with_tracking
log :info, "Im running before the actual request_call"
Tracker.hit(name) #name will return the provider
request_call_without_tracking
end
alias_method_chain :request_call, :tracking
end
You can achieve this by using the OmniAuth setup phase. You can pass a :setup option to an OmniAuth provider, with a proc which will be executed before the authentication is performed. You can add event tracking inside this proc.
So if you have some tracker class, you can do this:
use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'],
:setup => lambda { |env|
Tracker.track
}
end
For more information check out Avdi Grimm's great blog post about the subject.

How to access users who did not confirmed and those who confirmed (Rails/Devise/cancan/rolify)

I am building a daily deal app on Rails to train myself to Ruby on Rails.
I have installed authentication with devise/cancan/rolify.
I'd like to create in cancan two type of users
users who confirmed
users who did not confirmed yet
How can I achieve that ? how can I access on devise users who have and those who have not confirmed their account(i.e clicked on the activation link sent to them by email).
There is no need to add roles for confirmed and unconfirmed. You can use user.confirmed? in your ability.rb file to control authorization:
# models/ability.rb
if user.confirmed?
can :manage, Model
end
if !user.confirmed?
can :view, Model
end
Note: you can use an if/else construct, but I prefer to keep my rules nicely separated.
In regards to your comments, you're reimplementing what's already been done. With cancan you can use load_and_authorize_resource (see: here).
class ProductsController < ActionController::Base
load_and_authorize_resource
end
That's it. The user will receive an "unauthorized" response if they try to access without the required permissions.
I highly recommend you read through the documentation for rolify and cancan.

How to authenticate Token with devise

I want to use devise' token_authenticatable helper to authenticate users against the system.
I found some older documentations where a method named valid_authentication_token?(...) is used but couldn't find the same in newer devise version.
So what's the right way to authenticate a user?
Should I request the Model for user with named token and checking if email-adresses match?
Thanks a lot for your help.
PascalTurbo
If you add
t.token_authenticatable
to you user ActionRecord, and add
devise :token_authenticatable
to your User model
and specify which param is your token key in config/initializer/devise, something like this:
config.token_authentication_key = :auth_token
then controllers that use
before_filter :authenticate_user! # Tell devise to use :user map
to authenticate. after the authenticate_user!, individual methods can test using
user_signed_in?
will authorize users either by the login session or the devise authorization token that is passed on the query string or passed using HTTP basic authentication. See Devise helper code for details.

Resources