How to save Facebook UID in Devise? - ruby-on-rails

I am using Devise and I have it so a User clicks "log in with facebook". Once they confirm this, it then takes them to the registration url "http://0.0.0.0:3000/users/sign_up".
In here I then populate the name and email fields with the ones I got from Facebook. They now just have to pick a password to sign up. I also want to save the user's Facebook UID and their Facebook url.
How do I pass this into my User model once the user clicks on the sign up button? (I updated my model to include these extra fields).
Edit: I currently also store their Facebook hash data in a session that I got from Ominauth so that I can retrieve it later.

Using the auth hash provided by Omniauth you should be able to retrieve the user information.
For example the hash has a structure that looks like this.
You can pull the user information like this:
facebook_uid = user_auth.uid
facebook_url = user.user_info.urls

Related

devise sign In user automatically based on authentication_token

Is it possible to send an email with a user with a link that automatically sign him in based on authentication_token or something similar? (No login required).
Sure, it is possible. Just send a link to login with a query string containing the username that should be logged in. When your login code gets this, sign the user in.
Just be sure to generate a unique token instead of just using email/username
And also you should tell users that the link can be used to sign in since they might forward it to others.

Email based interaction with rails app

I need some gem that will allow users to interact with rails app through email, without need to register. For example: I publish something for sale, accompanied with email, and all of controls (CRUD, and submitting) I get on my email as links (delete, update, and so on). I'll like to, somehow connect it to devise, with opportunity of further registration using the same email with shopping history.
To publish something(services or products) for sale User has to fill:
name, email (validates unique), phone. That may or may not be used for future registration using devise.
in the same form may be: pictures, description, and other fields of product.........
the idea is to store: id, name, email, phone in user db without password, or be somehow pending for registration
Just create your own CRUD controller with authorization based on some hash that you will add to the URL. Store those hashes in the database and verify if user is legitimate to perform action.
Warning: anyone with the valid URL will be able to perform these actions.
Well, in comment you wrote that you want it to integrate with Devise. Devise supports login tokens but for existing users. You should then somehow virtually register them. Easiest approach would be to:
Include user email in the URL with some tolen
Check if we already have such user - add token verification here
user = User.find_by_email(params[:email])
if user.nil?
user = User.create(field_1: value1, field_2: value2)
end
sign_in(user)
redirect_to after_sign_in_path(user)
Done. User is authenticated based on the email and token included in the URL.

Does devise work with multiple email_id with same account ??

Devise is a fantastic gem available for basic or omniauth authentication sign_up and other things like sessions maintenance, resend confirmation password etc .
But is it possible using devise to map multiple email addresses to same user ?
Like I have 3,4 email ids such as
sahil#abc.com
sahil#xyz.com
sahil#mno.com
Use Case and Example
I have already registered with my first email id i.e. sahil#abc.com using an automated system and account is created. But i always prefer to use my other email_id i.e. sahil#xyz.com. So, i want to build a system where user can login using any one of the above email adresses with the same/different password. But there should be one single account for the user.
I'd say:
you've one email field
you have other emails stored somewhere
You could tell Devise that you allow login based on different fields.
I think the cooler way is to give a try to override the 'authentication_keys' method, as it allows you to define the keys.
But how ever , following link has a working solution :)
HTH
Here is what i exactly needed RoR Devise: Sign in with username OR email
def self.find_for_database_authentication(conditions={})
(self.find_by_email(conditions[:email])) || (AuthorizedEmail.confirmed.find_by_email(conditions[:email]).user if AuthorizedEmail.confirmed.find_by_email(conditions[:email]).present?)
end
What it does is :
Firstly tries to find the user record for authentication by searching with email id.
If it gets the record it returns the record else we go to next part.
It finds in the authorised emails table if there exists any validated and confirmed email in the table. If there is such an entry, it tries to find the user related to that particular authorised email and returns that.

Save Facebook data with custom values

I trying to use login with Facebbok but I have trouble. I don't know how to do this:
User use Facebook Connect and accept website's application.
Next, user is redirected to form where he give username for my website.
Fetched Facebook data and username are saved in database.
Any solution?
I found solution.
User use Facebook Connect.
Fetched Facebook data is saved to session.
User is redirected to form where is name input.
Form is sending request with data from session.

Saving the Comment object after successfull oauth login with Rails' omniauth

I want rails to either update or store the Comment after a user did a successfull loging trough omniauth (Twitter, Facebook or OpenID).
A (not logged in) user fills in a comment form. Posts that.
User gets an omniauth page where she can choose the sign-in-method: Twitter, Facebook or OpenID.
Once the user returns from the oAuth successfully, I want to store (or publish) the Comment.
I have this working, using a session-variable, but that is not very thread-safe: it will break if a user has multiple comment-tabs open with the same session.
Should I save the comment to the database in an unpublished state, and toggle that after returning? And if so, how to know how to toggle te correct comment? Problem is similar to abovementioned session-issue.
Is there some way to simply pass the comment-object along with the omniauth and recieve it back when the user returns from a successfull sign-in?
Or can I pass some hash-string along that I can extract after a successfull return?
Edit: complete rewrite to simplify and clarify the question.
Store post id along with comment text in session. On successful authentication, find the post using stored id and build the comment object for it.
Update - In case authentication fails, delete all such entries.

Resources