I have solved the problem by using the following code. This make sure when the users clicks the confirmation link he is not redirected back to the confirmation link again on signing in as the confirmable module logs us in after confirmation right away.
I am using Rails 4, Devise version 3.03.
def store_location
# store last url - this is needed for post-login redirect to whatever the user last visited.
if (!request.fullpath.match("/users/") &&
!request.xhr?) # don't store ajax calls
session[:previous_url] = request.fullpath
end
end