Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirecting to a subdomain after post request #99

Closed
drfraker opened this issue Nov 14, 2019 · 12 comments
Closed

Redirecting to a subdomain after post request #99

drfraker opened this issue Nov 14, 2019 · 12 comments

Comments

@drfraker
Copy link

drfraker commented Nov 14, 2019

I'm writing an app using a multi-tenant architecture. When a new customer signs up they are in the central part of the app. When they submit the registration form, we process payment, set up their tenancy and new database, add the admin user to their users table, log in the admin user and redirect to their dashboard page on their subdomain. When I do this using regular laravel views it works. However, when I'm trying it with inertia I get a cors error in the console. Is there some way I'm not seeing in the docs that explains how to do this?

Workflow overview:

  1. visit example.com/register and enter details & cc info
  2. click register button which fires a post request to the store method
  3. store method sets up tenancy and new database, logs user in, redirects to acme.example.com/app
    like so: return redirect()->to('acme.example.com/app');
  4. With inertia views this is not working due to cors problems and/or logged in user in the session not persisting. When I add the spatie/laravel-cors package it still does not work. If I add cors headers in bootstrap/app.php I can make the cors problem go away, but then the user is redirected back to acme.example.com/login because the session is not persisted.

This is the cors error I see: Access to XMLHttpRequest at 'http://foo.example.test/app/login' (redirected from 'http://example.test/register') from origin 'http://example.test' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

@sicsol
Copy link

sicsol commented Nov 17, 2019

@drfraker have you set your SESSION_DOMAIN to something like .example.com so your sessions will persist across domains?

Also what did you add in the bootstrap/app.php file to remove the cors errors?

@drfraker
Copy link
Author

@sicsol Hey Thanks for the response. I added this to the bootstrap/app.php file, just as a way to debug the issue.

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');

Where would one set the SESSION_DOMAIN value? I've never heard of that before.

@sicsol
Copy link

sicsol commented Nov 19, 2019

@drfraker it's in the config/session.php file line 156. You can hard code it there otherwise you can also add it to your .env file as a key and value. Just make sure you add the . before the domain name.

@reinink
Copy link
Member

reinink commented Dec 4, 2019

@drfraker Did you get this figured out? This is an annoying problem that really has to do more with the fact that you're making xhr requests than Inertia.js requests. Have you considered simply making these particular signup requests using a full page visit?

@drfraker
Copy link
Author

drfraker commented Dec 6, 2019

@reinink Thanks for replying, that (full page visit) is what we ended up going with.

@Larsklopstra
Copy link

I face this issue when dealing with a payment processor (mollie-cashier) - is it possible to redirect the user to the payment page if the form doesn't fail? A full page visit isn't pretty if the form fails for example... 🤔

@reinink
Copy link
Member

reinink commented Feb 5, 2020

Another option here is to return a 409 response with the X-Inertia-Location header set. Here is an example of this: inertiajs/inertia-laravel#57 (comment)

@kadusalles
Copy link

Another option here is to return a 409 response with the X-Inertia-Location header set. Here is an example of this: inertiajs/inertia-laravel#57 (comment)

Hi, i don't understand how i can use this to redirect to another page, can you explain to me please?

@claudiodekker
Copy link
Member

claudiodekker commented Aug 22, 2020

Hi @kadusalles 👋 ,

All you need to do is either use an inertia link, a form request method (such as post, patch etc.), or one of the manual request methods to make a request to your back-end.

Then, on the back-end itself, make sure that the response to that request is what @reinink described in his comment. Once Inertia that response, it should automatically redirect the browser to the location you specified in the X-Inertia-Location header.

Hope this helps!

@reinink
Copy link
Member

reinink commented Oct 2, 2020

Redirecting to external sites server-side, from within an Inertia request, is now possible using the new Inertia "location" method:

// Laravel
return Inertia::location('https://www.google.com');

And this is coming to the Ruby adapter shortly (inertiajs/inertia-rails#48).

inertia_location('http://google.com')

More in the docs here and on Twitter here.

@kevnk
Copy link

kevnk commented Jul 21, 2021

We have a site that switches subdomains a lot... so we wrapped the <inertia-link> component to check:

<template>
  <inertia-link :href="this.href" @click="visitLink">
    <slot/>
  </inertia-link>
</template>

<script>
  export default {
    name: 'SubdomainLink',

    props: ['href'],

    computed: {
      currentSubdomain() {
        return window.location.host.split('.')[1] ? window.location.host.split('.')[0] : null;
      },
      linkedSubdomain() {
        let linkedSubdomain = this.href.split('.')[1] ? this.href.split('.')[0] : null;
        return linkedSubdomain.replace('https://', '');
      }
    },

    methods: {
      visitLink() {
        if (this.currentSubdomain !== this.linkedSubdomain) {
          // Invalid subdomain link, force entire page visit/hard refresh
          window.location.href = this.href;
        }
      }
    }
  };
</script>

@MooseSaeed
Copy link

Redirecting to external sites server-side, from within an Inertia request, is now possible using the new Inertia "location" method:

// Laravel
return Inertia::location('https://www.google.com');

And this is coming to the Ruby adapter shortly (inertiajs/inertia-rails#48).

inertia_location('http://google.com')

More in the docs here and on Twitter here.

how about redirecting to a subdomain on the same site? I can't seem to solve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants