Using OmniAuth to Establish A Facebook Authentication - ruby-on-rails

I am trying to provide Sign Up with either an email and password or using Facebook.
I have tried using https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview
I followed each of the steps, but I run into the following error in Terminal when I try to run rails server:
'users/omniauth.callbacks' is not a supported controller name
I thought this was referring to the file I was instructed to make (app/controllers/users/omniauth_callback_controllers.rb) but deleting the file does nothing. I have Devise implemented, and I also installed the Omniauth gem. What am I doing incorrectly?
Steps I took:
Added the gem omniauth-facebook to my Gemfile
Ran bundle install in Terminal
Ran rails g migration AddColumnsToUsers provider uid
Ran rake db:migrate
Added the line config.omniauth :facebook, "APP_ID", "APP_SECRET" to the file config/initializers/devise.rb
Added link to Sign Up on sign up page using the line <%= link_to "Sign In With Facebooks", user_omniauth_authorize_path(:facebook %>
Added the line devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks} to the config/routes.rb file
(The part I was confused about) Added a new folder, users, and a new file, omniauth_callbacks_controller.rb (file path is app/controllers/users/omniauth_callbacks_controller.rb) and included code (see Imgur link)
Added code to the app/models/user.rb file (see Imgur link)
http://imgur.com/a/TyVoK

Related

Rake aborted. Devise.secret_key was not set [duplicate]

I am developing a Rails 4 app using the Active Admin gem for the administration back end. Active Admin in turn uses Devise for user authentication. Now, when I try to deploy the app using capistrano on the VPS server, I get the below error:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
A Google search does not do much for this error. Any suggestions why it is throwing an error? Should I add the secret key to devise initializer, as I cannot find any place to set such config key in initializers/devise.rb?
I ran bundle update this morning and started getting the same error.
I added it as a line in config/initializers/devise.rb and the error was fixed.
This seems to be the commit which introduced it.
What worked for me on Rails 4.1 and Devise 3.2.4 is in config/initializers/devise.rb:
config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production?
As of Devise 3.2.3 for Rails 4+ applications the key setting location defaults to YourAppName::Application.config.secret_key_base found in config/initializers/secret_token.rb
This solved my problem:
Add the code below to your config/initializers/devise.rb file.
config.secret_key = '-- secret key --'
Replace '-- secret key--' with your own key. I recommend storing it in an ENV variable for security purpose.
As per changelog:
Devise will use the secret_key_base on Rails 4+ applications as its secret_key. You can change this and use your own secret by changing the devise.rb initializer.
I went to config/secrets.yml and changed the production value.
Before:
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
After:
production:
secret_key_base: string of charaters
Of course, that should be set to the environment variable, which I will set later, but this at least got it running. I got my string by using bundle exec rake secret.
Could it be, that you did not run rails g devise:install?
Running rails generate devise User without the previous command does cause this problem.
In config/initializers/devise.rb I put:
config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?
Because if you put:
$ heroku config
You'll see a secret_key_base for the mode production.
I solve my initializer problem with this ugly approach:
config.secret_key = 'some1234keyq23' if Rails.env == 'production'
in config/initializers/devise.rb
It now works in production as well as in development !
I cloned my repository onto a new machine from git. The
config/secrets.yml
file was on my .gitignore list, so that file didn't exist, and Devise doesn't create the file.
I added the file, then re-ran
rails generate devise MODEL
and it worked.
Check if your config\initializers\secret_token.rb has:
YourAppName::Application.config.secret_token
It should be:
YourAppName::Application.config.secret_key_base
I has same issue. The problem was caused by these lines in routes.rb:
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
get '/users/sign_out' => 'devise/sessions#destroy'
end
I commented them and after that i run:
$ rails generate devise:install
And it has evaluated perfectly. And after that I uncommented routes.
Well, I have been following this post and tried almost everything here.
I have added the key to devise.rb. But I was still getting the same error.
Maybe a stupid answer, but all I had to do was to push the devise.rb key to the repository.
Fix:
In the production server:
sudo -H nano /etc/environment
Then in the file add:
export SECRET_KEY_BASE="yourkey"
export DEMO03_DATABASE_PASSWORD="yourpass"
to set this permanently, and system wide (all users, all processes) add set variable
In the local project devise.rb file:
config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?
Technical details:
Ubuntu 16.04
Devise (4.2.0)
rails 5.0.1
capistrano (3.7.1)
Ran into the same trouble with Rails 5.2.0 and Devise 4.4.1
Drop the following into /config/initializers/devise.rb
config.secret_key = Rails.application.credentials.secret_key_base
Trying to give a somewhat more complete answer to the ones above:
As mentioned in the devise_auth_token gem's documentation
...Additionally, you can configure other aspects of devise by manually
creating the traditional devise.rb file at
config/initializers/devise.rb. Here are some examples of what you can
do in this file:
Devise.setup do |config|
# The e-mail address that mail will appear to be sent from
# If absent, mail is sent from "please-change-me-at-config-initializers-devise#example.com"
config.mailer_sender = "support#myapp.com"
# If using rails-api, you may want to tell devise to not use ActionDispatch::Flash
# middleware b/c rails-api does not include it.
# See: http://stackoverflow.com/q/19600905/806956
config.navigational_formats = [:json] end
I had the same problem, and like metioned here, I created the devise initializer, and add the config.secret_key = ENV['DEVISE_SECRET_KEY'] line to it.
I do not know right solution but it's working. You can try it. I was cloned my project from my GitLab account and when I run in my local server, I have an error Message:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
Open config/initializers/devise.rb and add this line
config.secret_key = '<%= ENV["SECRET_KEY_BASE"] %>'
This code line is solved my problem.

Devise Secret Key was not set

I am developing a Rails 4 app using the Active Admin gem for the administration back end. Active Admin in turn uses Devise for user authentication. Now, when I try to deploy the app using capistrano on the VPS server, I get the below error:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
A Google search does not do much for this error. Any suggestions why it is throwing an error? Should I add the secret key to devise initializer, as I cannot find any place to set such config key in initializers/devise.rb?
I ran bundle update this morning and started getting the same error.
I added it as a line in config/initializers/devise.rb and the error was fixed.
This seems to be the commit which introduced it.
What worked for me on Rails 4.1 and Devise 3.2.4 is in config/initializers/devise.rb:
config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production?
As of Devise 3.2.3 for Rails 4+ applications the key setting location defaults to YourAppName::Application.config.secret_key_base found in config/initializers/secret_token.rb
This solved my problem:
Add the code below to your config/initializers/devise.rb file.
config.secret_key = '-- secret key --'
Replace '-- secret key--' with your own key. I recommend storing it in an ENV variable for security purpose.
As per changelog:
Devise will use the secret_key_base on Rails 4+ applications as its secret_key. You can change this and use your own secret by changing the devise.rb initializer.
I went to config/secrets.yml and changed the production value.
Before:
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
After:
production:
secret_key_base: string of charaters
Of course, that should be set to the environment variable, which I will set later, but this at least got it running. I got my string by using bundle exec rake secret.
Could it be, that you did not run rails g devise:install?
Running rails generate devise User without the previous command does cause this problem.
In config/initializers/devise.rb I put:
config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?
Because if you put:
$ heroku config
You'll see a secret_key_base for the mode production.
I solve my initializer problem with this ugly approach:
config.secret_key = 'some1234keyq23' if Rails.env == 'production'
in config/initializers/devise.rb
It now works in production as well as in development !
I cloned my repository onto a new machine from git. The
config/secrets.yml
file was on my .gitignore list, so that file didn't exist, and Devise doesn't create the file.
I added the file, then re-ran
rails generate devise MODEL
and it worked.
Check if your config\initializers\secret_token.rb has:
YourAppName::Application.config.secret_token
It should be:
YourAppName::Application.config.secret_key_base
I has same issue. The problem was caused by these lines in routes.rb:
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
get '/users/sign_out' => 'devise/sessions#destroy'
end
I commented them and after that i run:
$ rails generate devise:install
And it has evaluated perfectly. And after that I uncommented routes.
Well, I have been following this post and tried almost everything here.
I have added the key to devise.rb. But I was still getting the same error.
Maybe a stupid answer, but all I had to do was to push the devise.rb key to the repository.
Fix:
In the production server:
sudo -H nano /etc/environment
Then in the file add:
export SECRET_KEY_BASE="yourkey"
export DEMO03_DATABASE_PASSWORD="yourpass"
to set this permanently, and system wide (all users, all processes) add set variable
In the local project devise.rb file:
config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?
Technical details:
Ubuntu 16.04
Devise (4.2.0)
rails 5.0.1
capistrano (3.7.1)
Ran into the same trouble with Rails 5.2.0 and Devise 4.4.1
Drop the following into /config/initializers/devise.rb
config.secret_key = Rails.application.credentials.secret_key_base
Trying to give a somewhat more complete answer to the ones above:
As mentioned in the devise_auth_token gem's documentation
...Additionally, you can configure other aspects of devise by manually
creating the traditional devise.rb file at
config/initializers/devise.rb. Here are some examples of what you can
do in this file:
Devise.setup do |config|
# The e-mail address that mail will appear to be sent from
# If absent, mail is sent from "please-change-me-at-config-initializers-devise#example.com"
config.mailer_sender = "support#myapp.com"
# If using rails-api, you may want to tell devise to not use ActionDispatch::Flash
# middleware b/c rails-api does not include it.
# See: http://stackoverflow.com/q/19600905/806956
config.navigational_formats = [:json] end
I had the same problem, and like metioned here, I created the devise initializer, and add the config.secret_key = ENV['DEVISE_SECRET_KEY'] line to it.
I do not know right solution but it's working. You can try it. I was cloned my project from my GitLab account and when I run in my local server, I have an error Message:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
Open config/initializers/devise.rb and add this line
config.secret_key = '<%= ENV["SECRET_KEY_BASE"] %>'
This code line is solved my problem.

How do I setup omniauth-facebook on an already existing omniauth and devise rails app?

So, I followed the instructions as mentioned here http://railscasts.com/episodes/235-devise-and-omniauth-revised and successfully setup the the sign in and sign up with twitter option. Now I am trying to setup facebook. Using the gem 'omniauth-facebook'. After installing it. I then added to /config/initializers/devise.rb file
config.omniauth :facebook, ENV["FB_APP_ID"], ENV["FB_SECRET_ID"]
Now, when I try to sign in with facebook. I get the error -
ArgumentError in OmniauthCallbacksController#facebook
wrong number of arguments(3 for 2)
I was doubting an error might show up. But not sure how to fix it.
My controller and user model are same as mentioned here - http://railscasts.com/episodes/235-devise-and-omniauth-revised
What am i missing?
UPDATE: I did change this in the user.rb model file
instead of
alias_method :twitter, :all
I made it
alias_method :twitter, :facebook :all
Thanks
This worked for me:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, 'FB_APP_ID', 'FB_SECRET_ID'
but i didn't use devise, so just try to remove the ENV .
Also read here about all the options and some fails you may encounter:
https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview

Login with gmail and OmniAuth in rails 3

I am trying login with gmail and OmniAuth in rails 3. I am following http://hoisie.com/2011/09/12/using-google-oauth-with-omniauth/ this post. I added gem 'omniauth'
in my gem file. Create omniauth.rb inside initializers. Replace the oauth_secret by API key generated in google api site.
Create sessions controller and adding
match "/auth/:provider/callback" => "sessions#create"
match "/signout" => "sessions#destroy", :as => :signout
this in my route.rb
When i am running the app getting the following error.
/usr/lib/ruby/gems/1.8/gems/omniauth-1.0.1/lib/omniauth/builder.rb:25:in `provider': Could not find matching strategy for :google. You may need to install an additional gem (such as omniauth-google). (LoadError)
There is an omniauth strategy for Google, it is omniauth-google-oauth2. Just just add it to your Gemfile as you are adviced in the error message.
You may also want to look at my example app that includes authenticating via omniauth.

Rails3 - oauth-plugin problem

I'm trying to use oauth-plugin on a Rails application I'm developing, but I keep running into problems.
To make sure I'm not making any mistake, I started an application from scratch (using Rails 3.0.3). Here are the steps I followed:
Create da new rails application (rails.test)
Edited its Gemfile to include:
gem "oauth-plugin", ">=0.4.0.pre1"
gem "oauth", "0.4.4"
Generated oauth-consumer, by running script/rails g oauth_consumer
Edited oauth_consumers.rb to include my keys for Google integration:
:google=>{
:key=>"anonymous",
:secret=>"anonymous",
:scope=>"https://www.google.com/calendar/feeds/",
:options => {
:site => "http://www.google.com",
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path=> "/accounts/OAuthAuthorizeToken"
},
}
Edited routes.rb to add the route for oauth_consumer:
resources :oauth_consumers
Edited application_controller.rb to implement the logged_in? method as follows:
def logged_in?
true
end
Now when I access http://localhost:3000/oauth_consumers/google I get the following error:
uninitialized constant GoogleToken
Does anyone know what causes this error and how can I fix it? GoogleToken is a class that should have been auto generated by oauth-plugin, so I can't tell why I'm getting this uninitialized constant error.
The GoogleToken class doesn't get auto-generated unless you pass "google" to the generator like so:
script/rails g oauth_consumer google
or for rails 3:
rails g oauth_consumer google
Also check to ensure the relationship is set up in the user model like so:
has_one :google, :class_name => "GoogleToken", :dependent => :destroy
Did you remember to run bundle install from terminal after editing your Gemfile? Sounds like your Rails app doesn't know about these gems yet.
I have the same problem, i think a solution could be in this:
https://github.com/pelle/oauth-plugin/blob/master/lib/generators/oauth_consumer/USAGE
You need some sort of authentication like restful-authentication plugin, if you uncomment line 27..29 in your oauth_consumers_controller.rb file, you'll jump to next step!

Resources