Examples of a redirects file in root folder using netlify ONLY

I follow so far, and have logged in and out. I just added the library page (I should have done that before my apologies) with some css, and am now seeking to be directed to the library page after logging in, while as per before, unauthorized users are sent to the login page.

We need to see your site - just like I showed you a demo of mine :slight_smile:

It appears you still haven’t merged my PR - so I’m not sure if you’re experiencing issues with the old codebase or with the fixes I’ve submitted.

I gather a PR means pull request (idk). I see you “forked” from my site to yours (a term which is new to me). In context, I went to add the same files I added to my site to yours to no avail. It says I don’t have push access to upload files, but is that what you mean by “merge”? To add files to your demo? My site is the same as it was:

If you can fork the files over to you again it’s fine with me.

Let’s try a different approach…

You can try downloading my repo: https://github.com/Hrishikesh-K/pie-3.14

Then, the folder that you get after extracting, try uploading it to Netlify as a manual drag-n-drop site.

Enable Identity for the new site and try using that site.

1 Like

I’ve done everything you asked, then added my library page, but after I log in, I’m left on that page instead of being directed to my library page. I also set my role as an admin.

I ask again…could you share a site URL? And also the credentials like I shared above?

In the example I shared here:

are you seeing the behaviour you’re expecting?

Here is the url: https://joyful-pegasus-e6361b.netlify.app

Credentials:

Email: cmooregee@gmail.com
Password: testing123

It appears to be working correctly.

When you go to the homepage: https://joyful-pegasus-e6361b.netlify.app/ and login, you won’t be redirected anywhere, which is because of this code:

https://github.com/Hrishikesh-K/pie-3.14/blob/main/index.html#L16-L18

Without logging in, if you try to visit: https://joyful-pegasus-e6361b.netlify.app/library/, you can see that, you’d see the home page. Now, if you login, you’d be redirected back to the correct page.

Then, if you logout from the https://joyful-pegasus-e6361b.netlify.app/library/ page, you’d be redirected back to the home page. To me, this appears to be working as expected. Could you let me know what’s not working as expected?

You state that I will be redirected back to the correct page, but the login (index) page is not the correct page. The LIBRARY (library.html) page is where I want to be sent after logging in.

I pointed to the exact piece of code causing that.

The reason why I’ve written the code like that is because of a valid reason, but you can change it if you wish to. My current code will redirect back to the library page only if someone tried to access the library page. If someone logs into the home page, I did not want to redirect them to the library page, but you can do it like:

if (location.pathname.startsWith('/library')) {
  location.reload()
} else if (location.pathname === '') {
  location.href = '/library/'
}

At this point, I’d really advise you to try to understand the code that I’ve shared and start experimenting based on that. We cannot actively provide support with writing code. I gave a short example, but it’s up to you how you wish to develop it or adapt it for your needs and applications.

You have two index pages in the folder. One is inside the library, and one outside of it. They both have different code on it. What is the difference? And which page should this new code you provided me be placed on replacing the prior code?

Neither works. You don’t know how to do it. It’s ok, I’ll find a school. Thanks for trying.

@MM6th The repository that has been provided by @hrishikesh along with the most recent reply would achieve what you’re looking for. So it’s safe to say that @hrishikesh does know how to “do it”.

Based on some of your questions throughout the thread, it would definitely be beneficial for you to continue to skill up on some of the basics of web development, so best of luck on that front.

The two index files exist because it’s long standing default behavior of web servers, in each case they are the “default file that should be returned when accessing that folder” (instead of displaying an index listing of all files within the folder).

Thus…
The /index.html file is the one that is loaded when someone accesses the root /
The /library/index.html file is the one that is loaded when someone accesses /library/

There is barely any code in either file, so the difference between them is obvious simply by reading them.

The one at /index.html is intended for unauthenticated users and it listens for the login event
…Upon login you want this one to redirect to /library

The one at /library/index.html is intended for authenticated users and it listens for the logout event
…Upon logout this one redirects to /

In the root /index.html file if you change the JavaScript from:

netlifyIdentity.on('login', () => {
  if (location.pathname.startsWith('/library')) {
    location.reload()
  }
})

to either what @hrishikesh has, or the equivalent of what I posted two weeks ago…

netlifyIdentity.on('login', () => {
  window.location.href = '/library/'
})

Then when someone is unauthenticated, while on the root /index.html page and it detects a “login” event, they will be sent to the /library/ folder, which will subsequently return the /library/index.html file.

If you’re wondering about subtle differences with things like location, window.location, window.location.href these are just different ways to get/set:

It’s basic client side JavaScript.

2 Likes

Echoing @nathanmartin, @hrishikesh certainly does know “how to do it.” Considering the time spent by all here assisting you with this issue, I consider such sentiments a little rude on your part @MM6th.

The solution(s) offered here do work as I have used exactly these without issue.