Skip to content
This repository was archived by the owner on Sep 15, 2022. It is now read-only.

Commit

Permalink
Simple auth layer with Cognito
Browse files Browse the repository at this point in the history
  • Loading branch information
GearsAD committed Aug 9, 2020
1 parent 2908c34 commit 10cfcf8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/

# next.js build output
.next

sandbox.txt
23 changes: 23 additions & 0 deletions auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function setAuthToken(token) {
return window.localStorage.setItem('authToken', token);
}

function setIdToken(token) {
return window.localStorage.setItem('idToken', token);
}

function getAuthToken() {
return window.localStorage.getItem('authToken');
}

function getIdToken() {
return window.localStorage.getItem('idToken');
}

function clearTokens() {
window.localStorage.clear();
}

function getLoginUrl() {
return "https://synchrony.auth.us-east-2.amazoncognito.com/login?response_type=token&client_id=13d5f6opbi4o4sevg6fm3lpuml&redirect_uri=http://localhost:8000/callback&state=STATE&scope=openid+profile";
}
39 changes: 39 additions & 0 deletions callback/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>PoC Javascript Device</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src='../auth.js'></script>
<script>
//Redirect to auth if the user has not logged in.
function checkAuth() {
// Well this is weird, but ok...
// Sure it's a configuration thing in Cognito.
rawUrl = window.location.href;
url = new URL(rawUrl.replace("/#", "?"));
var urlParams = new URLSearchParams(url.search);

if (urlParams.has('id_token') && urlParams.has('access_token')) {
//TODO: Validate tokens with Cognito.
setAuthToken(urlParams.get('access_token'));
setIdToken(urlParams.get('id_token'));
}
gotoOrigin();
}

function gotoOrigin() {
rawUrl = window.location.href;
url = new URL(rawUrl.replace("/#", "?"));
window.location = url.origin;
}
window.addEventListener('load', checkAuth, false);
</script>
</head>
<body>
<h1>Successful Login</h1>
If you're not redirected automatically please click <a href="javascript:gotoOrigin()">here</a>.
</body>
</html>
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
<script src='apriltag.js'></script>
<script src="detector.js"></script>
<script src="capture.js"></script>
<script src='auth.js'></script>
<script>
//Redirect to auth if the user has not logged in.
function checkAuth() {
console.log("Checking auth");
// TODO: Check with Cognito that the tokens are valid.
if (getAuthToken() == null) {
window.location = getLoginUrl();
}
}

window.addEventListener('load', checkAuth, false);

</script>
</head>
<body>
<script>
Expand Down

0 comments on commit 10cfcf8

Please sign in to comment.