I have implemented twitter authentication with devise using something very similar to this: https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
My question is, since twitter doesn't give you the email of the user, how can you direct the user back to the flow of:
User signs in with twitter
User is presented with an email form
User needs to confirm his/her email
clicking confirmation link sends user to site logged in
Devise pretty much takes care with #3 and #4. How should I structure my code to allow #2 to transit into #3 and #4?
Thanks!
Show new user form in twitter callback page. Store twitter token in hidden field. Then you can create new user in your controller and do what you want with the twitter token. User.create also sends confirmation email.
User.create(:email => params[:email], :password => params[:password], :password_confirmation => params[:password_confirmation])
Ryan Bates covers most of this in his screencast OmniAuth Part 2, to get the email confirmation all you need to do is add the confirmable option to devise.
Related
I am using the devise for registration, login and confirm email, and here Devise is taking care of everything. Now if i click on link on email devise authenticating my email and now i am able to login.And i am sending small code to mobiles through sms. And my question is there any possibility to authenticate using that code and give the access to login to that particular user.I want to give both access to the user.like email and phone.
As far as I remember Devise doesn't provide this functionality out of the box. But this is what you can do:
Add SMS confirmation code generator to your
app/controllers/confirmations_controller.rb (class
ConfirmationsController < Devise::ConfirmationsController) create
method;
Modify confirmation email in your
app/views/devise/mailer/confirmation_instructions.html.erb to
include SMS confirmation code and instructions how to use it;
Add SMS confirmation page to your application;
If you want to authenticate using two fields for example say using email and phone_code
ovverride
find_for_database_authentication
method in devise model like this
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["phone_code = :value OR lower(email) = :value", { value: login.strip.downcase }]).first
end
Override the devise views, and replace
<%= f.text_field :email %>
with
<%= f.text_field :login %>
Using this, one can login with either email and phone_no.
But in this case also you must confirm email first.
you can skip email confirmation by removing confirmable module inclusion from devise model which is not recommended.
I am trying to implement Devise and Ominauth for multiple providers. One scenario is: user sign in with Twitter for the first time without creating an account first.
To handle this, I redirect user from callback link (/auth/twitter/callback) to sign up page to fill in email address.
However, I want to bring authentication information from the callback link {:provider => "twitter", :uid =>"123"} to sign up page (new_registration_path). Then when user submit sign up form, the authentication will be captured together with account information.
How should I go about doing this? I've tried
redirect_to(new_user_session_path, {:service => service})
where service = {:provider => "twitter", :uid =>"123"}
but service doesn't get passed through as a params.
What did I do wrong? Do I need to modify source code for Devise?
Thank you.
You're probabbly looking for this:
redirect_to(new_user_session_path(:argument => "value"))
And in controller after redirect (probably UserSessions#new) you can access argument with:
params[:argument]
You need to pass arguments to path helper not to redirect_to method.
In your example it should be:
redirect_to(new_user_session_path(service))
And in controller after redirect (probably UserSessions#new) you can access argument with:
params[:provider]
params[:uid]
or
redirect_to(new_user_session_path(:service => service))
and
params[:service][:provider]
params[:service][:uid]
I am using the Devise gem in my project.
When a user signs up, Devise sends him a mail for account confirmation.
That mail contains an account confirmation link, and when the user clicks it, their account is activated and the user gets redirected to the [ root :to => 'home#welcome' ] page.
How do I change the URL of the above-mentioned account confirmation link?
You can re-define new_user_confirmation_path in config/routes.rb to accomplish this.
I want to give users posibility to add content not being registered but show 'registration form' above submit button on my 'content form'. How to do that using Devise gem? Or maybe I should use some other gems for this functionality?
Thanks.
Generated in standard view user login is default by email. If you wont show registeration form directly in place you want put render file with registeration form:
<%= render :file => 'users/registrations/new' %>
I propose to review the wiki: How To: Add sign_in, sign_out, and sign_up links to your layout template
and RailsCasts: Episode 210
You can register the user in the back-end (for default configuration):
user = User.new(params[:user])
if user.save!
#some logic, where user adds content
else
#some logic
If you want to register only with the email, you will need to generate a password, and for example, send it to the user email. See more there: https://github.com/plataformatec/devise/wiki/How-To:-Automatically-generate-password-for-users-(simpler-registration)/
Out of the box, the devise gem asks for a user's email address and uses that to sign in (authenticate).
This railscast, "Customizing Devise", describes clearly how one can authenticate with a username in lieu of an email. How would one configure devise to try to authenticate against two different columns, either username OR email?
You just need define the method find_for_database_authentication(conditions). By example, with monogid I do that :
def self.find_for_database_authentication(conditions)
self.where({ :login => conditions[:login] }).first || self.where({ :email => conditions[:login] }).first
end
Ah, sorry, I didn't google sufficiently. There's an answer on the devise wiki: "Log in using login or mail"
Also, this question was already asked and answered on stackoverflow here: "Sign in with username OR email"
There's a further blog discussioon that uses the self.find_for_authentication method which is Rails 2.x compatible.