Granting Access to an S3 Object (Specific HTML Page) Using AWS Cognito JS

On my website hosted on s3, I have three HTML files - register.html, login.html, and dashboard.html.

After successfully registering and logging in, I obtain an access token.

What is the best way to limit access to the dashboard.html file and utilize the access token to grant permission to it?

Answer №1

Implementing JavaScript for User Access Control

To address this issue, a simple solution is to load the dashboard.html page and include JavaScript within the body of dashboard.html to manage content visibility based on whether the user is logged in or not (e.g., by checking for a Cognito JWT token in LocalStorage) and redirect unauthenticated users to login.html.

Note: This method can also be beneficial if you intend to utilize AWS Cognito UserPools Hosted UI.

Controlling Access

If the goal is to restrict access to dashboard.html, CloudFront Signed Cookies can be utilized as outlined below.

  • Configure S3 access via AWS CloudFront (establishing an Origin Access Identity - OAI) and set the S3 buckets to private (for all three pages). Link the OAI with the S3 origins using the wizard.
  • Create separate behaviors for each html file.
  • For the login.html and register.html behaviors, maintain Restrict Viewer Access(Use Signed URLs or Signed Cookies) as No
  • For dashboard.html specifically, set Restrict Viewer Access(Use Signed URLs or Signed Cookies) to Yes
  • If a user attempts to directly access dashboard.html (without logging in), CloudFront will generate an error. Therefore, configure the AWS CloudFront error response to direct to Login.html.
  • Develop an API Gateway and Lambda endpoint to validate the Cognito JWT Token from the Login Page, generating a Signed Cookie in Lambda and transmitting it in the setCookie header.
  • The browser will send the Cookie header, allowing CloudFront to permit loading of dashboard.html when the Cookie is present.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can one effectively track changes in authentication state within AWS Cognito?

In my React app, I have integrated AWS Cognito for handling authentication. Rather than utilizing the components offered by the AWS React package, I have opted to work with APIs in the AWS JavaScript library. My goal is to monitor changes in the authentica ...

JavaScript - Marking selected text: What are the options?

I am looking to implement a feature in JavaScript (without jQuery) that will allow me to highlight selected text with control points or markers (left and right). These markers should function similar to those on mobile phones, allowing me to extend the sel ...

Issue with Firebase Storage: Invalid character detected in the string format 'base64'

I am currently developing a project using react-native and I am facing an issue with uploading an image to Firebase using Firebase Storage. To select the image from the phone, I am utilizing react-native-image-picker, which provides me with the base64 enco ...

Removing cookies with angular js: A simple guide

I have a list of cookies that contain commas, and I want to remove a specific item when it is clicked. Here is an example of how my cookies are structured: 879273565,879269461,879273569,659234741 artistcontrollers.controller("CartController", ["$scope", ...

What's the best way to implement image size and type validation, specifically for .jpg and .png files, using Multer?

When using multer to receive files from the FrontEnd, I need to validate the image size to ensure it's less than 1MB. Additionally, I want to restrict the accepted file types to .jpg, .jpeg, and .png only. const multer = require("multer"); c ...

What could be causing a NULL response when there is a JSON Array present in the jQuery.ajax response?

Essentially, the response consists of two things: a JSON Array and isValid(flag). I am able to retrieve the flag value successfully, but when trying to parse the JSON data, it returns null var resJSON = jQuery.parseJSON(data.notification);. I have debugge ...

What are some methods for resolving the problem of CORS policy blocking access to retrieve data from Yahoo Finance?

Currently, I am attempting to retrieve the price of a stock within my pure React App by utilizing fetch. When I try to fetch without any options or configurations, using fetch(url), I encounter the following error: Access to fetch at 'https://quer ...

Use Python to enter a value on a webpage coded in HTML

My current project involves automating the input of values into a webpage. However, I have encountered a significant hurdle with the Mechanize library as it does not recognize the forms on my page which are <input> elements, unlike the typical form.n ...

Retrieving the maximum values from JSON data using D3

Currently, I am working with D3 and JSON data by using a function that looks like this: d3.json("http://api.json", function(jsondata) { var data = jsondata.map(function(d) { return d.number; }); After executing this, the value of the data becomes ["2", ...

Adjust the mouse position upon page loading

I'm looking for a way to manipulate the mouse pointer on my webpage displaying a full-screen slider. I want the slider to stop when the mouse pointer is over it, but I also want the mouse pointer to be displayed at the top of the page when it's n ...

Using JavaScript to modify the width of an element

I have been struggling to increase the width of an element in my HTML using JS, but it's not working correctly. This is crucial for responsive design as the content should scroll when necessary. I attempted to achieve this with the following code, ...

Adjusting diagram size based on screen width using Bootstrap and jQuery

I recently created a diagram that adjusts its size according to the screen width. However, when I implemented this code on my page, I encountered an issue where the final circle or glyph would drop to the next line instead of staying on the same line as in ...

Modifying the HTML5 data attribute according to the URL hash

Within my div element, I have this code: <div id="nav-aj" data-options='{"default":'t1","animation": {"duration": 500, "effects": "fade"}}'> </div> I am looking to dynamically adjust the value of the default option based on th ...

Using JQuery to dynamically set dropdown option values from a JSON object

I have an HTML code snippet: $.ajax({ type: "POST", url: "hanca/hanca_crud.php", dataType: 'json', data: { id_hanca: id_hanca, type: "detail_hanca" }, //detail_hanca success: function(data) { var teks = ""; $.each( ...

Even though my performance in a sandbox environment is excellent, I am unable to obtain a token in a production environment

After posting my question on the Evernote developer forum, I was disappointed to find that the forum had been closed before I received a response. Even after receiving a proposal from an Evernote employee named chanatx to verify if my key was activated co ...

Issue in Angular form: Element removal from the DOM does not remove it at the specified index, instead removes the last item

I'm encountering an issue where the wrong element is being removed from the DOM. In my onDelete() method, I am deleting a specific FormControl at a certain index, but in the actual DOM, it's removing the last item instead of the intended one. Fo ...

Is there a discrepancy in performance when running a function on an individual element versus a group of elements within jQuery?

Imagine having the choice between applying a function to an individual DOM element or a list of them: For Individual Elements: $('#element1').click(function () { $(this).hide(); return false; }); $('#element2').click(functi ...

changing the state of a bootstrap button once a task is completed

I've designed a Bootstrap 5 button to control the motor: <button id=button_90 onclick="return motor_turn(512)" class="btn btn-primary" type="button">90</button> Clicking the button highlights it in blue, and ...

Minimize a pop-up window and navigate to a different webpage

I have created a login/logout form that includes the option to delete my account. When I click the "Sterge cont" button, a pop-up window appears asking if I truly want to delete the account. If I select "NU," the account will not be deleted and the window ...

Implementing a different background image for every menu item when hovering over its parent

I am trying to figure out how to achieve this. My menu is placed in a container that is 500px high, with a background image. I would like the background image to change when hovering over a parent menu item. I believed I could do this using something like: ...