I’m a beginner programmer and am working on an email confirmation. Once users have registered it sends them to a page to enter a confirmation code, which will be sent to the email they used. If submitted correctly it saves the User and logs them in.
I had looked into using Devise and maybe that’s the way to go, but it seems I would spend as much time learning someone else’s code when I could learn to do it myself. My repo is Here.
What I was thinking of coding (in my User controller) is shaping up like this…what do you think? Am I completely off and would be better figuring out Devise/Authlogic or am I on the right track? I’m using rails 3.1. Any help would be much appreciated. Thanks in advance.
def confirmation_code_to_register(string)
@confirmation_code = #random number
end
def create_start
@user = User.new(params[:user])
#send email with @confirmation_code via ActionMailer
redirect_to page_to_enter_confirmation_code
email_authenticate
end
def email_authenticate
if #confirmation code user enters == @confirmation_code
create_finish
else
#sorry, you entered the wrong confirmation code.
end
end
def create_finish
if @user.save
sign_in @user
flash[:success] = "Welcome to the Site"
redirect_to @user
else
@title = "Sign Up"
render 'new'
end
end
Consider using Sorcery instead of Devise. There is Railscast about it.
https://github.com/NoamB/sorcery
It more simpler to use, and I think it fulfill your authentication needs.
PD: Check the Edit I did to your post. Try to use that syntax when you post code (just indent the code, no html markup)
EDIT:
And that code should be in the User model, not the controller. Try to keep your controller small, very small. Put all the logic you can in the models.