How can I use devise to send a reset password link? - ruby-on-rails

I currently have an app that allows users to invite other users to their organization. How can I make it so that devise will send a reset password link to the users that have been invited?

Add recoverable and use the send_reset_password_instructions method.
class User < ActiveRecord::Base
devise :recoverable
...
end
User.find(1).send_reset_password_instructions

Related

Devise Tumblr Omniauth

There is a way for create users without email with Devise?
I'm creating a single sign-on with Tumblr, Omniauth and Devise When accept on Tumblr dialog, it returns:
NoMethodError in Users::OmniauthCallbacksController#tumblr
undefined method `email' for #<User:0x00007f8835e4e110>
Tumblr don't provide email from their users on the whole hash.
Solved!
I used this devise: not requiring email
Then, removing from user model :validatable and this two methods:
def email_required?
false
end
def email_changed?
false
end

devise customization in ruby on rails

I am implementing devise for user registration but some customizations and it is below.
When user registers with username,email and password, an email will go to user
In email user will get a secret code which is required to complete sign up process.
When user clicks "Go to App" button, It will redirect user to App and here user has to use that secret code to complete sign up process.
Once user completes sign up process, there would be one to one relationship between user and that secret code.
I searched a lot on internet but could not find answer.
Problem: I do not know how to create secret code in rails and then send it to user thourgh email and then match it with code that user got.
Note: Secret code and password are two different things in my application.
Devise has an internal module called Confirmable that does exactly this.
In order to use it, all you need to do is:
Make sure your devise model is :confirmable and :registerable
class User
# ...
devise :confirmable, :registerable ...
# ...
end
Run a migration that creates confirmation_token fields
class AddConfirmableToUsers < ActiveRecord::Migration
def self.up
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
def self.down
remove_column :users, :confirmable
end
end
If you already have users, and don't want them to follow this confirmation process, than you need to make them confirmed. You can do that doing
User.update_all ["confirmed_at = ?", Time.now]
on the console.
More references: Confirable Module, Adding confirmable to Users in Devise

send password change confirmation with devise

I want to configure devise to send a confirmation email when the user has changed their password as a security measure. I'd prefer to re-use my devise mailers if possible. Any ideas how to do this?
Untested, but I'd try to do this within your User model with a simple after_update callback:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable # whatever
after_update :send_password_changed_notification
# attr_accessible, validations, ...
private
def send_password_changed_notification
# Send email with the mailer of your choice,
# e. g. your existing custom Devise mailer:
YourDeviseMailer.password_changed_notification(self).deliver if password_changed?
end
end
I would configure the update user action, you can read on how to do that in their documentation. Check how devise handles confirmations of new registered users in registration action and re-use that code in your new reset password action.

Whitelisting with devise

I am using devise to manage user authentication in my rails app. Devise is really great for that.
However I have a special requirement for my application: A user must be whitelisted before he can register as a User.
So there is a admin which creates a list of allowed emails. A user registers with a email and if the email is in the whitelist table he will be registered. If however, the mail is not in the whitelist, the registration should be aborted with a message like "You are not yet invited".
Do you have an idea how that could be solved with devise?
Thanks in advance.
I would just use model validation. I'm assuming your User class has the devise method
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable #etc
before_validation :whitelisted
def whitelisted
unless celebrityemail.include? email
errors.add :email, "#{email} is not on our invitation list"
end
end
end
What you can do is create your own registrations controller and extend the device one like:
class MyRegistrationController < Devise::RegistrationsController
def create
# do your checks
super
end
end
see: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb
And: https://github.com/plataformatec/devise/wiki/How-to:-Customize-routes-to-user-registration-pages
Good luck!
I did create my own controller as suggested:
class Users::RegistrationsController < Devise::RegistrationsController
def create
email = params[:user][:email]
if Admin::Whitelist.find_by_email(email) != nil
super
else
build_resource
set_flash_message :error, "You are not permitted to sign up yet. If you have already payed your registration fee, try again later."
render_with_scope :new
end
end
end
I placed it in app/users/registrations_controller.rb. Then I had to copy the devise registration views into app/views/users/registrations because the default views were not used.
It is working now, thanks for your help

Using Rails and Devise, I want to send a welcome email on sign up.

How can I send a welcoming email to the user when they sign up? I'm using the Devise gem for authentication. SMTP is already set up. I just need to understand how to extend devise to send emails.
NOTE - this is not confirmation email!
UPD Solution:
class User < ActiveRecord::Base
after_create :send_welcome_email
private
def send_welcome_email
UserMailer.deliver_welcome_email(self)
end
end
Add a callback (after_create ) in the model or observer to send the email using normal mailer methods.
FYI, in Rails 3 it's:
class User < ActiveRecord::Base
after_create :send_welcome_email
private
def send_welcome_email
UserMailer.welcome_email(self).deliver
end
end

Resources