Email is already taken only if account is validated - ruby-on-rails

I would like to overide the action thats says: 'Email is already taken' in Devise
I'm building an app where the user can invite someone to multiple items (todo, events, folder, etc ...) through his email. To avoid a multiplications of invitations tables and improve scalability. My idea was to create a user with only an email. This allows all users of the application to refer to a unique user (through his email) even if he is not registered. Instead of multiplying the invitations elements.
However, the basic 'Devise' configuration does not allow this organization.
I would like to redo this step of registration.
This is what I want to do :
if this mail was already been taken
if the account related has already confirmed his email
notice = 'Email is already taken'
else
send a confirmation on the mail
end
else
Create User
end
I've already consulted the gem Devise_invitable, but it's not exactly what I want to do.
Has anyone ever tried to do this? If yes, how ?

Related

How do I resend invitation to a user using devise invitable?

I have devise invitable gem
Hooked up in my app as well as devise.
Everything working fine. When I create new user, they are saved in database and mailer sends email an email address. User clicks on email, sets password and then is logged in.
But sometimes these users lose their email or rather can’t find it in their email inbox. I’d like to have a resend invite button on the users index page.
When user clicks resend, I want the invites user to get another email sent to them.
Had anyone successfully accomplished this with devise invitable? If so, can you share how?
The gem adds a resend_invitation configuration parameter on your invitable-enabled model. From the docs:
resend_invitation: resend invitation if user with invited status is invited again. Enabled by default.
So, calling .invite! again on a record that's marked as invited will do the trick for you.

User records created by devise invite gets deleted

I am using devise invitable module in one of my application. I have a report feature in which a user(i.e sender) can share his own details in a form of a report with other users(i.e. receiver). The sender needs to input the email address of the receiver in an form. We search for the receiver's record in our users table and if there is no entry then we use devise invite feature to create a record for him/her and update the association for share details in share_information table. The receiver gets an email with a link to set his/her password. Everything works well if the receiver uses that link to set his/her password and claim his/her account. But if the user does not use the link to set password and tries to sign up instead then problem arises. In such scenario the earlier record for that user gets deleted and a new record is generated with a new user id. The association breaks as the share_information has association with the earlier user_id. How do I solve the problem and why does devise deletes the user? I did not find information about such event in devise gem documents. I did not add code as I think it is not a code problem. Though I could add it if somebody needs to refer. Any help or suggestion is appreciated :)
I think, devise user works in a certain way. If a user is invited and he does not claim the account but tries to sign up then devise just deletes the previous user and creates a new record. It makes sense too in a certain perspective. Anyways I had to solve the issue so the invited user was generated with a black password. I inserted a devise friendly token password in the record and then the devise would not delete the user. It would give a notice of user already exist.
user = User.invite!({:email => user[:email], :first_name => user[:first_name], :last_name => user[:last_name], :phone_number => user[:phone]}, current_user)
user.password = Devise.friendly_token[0,20]
user.save!

How to associate multiple emails for a single user in rails Devise gem

I am developing an application which uses Devise for user authentication. It performs all standard task that Devise handles (e.g.: Email verification during user sign up). But a user may have multiple email addresses to access his account and I want to verify all those addresses too.
My design is: user will get a email field in his profile page to add another email address to access his account along with his existing email address. After clicking submit, an email verification will occur like first time sign up process and user will be able to use both of this email address after successful verification.
Is there any gem available for this? If I need to implement it by myself, how can I do this without breaking the existing system?
It's very late to reply but recently I faced similar issue and found one gem which lets user have many emails, user can login with any email, set one email as primary, and provides support for confirmable, authenticable and validatable for each email.
Here is the link to gem:
https://github.com/allenwq/devise-multi_email
Hope it helps someone facing same situation :)

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.

Send custom confirmation email in Devise depending on role defined in the database

I am using Devise for registration of a site with confirmable. However, I have two different roles for this site. The first role is the "main" role that uses the regular Devise signup procedure. Accounts in a second role are supposed to be created after the original user confirms their account, logs in for the first time and saves a certain model. For example, if a user signs up for the site (as role type 1) the get a confirmation email from Devise as normal. Next, they visit the confirmation link, verify their account and then fill out a form where they specify some friends that should also get accounts. The friends are role type 2 and they should get a different confirmation email than the original person who signed up their friends for the account. The accounts for the friends are created when the form filled out by the original user is saved. In addition, a person can edit and add more friends later so accounts might also need to be created on the update method of the relevant form/object and those new users will need to be sent the correct email. To be clear, I do not want to skip confirmation - I just want to send different confirmation emails to the user depending on their roles. I cannot figure out how to handle this properly. If I try to create the friends accounts in code when the form is saved with User.new, calling user.skip_confirmation! will automatically confirm them. However, I do not want anyone automatically confirmed - I just want to select a different customizable confirmation email to send depending on various conditions. Can someone point me in the right direction?
Check out send_on_create_confirmation_instructions method and comments for it in your /gems/devise-x.x.x/lib/devise/models/confirmable.rb

Resources