Home > OS >  How to redirect user to a page with different subdomain after sign_in/sign_out devise?
How to redirect user to a page with different subdomain after sign_in/sign_out devise?

Time:11-04

I have a problem with redirecting to subdomain after devise sign_in or sign_out. I generated devise controllers uncommented methods that should take me where I want but subdomains don't work.

    def after_sign_in_path_for(resource)
      root_url(subdomain: resource.subdomain)
    end

    def after_sign_out_path_for(resource_or_scope)
      home_url(subdomain: "www")
    end

After sign_in I receive an error

Unsafe redirect to "http://chris.lvh.me:3000/", pass allow_other_host: true to redirect anyway.

So I modified methods in couple ways but it gave me more errors, example of what I've tried:

root_url(subdomain: resource.subdomain), allow_other_hosts: true
root_url(subdomain: resource.subdomain, allow_other_hosts: true)
redirect_to root_url(subdomain: resource.subdomain),allow_other_hosts: true
redirect_to root_url(subdomain: resource.subdomain, allow_other_hosts: true)

And errors for that were: wrong amount of arguments or unexpected ', ' expected end or something.

After sign_out it takes me to sign_in page and not to home_url and subdomain doesn't change.
I'm using devise, rails-on-services/apartment and rails 7

CodePudding user response:

The method name is allow_other_host without a s.

Besides that, you are correct that you should pass it as an argument to redirect_to

redirect_to(root_url, allow_other_host: true)

The method request.subdomain will use the current URL to determine the subdomain. If you like to redirect to a subdomain, different from the current URL, you can specify it.

redirect_to(root_url(subdomain: "chris", allow_other_host: true)
# "http://chris.lvh.me:3000/"
  • Related