Skip to content

Commit d6675f3

Browse files
Aleksandar SimovicAleksandar Simovic
authored andcommitted
add fb login for cognito
1 parent 40aff55 commit d6675f3

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

iot-mqtt-websocket-chat/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7+
"start": "claudia create --region us-east-1 --api-module api --set-endpoint --set-pool-id",
78
"test": "echo \"Error: no test specified\" && exit 1"
89
},
910
"author": "",
1011
"license": "ISC",
1112
"dependencies": {
1213
"aws-device-gateway-signed-url": "github:gojko/aws-device-gateway-signed-url",
13-
"claudia-api-builder": "^1.5.1",
14+
"claudia-api-builder": "^2.3.1",
1415
"denodeify": "^1.2.1"
1516
},
1617
"devDependencies": {
17-
"claudia": "^1.6.2"
18+
"claudia": "^2.5.0"
1819
}
1920
}

iot-mqtt-websocket-chat/website/index.html

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ <h1>Hi there</h1>
44
<form onsubmit="return false;" action="#">
55
<input id="poolId" placeholder="pool id" value="us-east-1:{{poolid}}"/> <br/>
66
<input id="channel" placeholder="channel topic" value="game/123"/> <br/>
7-
<button id="go">Go</button>
7+
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
8+
</fb:login-button>
89
</form>
910

1011
<hr/>
@@ -16,7 +17,7 @@ <h1>Hi there</h1>
1617

1718
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>
1819
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.5.2.min.js"></script>
19-
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
20+
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
2021

2122
<script>
2223

@@ -38,7 +39,7 @@ <h1>Hi there</h1>
3839
});
3940
}
4041

41-
42+
4243
MQTTClient.prototype.on = function(event, handler) {
4344
if (!this.listeners[event]) {
4445
this.listeners[event] = [];
@@ -109,7 +110,7 @@ <h1>Hi there</h1>
109110
url: '/dev/endpoint-url',
110111
method: 'POST',
111112
dataType: "json",
112-
data: JSON.stringify({
113+
data: JSON.stringify({
113114
secretKey: credentials.secretAccessKey,
114115
accessKey: credentials.accessKeyId,
115116
sessionToken: credentials.sessionToken
@@ -145,24 +146,78 @@ <h1>Hi there</h1>
145146
});
146147
};
147148

149+
function statusChangeCallback(response) {
150+
if (response.status === 'connected') {
151+
152+
console.log('logged in');
153+
154+
if (response.authResponse) {
155+
handleLogin(response);
156+
157+
} else {
158+
console.log('There was a problem logging you in.');
159+
}
160+
161+
} else if (response.status === 'not_authorized') {
162+
// The person is logged into Facebook, but not your app.
163+
document.getElementById('status').innerHTML = 'Please log ' +
164+
'into this app.';
165+
} else {
166+
// The person is not logged into Facebook, so we're not sure if
167+
// they are logged into this app or not.
168+
document.getElementById('status').innerHTML = 'Please log ' +
169+
'into Facebook.';
170+
}
171+
}
148172

149-
window.go.addEventListener('click', function () {
150-
AWS.config.region = window.poolId.value.split(':')[0];
173+
174+
function checkLoginState() {
175+
FB.getLoginStatus(function(response) {
176+
statusChangeCallback(response);
177+
});
178+
}
179+
180+
window.fbAsyncInit = function() {
181+
FB.init({
182+
appId : '104266690079319',
183+
cookie : true, // enable cookies to allow the server to access
184+
// the session
185+
xfbml : true, // parse social plugins on this page
186+
version : 'v2.8' // use graph api version 2.8
187+
});
188+
189+
FB.getLoginStatus(function(response) {
190+
statusChangeCallback(response);
191+
});
192+
193+
function handleLogin(response){AWS.config.region = window.poolId.value.split(':')[0];
151194
var credentials = new AWS.CognitoIdentityCredentials({
152-
IdentityPoolId: window.poolId.value
195+
IdentityPoolId: window.poolId.value,
196+
Logins: {
197+
'graph.facebook.com': response.authResponse.accessToken
198+
}
153199
});
154200
credentials.get(function(err) {
155201
if(err) {
156202
console.log(err);
157203
return;
158204
}
159-
console.log('access key',
205+
console.log('access key',
160206
credentials.accessKeyId);
161207
console.log('secret key', credentials.secretAccessKey);
162208
console.log('session token', credentials.sessionToken);
163209
startClient(credentials);
164210
});
165-
});
211+
}
212+
213+
// Load the SDK asynchronously
214+
(function(d, s, id) {
215+
var js, fjs = d.getElementsByTagName(s)[0];
216+
if (d.getElementById(id)) return;
217+
js = d.createElement(s); js.id = id;
218+
js.src = "https://connect.facebook.net/en_US/sdk.js";
219+
fjs.parentNode.insertBefore(js, fjs);
220+
}(document, 'script', 'facebook-jssdk'));
166221

167222
</script>
168223
</body>

0 commit comments

Comments
 (0)