I'm following along with a Railscast http://railscasts.com/episodes/235-devise-and-omniauth-revised?view=comments about implementing omniauth into devise, specifically adding Twitter signin. When I add the link to sign into Twitter to my app, I'm getting this error visiting the homepage
<li><%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:provider) %></li>
No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:provider}
If I do rake routes (see below), it shows that I have 'user_omniauth_callback' route, but it doesn't specify a request type (GET, POST) etc, which I'm not sure is significant or not.
I searched this error on SO, and one person had a similar problem that resulted from not including the following code in routes.rb, but I have it included.
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks"}
The error arose before getting to the part of the episode where he adds the omniauth controller; I added it just to be sure but I'm still getting the same error:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
request.env["omniauth.auth"]
end
alias_method :twitter, :all
end
I'm not really sure what else I can look at. In the devise initializer, I set up config for Twitter
config.omniauth :twitter, ENV["TWITTER_KEY"], ENV["TWITTER_SECRET"]
This is my user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :image, :provider, :uid
validates_uniqueness_of :name, :email, :case_sensitive => false
end
Can you make any suggestions about what I might do to fix this problem?
Rake Routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_omniauth_authorize /users/auth/:provider(.:format) devise/omniauth_callbacks#passthru {:provider=>/twitter/}
user_omniauth_callback /users/auth/:action/callback(.:format) devise/omniauth_callbacks#(?-mix:twitter)
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
The mistake was that I was passing :provider rather than :twitter as an argument in the link.
Incorrect
<%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:provider) %>
Correct:
<%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:twitter) %>
Related
I had implemented devise gem and omniauth-facebook before few months into my app and everything was worked as usual, until now when I deleted my fb user from db.
Now I am trying to sign in again as nonexisting user and all the time I am redirected to Devise Sign Up form.
So I cant log in/ register my FB user into my app.
What can cause this type of problem?
routes.rb
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
omniouth_callback_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end
user.rb model
class User < ApplicationRecord
# 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
rails routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) users/omniauth_callbacks#passthru
user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) users/omniauth_callbacks#facebook
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
I found the reason in user.rb model I had one association belongs_to:somemodel and that I was put there for testing some model relations.
So commenting this line I am logged in by facebook!
I'm now using Devise gem in a Ruby application.
It seems a password method doesn't work because in somehow, devise controller is a bit modified. When I typed /users/password/new, it goes back to a root.
I followed a tutorial but now I'm stuck at this really important method that I have to provide to the people.
The passwords_controller.rb file is located in controllers/users/password_controller.rb.
And the code is
class Users::PasswordsController < Devise::PasswordsController
prepend_before_filter :require_no_authentication
# Render the #edit only if coming from a reset password email link
append_before_filter :assert_reset_token_passed, only: :edit
# GET /resource/password/new
def new
self.resource = resource_class.new
end
# POST /resource/password
def create
self.resource = resource_class.send_reset_password_instructions(resource_params)
yield resource if block_given?
if successfully_sent?(resource)
respond_with({}, location: after_sending_reset_password_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
( which is a typical devise controller content )
My user.rb model goes as follow.
...
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:google_oauth2] #, :confirmable
...
I have view files in this directory.
views/users/passwords/create.html.erb, edit.html.erb, new.html.erb
views/users/sessions/new.html.erb
views/users/registrations/edit.html.erb, create.html.erb
Routes file goes like this
devise_for :users, controllers: { passwords: "users/passwords", sessions: "users/sessions", registrations: "users/registrations", omniauth_callbacks: "users/omniauth_callbacks" }
Whenever I type "http://localhost:3000/users/password/new", it goes back to root.
This is my route file.
new_user_session GET /users/sign_in(.:format) users/sessions#new
user_session POST /users/sign_in(.:format) users/sessions#create
destroy_user_session GET /users/sign_out(.:format) users/sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/google_oauth2/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) users/omniauth_callbacks#:action
user_password POST /users/password(.:format) users/passwords#create
new_user_password GET /users/password/new(.:format) users/passwords#new
edit_user_password GET /users/password/edit(.:format) users/passwords#edit
PATCH /users/password(.:format) users/passwords#update
PUT /users/password(.:format) users/passwords#update
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST|GET /:controller(/:action(/:id))(.:format) :controller#:action
root GET / landings#index
The log I got when I go to localhost:3000/users/password/new goes like this.
Started GET "/users/password/new" for ::1 at 2015-10-06 04:11:58 +0900 Processing by Users::PasswordsController#new as HTML Redirected to localhost:3000 Filter chain halted as :is_login rendered or redirected Completed 302 Found in 2ms (ActiveRecord: 0.0ms) This is a log that I got right after I posted the address
If you guys have any hint, please let me know!!
Best
I fix it by append /users/password/edit?reset_password_token=aaaaaa
maybe it is the devise bug .
I keep getting this error when I try to sign up on my application, I am using devise for authentication;
NoMethodError in Devise::Registrations#new
undefined method `registration_path' for #<#<Class:0x007fe45c6c35e8>:0x007fe45d9ffd78>
Extracted source (around line #3):
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
When I run rake routes I get;
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
Sign up link code;
<%= link_to "Sign up", new_user_registration_path%>
UPDATE
Thanks to ParaPenguin the sign up field works now, but when I click submit I keep getting this problem
No route matches [POST] "/users/sign_up"
new_user_session_path GET /users/sign_in(.:format) devise/sessions#new
user_session_path POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session_path DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password_path POST /users/password(.:format) devise/passwords#create
new_user_password_path GET /users/password/new(.:format) devise/passwords#new
edit_user_password_path GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration_path GET /users/cancel(.:format) devise/registrations#cancel
user_registration_path POST /users(.:format) devise/registrations#create
new_user_registration_path GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration_path GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format)
Update 2-Gjaldon asked me to include my user model and routes
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
end
Routes.rb
Document::Application.routes.draw do
devise_for :users
root "pages#home"
get "about" => "pages#about"
get "prices" => "pages#prices"
get "faq" => "pages#faq", :as => :faq
get "terms" => "pages#terms"
get "view" => "pages#view"
get "policy" => "pages#policy"
get "contact" => "pages#contact"
get "works" => "pages#works"
Could you provide your routes for Devise in your routes.rb and the devise code for your User model?
It's either one of the following that might fix your problem:
Restart your Rails server. This way, Rails will see the new files and changes Devise has made to Rails.
Did you override the Devise::Registrations#new action? If so, you will need to replace your view code below:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
with:
<%= form_for(resource, :as => resource_name, :url => user_registration_path) do |f| %>
In your devise_registrations/edit.html.erb view, your form_for code would look like:
<%= form_for(resource, :as => resource_name, :url => edit_user_registration_path(resource_name, :method => :put) do |f| %>
Your rake routes actually provides you with the routes you need to submit to. Just keep in mind that the corresponding controller and action is displayed under the Controller#action column in the output of rake routes. The form in edit action should always submit to the update action and the form in new action should always submit to the create action if you stick to the Rails conventions. If you decide to have a form in another action for creating a record, then make sure to have the form submit to the create action.
Try changing registration_path(resource_name) from
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
to new_registration_path(resource_name). This could fix the error but I am not certain, it's possible. It seems that it is only for you as that file setup runs just fine for my devise.
I get the following error using device and carrierwave gem:
undefined method `user_media_index_path'
.Showing .../user_medias/new.html.erb where line #3 raised:
I have added index on user_id in user_media model
I have successfully implemented file upload for for single model but I don't know how to do it with a seperate module.
new.html
form_for #media, :html =>{:multipart =>true} do |f|
Upload an Image f.file_field :image
f.submit
end
This is the user model generated using the device gem:
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :user_media, dependent: :destroy
end
its the model to store the user medias like images,etc i;m using this now only for images
but for future to add more types of media i have created this user_media model
user_media.rb
class UserMedia < ActiveRecord::Base
attr_accessible :anudio, :image, :video
belongs_to :user
mount_uploader :image, MediaUploader
end
This is where its get redirected to when asked for create action for uploading the image
user_medias_controller
class UserMediasController < ApplicationController
def new
#media = UserMedia.new
end
def create
#media=current_user.user_media.build(params[:media])
if #media.save
render'index'
else
render'new'
end
end
end
The routing details are:
routes.rb
Projectx::Application.routes.draw do
get "dashboard/index"
resources :dashboard, :UserMedias
get "home/index"
devise_for :users
root :to => 'home#index'
match 'uploder' =>'UserMedias#new'
rake routes output this all after adding resources suggested by #peter
dashboard_index GET /dashboard/index(.:format) dashboard#index
GET /dashboard(.:format) dashboard#index
POST /dashboard(.:format) dashboard#create
new_dashboard GET /dashboard/new(.:format) dashboard#new
edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit
dashboard GET /dashboard/:id(.:format) dashboard#show
PUT /dashboard/:id(.:format) dashboard#update
DELETE /dashboard/:id(.:format) dashboard#destroy
user_medias GET /user_medias(.:format) user_medias#index
POST /user_medias(.:format) user_medias#create
new_user_media GET /user_medias/new(.:format) user_medias#new
edit_user_media GET /user_medias/:id/edit(.:format) user_medias#edit
user_media GET /user_medias/:id(.:format) user_medias#show
PUT /user_medias/:id(.:format) user_medias#update
DELETE /user_medias/:id(.:format) user_medias#destroy
home_index GET /home/index(.:format) home#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / home#index
uploder /uploder(.:format) user_medias#new
the error points to a missing route in your routes file. add this to your routes
resources :user_medias
I'm coming from Sinatra to Rails and still quite new to Rails. My problem is that after I create a User account I am just directed to the index.html page in the /public folder and can't seem to access any other routes, I can't sign_out the user and I can't add another user.
I am using the devise gem to manage my user model and authentication. After installing the gem I followed the instructions on the devise github page.
ie:
rails generate devise:install
I also added to the 'config/environments/development.rb' file
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
and I added to the 'config/routes.rb' file
root :to => "home#index"
and I added to the 'app/views/layouts/application.html.erb' file
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
Then I ran
rails generate devise User
And finally
rake db:migrate
Here is my user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
Then I navigated to "/users/sign_up" and I entered an email and password after which I was redirected to the index.html page in the public folder.
The problem is I just seem to be stuck there. '/users/sign_out' yields
Routing Error
No route matches [GET] "/users/sign_out"
Try running rake routes for more information on available routes.
And running 'rake routes' yields
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / home#index
Seems like '/users/sign_up' should be a working route.
I am wondering if the problem is that there is no email service setup and this account is trying to be validated trough email? If so, how do I disable that?
Thanks! And let me know if you need more information or for me to clarify something.
===============UPDATE===================
The 'users/edit' route does work and I think possibly the problem lies in the fact that the route that is setup for 'users/sign_out' is a DELETE route. I forget the terminology about this, but I know that there is some sort of trickery in making a DELETE route out of a GET route. So is this where my problem lies?
You can do one of two things here.
Create a link to do the logout, something like this:
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
Or add an additional route like this to your routes.rb file:
devise_for :users, :skip => [:sessions]
as :user do
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
The above example is from https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
To sign out, you should be calling:
destroy_user_session_path
Devise stores sessions, and DELETE just removes your session.
For the routing error, can you post up your HomeController?
And for the edit, are you calling:
edit_user_registration_path
A few moments ago I was having this problem and instead of looking for sites and more sites, it was when i stop here and one of the codes shown above is also shown below worked fine for me thanks
devise_for :users, :skip => [:sessions]
as :user do
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end