Authenticating Rails Application With Omniauth Github in Local - ruby-on-rails

I am working on authenticating users using GitHub in local development mode.
I am using using omniauth-github Rubygem.
I have bellow code in config/initializers/omniauth.rb
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'],
{
:client_options => {
:site => 'https://github.com/api/v3',
:authorize_url => 'https://github.com/login/oauth/authorize',
:token_url => 'https://github.com/login/oauth/access_token',
}
}
end
In view layout I have
<%= link_to "Sign in with Github", "/auth/github" %>
In routes.rb I have
match 'auth/:provider/callback' => 'session#create', :via => [:get, :post]
match 'signout' => 'session#destroy', :via => [:delete], :as => 'signout'
I have registered new application in github with
Homepage URL : http://localhost:3000
, Authorization callback URL : http://localhost:3000/callback
After clicking on Sign in with Github, I am getting following error.
http://localhost:3000/callback?error=redirect_uri_mismatch&error_description=The redirect_uri MUST match the registered callback URL for this application.&error_uri=https://developer.github.com/v3/oauth/#redirect-uri-mismatch&state=14216a2416431297d9690e68efe0723a03aa7a1eaee51db3
Kindly help me to set proper values for site ,authorize_url , token_url to work in local system.

Related

How to add ScriptTag on shopify_api gem?

Hi Everyone,
I came across on a problem that I can't really figure out myself, It believe that all your expertise can help me through solving this error I get whenever I try to access a route to add a script. Here is my controller code:
class HomeController < AuthenticatedController
def index
#products = ShopifyAPI::Product.find(:all, :params => {:product_type => "Underarmour"})
# script = ShopifyAPI::ScriptTag.new(:all, :params => {:event => "onload", :src => "https://shopperapproved.herokuapp.com/sajs/14043.js"})
end
def script
ShopifyAPI::ScriptTag.create(:event => "onload", :src => "https://shopperapproved.herokuapp.com/sajs/14043.js")
end
end
and my route file is:
controller :sessions do
get 'login' => :new, :as => :login
post 'login' => :create, :as => :authenticate
get 'auth/shopify/callback' => :callback
get 'logout' => :destroy, :as => :logout
end
root :to => 'home#index'
match "script/",
:to => "home#script",
:via => :get
I want to add a script by accessing this route: on my index view:
<h3>Add your ShopperApproved site ID:</h3>
https://shopperapproved.herokuapp.com/script --> if i am going to click this link i will be redirected to HomeController#script
I hope you can help me..
Have you set up a seesion & token? i fnot you cant create the script in your store
https://github.com/Shopify/shopify_api
I assume you did not set up a session so you cant "connect" to your store.
Verify that you have a valide session and it will work

Incorrect routes for Devise + LinkedIn (OmniAuth)

I am trying to implement sign up through LinkedIn to the current Devise gem. These are the current routes:
devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret',
:confirmation => 'verification', :unlock => 'unlock', :registration => 'register',
:sign_up => 'signup' }, :controllers => {:omniauth_callbacks => "omniauth_callbacks"}
And the view: = link_to "Sign in with Linkedin",user_omniauth_authorize_path(:linkedin)
Returns to this error:
No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:linkedin, :format=>nil} missing required keys: [:provider]
I've tried to add provider key too, like: = link_to "Sign in with Linkedin",user_omniauth_authorize_path(:provider => 'linkedin')
But then I got:
No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>"linkedin"} missing required keys: [:provider]
What am I missing at this point?
Thank you very much
Add the line in devise.rb
config.omniauth :linkedin, 'APP_ID', 'APP_SECRET'
Devise will automatically add a signin link using linkedin.
In omniauth_callbacks_controller.rb add a method as:-
def linkedin
#code for authorization using linkedin callback credentials
end

Rails 4, Devise Routes

I've created custom routes to route to the devise login and logout paths:
devise_scope :admin do
get "logout" => "devise/sessions#destroy", as: :logout
get "login" => "devise/sessions#new", as: :login
end
This works. The only problem is that if the the login fails it redirects back to admins/sign_in instead of /login.
Any ideas?
According to this answer and this description, it seems the proper way to achieve what you're attempting to do is to make use of the :path_names option. According to the description from the Devise wiki:
devise_for :admin, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}
will create the normal admin routes for you, and will assign the /sign_in and /sign_out route to /login/ and /logout respectively.
Using the :path option, you can further alter the URL, such as using :path=>"admins" will yield routes like /admin/login, etc.

Issue with using devise gem authentication with Mobile app (Titanium)

I am using Devise gem in my Rails app for authentication. I could able to create user by passing data in Json request format to devise rails app from Titanium mobile application.
But when i try to set up login in mobile app to use devise, it shows an error like below
{"error":"You need to sign in or sign up before continuing."}
My code looks like below
var regDetails = '{"email": "'+userNmTxt.value+'", "password": "'+passwordTxt.value+'"}';
var client = Titanium.Network.createHTTPClient({timeout: 5000});
client.open("POST", "http://10.100.85.43:3000/session/create");
client.setRequestHeader("Content-Type", "application/json");
client.send(regDetails);
client.onload = function(e)
{
alert('Logged in successfully' + this.responseText);
}
client.onerror = function(e)
{
alert('On error' + this.responseText);
}
What am i doing wrong?
I found the solution, i was missing some routing information in routes.rb file
devise_scope :user do
match "/session/create" => "session#create" #, :via => [:get, :post]
match "/session/destroy" => "session#destroy" #, :via => [:get, :post]
match "/registration/create" => "registration#create" #, :via => [:get, :post]
end
Hope this will help someone :)

Is there an equivalent of "after_sign_in_path_for" for user edit/commit?

Running Rails 3.2.1 with devise-2.0.4.gem.
Is there an equivalent redirect configuration for user/commit similar to "after_sign_in_path_for"? In production, I have to use HTTPS so the URL for "Edit User" is https://www.xyz.com/users/edit. When I click "Update", the correct update takes place but then Devise redirects to "http://www.xyz.com/users/edit" which results in error loading page since HTTP is not supported in production.
I had a similar issue with sign in/out (http://groups.google.com/group/plataformatec-devise/browse_thread/thread/5fafb2a8c90f1d43) which I solved by defining after_sign_in_path_for. But I don't see such similar config for user edit/commit.
Then I tried to force HTTPS in routes.rb:
devise_scope :user do
get "users/edit", :to => "users/registrations#edit", :as => :edit_user, :protocol => "https"
put "users/commit", :controller => "users/registrations", :action => 'commit', :as => :commit_user, :protocol => "https"
end
And see this in rake routes:
edit_user GET /users/edit(.:format) users/ registrations#edit {:protocol=>"https"}
commit_user PUT /users/commit(.:format) users/registrations#commit {:protocol=>"https"}
But Devise still routes to HTTP after the update action.
Looking at registrations_controller.rb, I see several instances of
redirect_to edit_user_path
So as a work around, I change it to
redirect_to https://www.xyz.com/user/edit
And that is working. But I'm not sure if this is the correct approach.

Resources