Redirects not working - Gatsby site

Hello,

I would like to use Netlify to host a design system built with Gatsby. One critical element is being able to use the Identity feature. Looking at the docs on that I saw that redirects was a big part of being able to gate pages behind a login.

In my current POC I cannot get the redirects to work for the life of me. I tried adding them to the netlify.toml file as well as using a _redirects file and none are working. You can check out the repo I am working out of here: https://github.com/antoinejuhel/smarshdesign .

Edit: I have tried multiple times with Pretty URLs turned on and off.
Edit 2: I have installed the gatsby-plugin-netlify as well but that did not help.

Thanks in advance for your help.
Antoine

I did a bit more investigation and am now wondering if I need to use a custom domain in order to have the redirects work? I don’t quite understand why that would be the case but can’t seem to find a definite answer on the matter.

I also tried to setup redirects with Hugo this time and still no luck so I must be doing something wrong…

I see the current redirect for allowing identity user access written like this:

from = "/docs/*"
to = "/docs/:splat"
status = 200
force = true
conditions = {Role = ["Role_Name"]}

Our documentation doesn’t recommend using a “to” location (although this is _redirects not netlify.toml format):

Quoting:

/admin/*	200!	Role=admin

Would you please try changing the netlify.toml redirect rule like so?

from = "/docs/*"
status = 200
force = true
conditions = {Role = ["Role_Name"]}

In other words, just delete the “to” line leaving everything else unchanged. After this is done, please redeploy the site to have the changes detected by Netlify. If this doesn’t resolve the issue, please let us know.

Hello @luke , thanks for helping me out here!

So I believe redirects are now working properly (ended up using a _redirects file but the TOML approach worked as well. I deployed a new test site using Hugo (easier for me to use for testing) which you can check out here: https://hungry-chandrasekhar-ced2a7.netlify.com/. The login button is at the top left sandwiched between the two logos (I haven’t bothered cleaning-up anything until I can the overall redirect/role working). The non-logged in behavior works as expected, when you click on “Docs” in the top-right nav it brings you to “Blog”. The problem is it does the same thing when you are logged in. This leads me to think I am now facing a problem around the identity aspect.

I know the sign-up process is working fine and is properly assign the role of “member” as I can see it automatically assigned in the Identity setting panel:


Oddly enough when I re-deploy the site, I am sometimes still logged in and the redirect logic works: it will let me access “Docs” just fine. However as soon as I log out and log back in, I can no longer access “Docs” and keep on being redirected to “Blog”.

Git repo: https://github.com/antoinejuhel/hugo-test

My _redirects file:
/docs/* 200! Role=member
/docs/* /blog/ 401!

More info on my setup:
I am using a function called “identity-validate.js” which contains the following:

exports.handler = function(event, context, callback) {
const data = JSON.parse(event.body);
const { user } = data;
    
const responseBody = {
    app_metadata: {
        authorization: {
            roles: ["member"]  
        }
    my_user_info: "Logged in user"
    },
};
callback(null, {
    statusCode: 200,
    body: JSON.stringify(responseBody)
});
};

I have also checked that I in fact get a nf_jwt JWT when I log in, and decoded it to find the following payload which seems to indicate that is working as expected:

{
  "exp": 1574719311,
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NzQ3MTkzMTEsInN1YiI6IjNiNmUxNTYyLTE2MTgtNDg5NS04NDc1LTljZjRkZmY5MjU2ZiIsImVtYWlsIjoiYW50b2luZWp1aGVsQGdtYWlsLmNvbSIsImFwcF9tZXRhZGF0YSI6eyJyb2xlcyI6WyJtZW1iZXIiXX0sInVzZXJfbWV0YWRhdGEiOnsiZnVsbF9uYW1lIjoiQW50b2luZSJ9fQ.GQDafdkdteI7Li-elWMcycsK0s5FMholHMV5l7yatw8sub": "3b6e1562-1618-4895-8475-9cf4dff9256f",
  "email": "antoinejuhel@gmail.com",
  "app_metadata": {
    "roles": [
      "member"
    ]
  },
  "user_metadata": {
    "full_name": "Antoine"
  }
}

I know this has now deviated from the original redirect issue so please let me know if I should post this somewhere else.

Thanks again,
Antoine

Hi @antoinejuhel, I suspect this may be a caching issue. Can you please try changing the status code to a 404 for your error route rather than a 401 and let us know if you have similar problems? Thanks.

Hey @futuregerald, I changed the status code from 401 to 404 and cleared my cache but to no avail. I am still being redirected to “Blog” when logged in. I also tested on Firefox, Safari, and the Android Chrome app on my phone in addition to Chrome and all are having the same result.

The 404 status is being properly detected though, as you can see here:

Edit: Right after writing this post, I was still logged-in to the site on Safari and tested it out again but this time it worked and let me access “Docs” just fine. I logged-out and back in and it reverted to not working and kept on redirecting me to “Blog”.

Hi @antoinejuhel, I just tested your site and it’s working for me. did you make any changes? Are you still having the same failure? Have you tried from an incognito window?

Hey @futuregerald,

That is the oddest thing… I had it already opened and logged in when I saw your reply so I went and checked and then it was working: Docs was leading to Docs. However sure enough, I logged out and back in and it wasn’t working. Tested on Incognito and on my phone as well. I think at this point I may just try again from scratch…not sure what else to do.

How are you logging out and logging in? I was manually opening the netlify identity widget from the console. I assume you are doing it a different way. I’d like to recreate it the same way you are doing. Thanks.

@futuregerald, here are the steps to reproduce the behavior I am seeing:

  1. Go to https://hungry-chandrasekhar-ced2a7.netlify.com/

  2. Click on “Docs” in the navigation menu on the top right of the screen:

  3. The redirect will work and redirect you to “Blog” as you are not logged it.

  4. Now click on the temporary “Log in” button located in the top left between the duplicate site logos:

  5. Once logged in go ahead and click on “Docs” again. Currently it still redirects me to “Blog” even though I am logged in. This behavior happens on Chrome, Firefox, Safari, and Chrome for Android. I have experimented with clearing the cache as well as using Incognito/Private modes all with the same result.

Hi @antoinejuhel, it looks like the site is on a starter team. Note that role-based redirects will not work on that plan: Redirect options | Netlify Docs. The site needs to be on a business plan or higher.

@Dennis :man_facepalming: Wish I had figured that out earlier! Thanks for following up on this though.

Happy New Year!
Antoine