How to give option for resend verification email to user? - ruby-on-rails-3

i am working on rails3 application. In my application when a user registered first time, an email has been sent to user with a verification link, after clicking that link i update the status_id and redirect user to login page. Here is my code :
code for token generation:
require 'digest/sha2'
class Subscription < ActiveRecord::Base
validate :ids_must_be_present, :on => :create
def ids_must_be_present
if status_id==0
generate_token
else
errors.add('Something gone wrong')
end
end
def generate_token
self.token = encrypt_string(id, generate_salt)
end
def encrypt_string(id, salt)
Digest::SHA2.hexdigest(id.to_s + "prftnxt" + salt)
end
private
def generate_salt
self.object_id.to_s + rand.to_s + company_id.to_s + Time.now.to_i.to_s
end
end
code to send email with link:
def email_verify
if subscription = Subscription.find_by_id_and_token(params[:id], params[:token])
subscription.update_attribute(:status_id, 1)
redirect_to("/login/index", :notice => "Thanks, email successfully verified")
else
flash.now[:notice] = "Your email has not verified yet. Please verify your email by clicking the link we have sent you."
end
end
Email template with verification link:
Hello <b><%= #user.username %></b>,
<br/>
<br/>
Thank you for signing up .
<b> Please Verify your email</b>
<%= link_to "verify", "http://localhost:3000/login/email_verify?token=#{#subscription.token}&id=#{#subscription.id}"%>
</br></br>
</br>
Now everything is fine, now my client want if user did not get verification email, then we some where give the option or link to request to resend verification mail.
i am thinking on to display flash msg on login attempt with a link to request for email.
but i am confused how do i do this any example or help would be helpful thanks.

Hi friends i got a solution, i have used a method in login controller that check the email is verified or not and if not verified a flash message displayed. The message contains the link .
When a user click on that link i resend the verification mail.
Here is my code:
subscription = Subscription.find_by_company_id_and_plan_id(current_company.id, current_company.plan.id)
link = "<a href= '/login/resend_verification_email'>Click</a>"
if subscription.status_id == 0
flash[:error] = "Your email is not verified. Please verify before login. <br/> #{link} here to resend verification email.".html_safe
redirect_to :back
end
and in login controller:
def resend_verification_email
subscription = Subscription.find_by_company_id_and_plan_id(current_company.id, current_company.plan.id)
Email.verify_email(current_user, subscription).deliver
redirect_to :back
flash[:success] = 'Verification email has been resend successfully, please check your inbox.'
end

Related

How to send SMS on osclass user registration

I am a beginner.
I want to send a welcome SMS to a new user in osclass, adding the SMS API on each page.
If anybody knows where is the "Registration email send" I can integrate it there.
I have tried in utils.php but it's not working.
My Script is:
////SMS Start
$name = osc_user_name();
$mobile = osc_user_phone_mobile();
$msgs = "Thank You $name, You have Successfully Registered with Classified123.";
$msgb = urlencode($msgs);
$stra ="http://sms.website.in/api/mt/SendSMS?user=user&password=pw&senderid=ABCDEF&channel=Trans&DCS=0&flashsms=0&number=$mobile&text=$msgb&route=15";
file_get_contents($stra);
/////SMS End

Rails OAuthException :: An active access token must be used to query information about the current user

Iam using fb_graph in my rails application to get and set the access token if it authenticates with facebook. I have an Invites controller where a user will have to authenticate to invite friends. My code looks like this:
def facebook
if params[:code]
set_oauth_token(params[:code])
elsif params[:error]
redirect_to landing_path, :alert => "Access Denied"
return
end
if current_user.oauth_token.nil?
redirect_to client.authorization_uri(
:scope => "user_about_me, email, publish_stream, user_location, user_interests, user_birthday, user_likes, user_hometown, offline_access"
)
end
private
def set_oauth_token(token)
client.authorization_code = params[:code]
access_token = client.access_token! :client_auth_body
user = FbGraph::User.me(access_token).fetch
current_user.oauth_token = access_token
current_user.save(:validate => false)
end
def client
FbGraph::Auth.new(ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], :redirect_uri => invites_facebook_url).client
end
But Iam getting the error:
FbGraph::InvalidRequest at /invites/facebook
OAuthException :: An active access token must be used to query information about the current user.
The error is at the following line:
user = FbGraph::User.me(access_token).fetch
I tried to look for a solution and modified the code but still couldn't able to resolve the problem. Its all that the oauth token is not valid.
Please help me find a solution. Many thanks!!
Finally I got the error wchich was that after the callback you have to hit the facebook again to get the oauth token. I have modified my code and it worked.
User model
def self.auth(redirect_url)
FbGraph::Auth.new(ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], :redirect_uri => redirect_url)
end
User controller
def facebook
if params[:code]
set_oauth_token(params[:code])
elsif params[:error]
redirect_to landing_path, :alert => "Access Denied"
return
end
if current_user.oauth_token.nil?
client = User.auth(invites_facebook_url).client
redirect_to client.authorization_uri(
:scope => "user_about_me, email, publish_stream, user_location, user_interests, user_birthday, user_likes, user_hometown, offline_access"
)
end
end
private
def set_oauth_token(token)
client = User.auth(invites_facebook_url).client
client.authorization_code = params[:code]
access_token = client.access_token! :client_auth_body
user = FbGraph::User.me(access_token).fetch
current_user.oauth_token = access_token
current_user.save(:validate => false)
end

railstutorial.org NoMethodError in SessionsController#create

undefined method `current_user=' for #<SessionsController:0x1044926b8>
Rails.root: /Users/Bulow/ruby/sample_app
Application Trace | Framework Trace | Full Trace
app/helpers/sessions_helper.rb:4:in `sign_in'
app/controllers/sessions_controller.rb:20:in `create'
Request
Parameters:
{"commit"=>"Sign in",
"session"=>{"password"=>"[FILTERED]",
"email"=>"kennyvonbulow#gmail.com"},
"authenticity_token"=>"/fWZncGEWuhQLIYMxRPXBcBJ37vzLoVrIz1QHU28u6w=",
"utf8"=>"✓"}
Show session dump
Show env dump
Response
Headers:
None
unable to log in simply - and i have no clue how to correct it. been all over the guide back and forth and can't seem to find the error that's causeing this
The online tutorial has the following definitions in sessions_helper.rb
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end
.
.
.
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end
I changed them to the following and was able to login/logout without any errors.
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
#current_user = user
end
.
.
.
def sign_out
cookies.delete(:remember_token)
#current_user = nil
end
Also, to confirm it's you that's logged in, change the URL localhost:3000/users/(your user ID) to another user ID. It'll show that users info. Click on the profile link at the top and it'll go back to your profile. The app is set up so that this link will got to the logged in user's profile.
Where I found the solution.
http://getsatisfaction.com/railstutorial/topics/undefined_method_current_user_for_sessionscontroller

Rails 3: ActionMailer

I'm following this Railscast here for sending mails from my rails app.
At first I tried to set up a mail interceptor like in the tutorial, but I've given up on that, because I always got a lovely can't convert nil into Hash (TypeError).
Now that I left out the interceptor, I wanted to actually send a mail, but guess what - my old friend is back:
can't convert nil into Hash (TypeError)
app/mailers/user_mailer.rb:20:in `registration'
Here's the mailer class (line #20 is the one with the call to mail):
class UserMailer < ActionMailer::Base
def registration( user )
#invited = user
subject = 'Welcome!'
if Rails.env.development?
to = 'my.private#email.com'
subject = "to #{#invited.email}: #{subject}"
else
to = #invited.email
end
logger.info "Sending email to #{to} with subject #{subject}"
mail( :from => 'noreply#mysite.com', :to => to, :subject => subject )
end
end
The log shows:
invite entered for user foo#bar.com
Sending email to foo#bar.com
Sending email to my.private#email.com with subject to foo#bar.com: Welcome!
And here's the requested view file (registration.text.erb):
Welcome!
blablah blah click the following link to activate blah blah <%= activate_user_path( #invited )%> blah blah.
blah blah this is actually german (but plaintext) encoded in utf-8.
Viel Vergnügen beim Verwenden des Systems!
thx for any help
did it, I had an error in actionmailer's config

Rails 3 Flash with jquery

I use flash to tell the user if they logged in correctly or not, is there a way to put in the jquery code to fade after so many seconds or is there a way to do it with just the flash command?
Here is what im talking about
def login
#title = "Log in to Connect my Friends"
if param_posted?(:user)
#user = User.new(params[:user])
user = User.find_by_screen_name_and_password(#user.screen_name,
#user.password)
if user
user.login!(session)
flash[:notice] = "User #{user.screen_name} logged in!"
redirect_to_forwarding_url
else
#user.clear_password!
flash[:notice] = "Invalid screen name/password combination"
end
end
end
See http://snippets.dzone.com/posts/show/5271 for several examples of how to accomplish this.
Another example here: http://bjhess.com/blog/fading-flash-message/