Problem with sessions, subdomains and authlogic in Rails - ruby-on-rails

I've got a rails app with authlogic authentication and a username.domain.com structure built with subdomain-fu.
But my session breaks when going from domain.com to username.domain.com. I've tried to add
config.action_controller.session = {:domain => '.localhost:3000'}
to my development.rb but that seams to break authlogic disabling sign out/sign in.
Any suggestions on what to do?
Thanks in advance!

you are having this issue in the development mode but probably wont have this issue in prod mode.. you are trying to set the top level cookie. your browser wont let you do that. what you are trying to do with
config.action_controller.session = {:domain => '.localhost:3000'}
is as good as saying
config.action_controller.session = {:domain => '.com'}
try creating custom local domain like localhost.localdomain or dummylocal.com or something and that will make it work.
config.action_controller.session = {:domain => 'localhost.localdomain'}
config.action_controller.session = {:domain => 'dummylocal.com'}

For Rails3 the code above will raise NoMethodError:
undefined method `session=' for ActionController::Base:Class
So, for Rails3 you should not change you environment config but should set your app/config/initializers/session_store.rb to look like:
YourAppName::Application.config.session_store :active_record_store,
{:key => '_your_namespace_session', :domain => '.yourdomain.com'}

Maybe this can help: http://erikonrails.snowedin.net/?p=248 ?

Related

Session breaks when use subdomains with dalli store in rails3

I tried to use :domain => :all in session_store.rb and config.action_controller.session = {:domain => '.mydomain.com'} but anyway session breaks when i visit mydomain.com after www.mydmain.com.
Any suggestions?
SubdomainFu.configure do |config|
config.tld_size = 2
config.preferred_mirror = 'www'
end
Try with these configurable options. I hope it helps you.

Subdomain cookie sharing in Rails 3 is not working (on Heroku)?

I'm trying to have cookies on my site dapshare.com work for both the root address and the 'www' subdomain.
A lot of other stackoverflow answers (and the great Railscasts vid on this topic) have suggested adding this line to session_store.rb:
Dapshare::Application.config.session_store :cookie_store, :key => '_dapshare_session', :domain => :all
This doesn't seem to make a difference: if I log in at dapshare.com, I still am not logged in at www.dapshare.com.
Am I doing something wrong here? I am using the following code to store information in the cookie:
cookies.permanent.signed[:thing_to_store] = store_information
Thanks for any help!
Short answer: using the 'cookies[:new_cookie] =' does not seem to grab the domain from the session_store config settings.
I added the :domain to the new cookie and it now works:
cookies.permanent.signed[:new_cookie] = {:value => new_value, :domain => ".dapshare.com"}
For anyone else reading, you also need to specify the domain when deleting the cookie
cookies.delete :new_cookie, :domain => ".dapshare.com"
(Thanks for your help with diagnosis Andrew Marshall.)
You can actually just specify your cookies using domain => :all instead of domain => '.dapshare.com' in Rails 3.1 +:
cookies.permanent.signed[:new_cookie] = {:value => new_value, :domain => :all}
This more flexible than outright specifying a string domain. Now your application won't break on a different production domain.
I encountered this issue, when passing :all doesn't seems to work properly. If you want to use only for subdomains try the following:
Dapshare::Application.config.session_store :cookie_store, :key => '_dapshare_session', :domain => '.dapshare.com'

Rails 3: SMTP Settings for Google Apps / Heroku

Here are my smtp settings for Google Apps in setup_mail.rb.
:address => "smtp.gmail.com",
:port => 587,
:domain => 'mysite.co',
:user_name => 'noreply#mysite.co',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true
My development logs show in detail the e-mails being generated and sent to the right e-mail address... but they are not arriving. I can only think that there must be something wrong with the settings above. Can you see what the problem is?
Once this is solved, would I have any issue getting it to work on Heroku?
Note: the above is logging a deprecation warning:
DEPRECATION WARNING: Giving a hash to body is deprecated, please use instance va
riables instead. (called from process at C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gem
s/actionmailer-3.0.0/lib/action_mailer/old_api.rb:77)
I think if you are using rails 3, the correct approach to setup mail would be to follow this railscasts tutorial on action mailer.
in your user controller don't forget to add the .deliver
UserMailer.registration_confirmation(#user).deliver
that is what stumped me
Turned out that the issue was elsewhere - an old AuthLogic tutorial had put me in the right direction on sending out activation codes but the wrong direction on sending out the e-mails themselves. Rails was generating the e-mail but not sending it because the mail_helper's code wasn't going the final step.
If you're reading this in retrospect, what I learned: make sure your Rails 3.0 user_mailer has (or similar):
mail(:to => "#{user.login} <#{user.email}>", :subject => "Registered" )
I haven't used rails3 but I remember reading that sent emails in development are sent to the sender. Your deprecation warning is because you're defining variables for the email template in the previous hash format. Rails 3 works differently. In my experience deploying to heroku works flawlessly but you will need to define mx records.

Rails: generate a full URL in an ActionMailer view

I'm using ActionMailer to send a sign up confirmation email. The email needs to contain a link back to the site to verify the user, but I can't persuade Rails to generate a full URL (including the domain etc).
I'm using:
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
in my view
Part b:
In development mode Rails gripes if I don't specify the host explicilty in the link above. But I don't want to do this in production. Any solutions?
To solve the problem to pass a host for generating URLs in ActionMailer, check out this plugin and the reason why I wrote it.
To solve the first issue, use named routes when applicable. Instead of
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
assuming the route is called login, use
<%= login_url(:guid => #user.new_user.guid) %>
Note, I'm using login_url, not login_path.
I'm not sure if it is what you want but in config/environments/development.rb you can specify default options for mailer urls
config.action_mailer.default_url_options = {
:host => "your.host.org",
:port => 3000
}
you can do the same in config/environments/production.rb
I don't know why the previous solutions seem so complicated, but since I'm here why not give my 2 cents...
Go to /config/environments and add:
config.absolute_site_url = 'your site url'
for the respective environment (ie. in development.rb, test.rb, or production.rb). Restart web server.
This allows you to call Rails.application.config.absolute_site_url to get the desired URL. No need for plugins or weird cheat, just store the site url as an application wide variable.
I think its not 100% correct way but this can also be a solution :
See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}/login/?guid=#{#user.new_user.guid}"
To generate url, try this
Rails.application.routes.url_helpers.user_url(User.first.id, host: 'yourhost.io')
this will generate url like this:
http://yourhost.io/users/1
As well you can pass some params
expires = Time.now + 2.days
params = {expires: expires}
u = User.first.id
Rails.application.routes.url_helpers.user_url(u, params, host: 'host.com')
will generate:
http://yourhost.io/users/1.expires=2018-08-12+15%253A52%253A15+%252B0300
so you can werifi in action if link is not expired

localhost on rails

I am sending an email that contains a link to my website. I want to be able to test it locally and be able to move the scripts around to different hosts easily.
In my email right now I use the following:
<%= url_for(:host => 'localhost:3000', :controller => "user_activations", :action => "show", :id=>#id, :confirm=>#passcode) %>
This works for when testing locally but will obviously fail for production. Is there an easy way to have rails (or ruby) detect what the current host is? I'm thinking something like $_SERVER of php.
I realize I can use some logic using my environment variable but I would like to avoid this.
Thanks
I define a constant 'HOST' in my environment.rb that sets my host. Alternatively you can use request.host or request.domain.
in environments/development.rb
config.action_mailer.default_url_options = { :host => "localhost", :port => 3000 }
in environments/production.rb
config.action_mailer.default_url_options = { :host => "www.xyu.at" }
and use tests with rspec-email :)

Resources