I'd like to have an "edit profile" page, in which the user can change the email address registered when signing up.
I'd like to have the following process:
the user has to input his password to confirm before he makes changes in the email field.
after submitting that page, the user should receive a verification mail just like Devise's default sign up.
the email change is completed as soon as the user clicks the verification token URL on the mail.
How would I do this?
I created this same flow for a site of mine. Here's an example of what you can do:
add to config/routes.rb
(note that the routing could be better, but I did this a while ago)
scope :path => '/users', :controller => 'users' do
match 'verify_email' => :verify_email, :as => 'verify_email'
match 'edit_account_email' => :edit_account_email, :as => 'edit_account_email'
match 'update_account_email' => :update_account_email, :as => 'update_account_email'
end
add to app/controllers/users_controller.rb
def edit_account_email
#user=current_user
end
def update_account_email
#user=current_user
#user.password_not_needed=true
#user.email=params[:address]
if #user.save
flash[:notice]="your login email has been successfully updated."
else
flash[:alert]="oops! we were unable to activate your new login email. #{#user.errors}"
end
redirect_to edit_user_path
end
def verify_email
#user=current_user
#address=params[:address]
UserMailer.confirm_account_email(#user, #address).deliver
end
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
def confirm_account_email(user, address)
#user = user
#address = address
mail(
:to=>"#{user.name} <#{#address}>",
:from=>"your name <'your_email#domain.com'>",
:subject=>"account email confirmation for #{user.name}"
)
end
end
app/views/user_mailer/confirm_account_email.html.erb
<p>you can confirm that you'd like to use this email address to log in to your account by clicking the link below:</p>
<p><%= link_to('update your email', update_account_email_url(#user, :address=>#address)) %></p>
<p>if you choose not to confirm the new address, your current login email will remain active.
Related
I am using devise for authentication. What I am trying to do is send a reset_password_token in the link which is sent to the user. I am creating an agent from ActiveAdmin portal once it is created I am sending a link to the agent's email address so they can reset their password but I am not able to figure out how to send the reset_password_token with the link which is sent to the agent. Please have a look at the code below and help me find the solution
organisation_mailer.rb
class OrganisationMailer < ApplicationMailer
default from: 'abc#yahoo.com'
def send_link(agent_id)
#agent = Agent.find(agent_id)
token = SecureRandom.urlsafe_base64
#agent.update(organisation_token: token)
#link = "#{ActionMailer::Base.default_url_options[:host]}/organisation?token=#{token}"
mail to: #agent.email, subject: "On boarding link for #{#agent.full_name}"
end
end
routes.rb
get 'organisation', to: 'agents#organisation', constraints: ->(req) { req.format == :json }
agents_controller.rb
def organisation
return if params[:token].blank?
#agent = Agent.find_by(organisation: params[:token])
sign_in(#agent, scope: :agent)
end
agents.rb
ActiveAdmin.register Agent do
action_item :send_email, only: :show do
link_to 'Send Email', send_email_admin_agent_path, method: :post
end
member_action :send_email, method: :post do
OrganisationMailer.send_link(resource.id).deliver_now
redirect_to admin_agent_path(resource.id), notice: 'Organisation link has been sent'
end
I have a website that connect to an API which requires that I generate a reference code along with other information i will need to complete my registration. After the registration has been confirmed the server will respond with a redirect to my controller action to complete the whole process where I have to verify same with another controller action. However, I have tried the following but still have some difficulty making my website respond to the redirect ie: showing a message 'book confirmed!'
Here is the url i get:
http/example.com/?ref=2755558333388=referencecode=2755558333388
instead of
http/example.com/book?ref=2755558333388=referencecode=2755558333388
# route
Rails.application.routes.draw do
get 'confirmations/register'
post 'confirmations/verified'
end
# controller:
class ConfirmationsController < ApplicationController
def register
registerationObj = Registration.new(ENV["BOOKREG_PUBLIC_KEY"], ENV["BOOKREG_PRIVATE_KEY"])
book = RegistrationBooks.new(registrationObj)
:action = books.create(
:book_title => #book.title,
:phone => current_user.phone,
:email => current_user.email,
:referencecode => #book.referencecode
end
def verified
if params[:referencecode == #book.referencecode
flash[:alert] = "Your book has been verified"
redirect_to books_path
else
redirect_to current_user_edit_path, notice: "Sorry your book was not verified"
end
end
Currently I have ActionMailer send an email when a user registers, and I generate a random :sign_in_token with the user.
How can a user then click on the link sent to his email and update the users :registration_complete boolean value to TRUE?
Currently, I am able to send the link and generates a random token, but I don't know how to update the boolean value through the email.
MODELS
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :sign_in_token,
:registration_complete
###This generates my sign_in_token
def generate_sign_in_token
self.sign_in_token = Digest::SHA1.hexdigest([Time.now, rand].join)
end
end
CONTROLLER
def create
#user = RegularUser.new(params[:regular_user])
if #user.save
###Sends the User an email with sign_in_token
UserMailer.registration_confirmation(#user, login_url+"/#{#user.sign_in_token}").deliver
flash[:success] = "Please Check Your Email to Verify your Registration!"
redirect_to (verifyemail_path)
else
render 'new'
end
end
USER_MAILER
def registration_confirmation(user, login_url)
#login_url = login_url
#user = user
mail(:to => "#{user.name} <#{user.email}>", :subject => "Welcome to APP")
end
VIEWS
###Redirects User to Login Page, But how do i connect it to my activate method?
<%= link_to "Complete Registration", #login_url %>
ROUTES
match '/login/:sign_in_token', :to => 'sessions#new'
When they click a link, it takes them to a controller with an action of set_complete using a GET request, which sets the boolean value.
Something like:
def set_complete
user = User.find(params[:user])
user.update_attribute(registration_complete => true)
redirect_to login_path # or whatever your login url is...
end
For the controller action and something like this for the link:
<a href="example.com/registrations/set_complete?user=1" />
Here is a sample of what might go in the routes file:
get "/users/set_complete", :to => "users#set_complete"
You'd probably need to set the user id to whatever you want using erb, andmake a few other app-specific customizations, but this is the general idea.
Hope this helps!
class JobMailer < ActionMailer::Base
default :from => "emailer#email.com"
def new_job_email_for_client
#
#
#url = "http://simplesite.com/users/login"
mail(:to => #???,
:subject => "You have created a new case on simplesite.")
end
end
I would like each user to receive an email each and every time he/she creates a "job." In other parts of the application, I can access #user and user.email and such, but in the mailer I'm getting "undefined errors."
How can I access the current users email address in the mailer (taking into consideration that Devise is in control of Users)?
I'm not sure if this is a great way of doing it, but this is how I got it working:
def new_job_email_for_client(user_email)
#url = "http://simplesite.com/users/login"
mail(:to => user_email,
:subject => "You have created a new case.")
end
Update: this question has been answered (see below). I'll leave it up in case anyone can benefit in the future.
I am trying to get e-mail confirmations working on Authlogic using Rails 3. http://github.com/matthooks/authlogic-activation-tutorial
Authentication is working and the activation e-mails are being generated and sent, each containing a perishable token, but the perishable tokens are incorrect, in that they do not match the one saved in the user's record.
Upon following the token in the e-mail, I get: Exception in ActivationsController#create
Note: When I manually enter the correct token from the table into the URL, it validates and redirects as it is supposed to. Therefore, the only issue is that the perishable token being generated is not the same as the one being saved.
# UserMailer
class UserMailer < ActionMailer::Base
default :from => "notifications#myapp.com"
def registration_confirmation(user)
#user = user
mail(:to => "#{user.login} <#{user.email}>", :subject => "Registered")
end
def activation_instructions(user)
subject "Activate Your Account"
from "noreply#myapp.com"
recipients user.email
sent_on Time.now
body :account_activation_url => activate_url(user.perishable_token)
end
def welcome(user)
subject "Welcome to the site!"
from "noreply#myapp.com"
recipients user.email
sent_on Time.now
body :root_url => root_url
end
end
# E-mail itself:
To activate, click here: <%= #account_activation_url %>
The error is occurring on line 5 where the system tries and fails to find User by token:
class ActivationsController < ApplicationController
before_filter :require_no_user
def create
#user = User.find_by_perishable_token(params[:activation_code], 1.week) || (raise Exception)
raise Exception if #user.active?
if #user.activate!
flash[:notice] = "Your account has been activated!"
UserSession.create(#user, false) # Log user in manually
#user.deliver_welcome!
redirect_to home_url
else
render :controller => "welcome", :action => "linklogin"
end
end
end
It's funny - sometimes the process of asking the question itself reveals the answer.
In my users#create, there are different user types, and the action sets a couple of values after the initial validated save and saves the simple changes again without validation.
My e-mail was being sent in between the first and second saves, so of course by the time the user clicks on the activation e-mail, the perishable_token has already been reset.
I moved the mailing down to after the second save, and now the activation e-mail works perfectly.
Thank you very much for any time you've spent considering this problem. :)
Cirrus