netlify / netlify-identity-widget

A zero config, framework free Netlify Identity widget
https://identity.netlify.com
MIT License
770 stars 236 forks source link

Identity doesn't seem to work across subdomains #370

Open TeamTangrams opened 4 years ago

TeamTangrams commented 4 years ago

Hi!

I've created a website that uses subdomains (tangrams.dev, auth.tangrams.dev, notes.tangrams.dev) and I wanted to integrate Netlify Identity to gate access to app.notes.tangrams.dev, but the app always acts as if the user is not signed in or they don't have the required role, redirecting them to notes.tangrams.dev (the landing page).

I made a minimal example with only one subdomain (test.tangrams.dev) and it seems to work, so I think it's to do with logins not transferring between auth.tangrams.dev and notes.tangrams.dev.

Here's the part of the netlify.toml file that deals with the redirects:

# Allow access
[[redirects]]
    from = "https://app.notes.tangrams.dev/*"
    to = "/notesApp/:splat"
    status = 200
    force = true
    conditions = {Role = ["Basic"]}

# Else redirect to landing
[[redirects]]
    from = "https://app.notes.tangrams.dev/*"
    to = "/notes/"
    status = 401
    force = true

# Allow access
[[redirects]]
    from = "https://notes.tangrams.dev/*"
    to = "/notesApp/:splat"
    status = 200
    force = true
    conditions = {Role = ["Basic"]}

# Else redirect to landing
[[redirects]]
    from = "https://notes.tangrams.dev/*"
    to = "/notes/:splat"
    status = 200
    force = true

# Auth subdomain
[[redirects]]
    from = "https://auth.tangrams.dev/*"
    to = "/auth/:splat"
    status = 200
    force = true

In the repository, /notesApp/ is the app and /notes/ is the landing page.

And here's the code used in auth.tangrams.dev (which is where the user is directed when they click the log in or sign up buttons on notes.tangrams.dev):

params = new URLSearchParams(window.location.search)

setTimeout(function() {
    if (params.get("intent") == "logIn") {
        netlifyIdentity.open('login')
    } if (params.get("intent") == "signUp") { 
        netlifyIdentity.open('signup')
    } else if (params.get("intent") == "logOut") {
        netlifyIdentity.logout()
        setTimeout(function(){window.location.href = params.get("redirect")}, 500)
    }
}, 1000)

netlifyIdentity.on('close', function() {
    window.location.href = params.get("redirect")
});

netlifyIdentity.on('login', function() {
    window.location.href = params.get("redirect")
});

Ideally, this would work like this: User tries to access app.notes.tangrams.dev without being logged in and is redirected to landing page --> User clicks 'Log In' and is redirected to auth.tangrams.dev --> User logs in and is then redirected back to app.notes.tangrams.dev --> User is allowed in

However, while the first, second and third step works, app.notes.tangrams.dev always sends the user back to the landing page.

Even if I log the user in on the root domain tangrams.dev, it doesn't work.

Is this intentional? If so, can I work around it somehow?

mdbenito commented 3 years ago

See #414