Getting an "Access Denied" error when I reload my React app on AWS Amplify

Viewed 9737

I am working on a React app that's running on AWS Amplify. My React app is using the react-router-dom library to route to different components. After running amplify publish in the CLI, at the root of the app (obktraining.com) everything is fine in the browser, I can also route to other components in the app just fine as well. But when I refresh my browser while I am on a route (obktraining.com/menu), I get an Access Denied error message.

I have found other posts about similar issues regarding rewrites and redirects in Amplify, but the solutions given do not work for me.

Here is an image of the error: enter image description here

My Amplify app rewrites & redirects : enter image description here

Again, the error only displays when I refresh on a route (obktraining.com/menu or obktraining.com/drinks) not on obktraining.com. Is the issue being caused by the react-router-dom library or is it an issue with Amplify settings? I am not sure where to go from here.

6 Answers

I have been facing the same error since days.The error is being caused by the amplify settings. The solution is simple, Edit your Rewrites and redirects by adding a new rule.

source address = </^[^.]+$|.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf|map|json)$)([^.]+$)/>

target address = /index.html

status = 200 (Rewrite)

Country code can be left blank

Save and try refreshing your app again. It should probably work.

use this for reference: https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html#redirects-for-single-page-web-apps-spa

This is where you can find Redirects on your Amplify Menu

Then you can just put in (as Dhruv Godambe posted above)

</^((?!.(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$).)*$/>

as the Source address and

/index.html as your target address

This is what it looks like in AWS

The correct rule should be like this:

Source address: </^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$)([^.]+$)/>
Target address: /index.html
Status: 200

Reference:
https://docs.aws.amazon.com/amplify/latest/userguide/manual-deploys.html


I have faced the same problem.
I was using Manual Zip File (Compressed) download (wrong).

Here is how to deploy manually correctely:

  1. Run this command
npm run build

Now a build folder will be created.

2. Compress the content of this folder
Not the content of the whole project.
You chnage directory to the build folder and Compress the content in side.

3. Upload that compressed file Inside the build directory Now this should work just fine.
:)

I have faced the same problem. I was zipping build folder, but not the contents within the zip folder.

Here is how I fixed it:

  1. Run build command

    npm run build
    
  2. Now a build folder will be created inside your project directory.

  3. Open build folder.

  4. Compress the contents of build folder, and not build folder.

  5. Now upload this new zip created from sub files and folders from build folder.

App will run fine on AWS Amplify.

Related