Skip to content

Try out Cognito for identifying users via federated login. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"PACE": "pace#^1.0.2",
"classie": "^1.0.1",
"wow": "^1.1.2",
"respond": "^1.4.2"
"respond": "^1.4.2",
"jsbn": "^1.3.0",
"sjcl": "^1.0.3",
"moment": "^2.13.0"
}
}
5 changes: 5 additions & 0 deletions gulp/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ module.exports =
'classie'
'AnimatedHeader'
'wow'
'aws'
'amazon'
'jsbn'
'sjcl'
'moment'
'inspinia'
]
ie: [
Expand Down
26 changes: 24 additions & 2 deletions gulp/lib/component.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ config = require '../config'
module.exports = (pkg, kind) ->
[ p, f ] = pkg.split('/')

if p in [ 'inspinia', 'AnimatedHeader' ]
if p in [ 'inspinia', 'AnimatedHeader', 'amazon' ]
if f
vendorComponent(p, kind, f)
else
Expand Down Expand Up @@ -34,6 +34,17 @@ bowerComponent = (pkg, kind) ->
js: 'html5shiv/dist/html5shiv.js'
animate:
css: 'animate.css/animate.css'
aws:
js: 'aws-sdk/dist/aws-sdk.js'
jsbn:
js: [
'jsbn/jsbn.js'
'jsbn/jsbn2.js'
]
sjcl:
js: 'sjcl/sjcl.js'
moment:
js: 'moment/moment.js'

if !pkgs[pkg] || !pkgs[pkg][kind]
throw "couldnt find " + kind + " for package named '" + pkg + "'"
Expand All @@ -55,8 +66,19 @@ vendorComponent = (pkg, kind, file = 'default') ->
AnimatedHeader:
js:
default: 'js/cbpAnimatedHeader.js'
amazon:
js:
default: [
'javascripts/aws-cognito-sdk.min.js'
'javascripts/amazon-cognito.min.js'
'javascripts/amazon-cognito-identity.min.js'
]

if !pkgs[pkg] || !pkgs[pkg][kind] || !pkgs[pkg][kind][file]
throw "couldnt find #{kind} for package named '#{pkg}'."

"#{config.vendorDir}/#{pkg}/#{pkgs[pkg][kind][file]}"
files = pkgs[pkg][kind][file]
files = [files] if typeof files is 'string'

files.map (file) ->
"#{config.vendorDir}/#{pkg}/#{file}"
3 changes: 2 additions & 1 deletion gulp/tasks/javascripts.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ javascriptFilesFor = (filename) ->

jsTasks = (filename) ->
gulp.task "build:javascripts:#{filename}:development", ->
gulp.src javascriptFilesFor filename
gulp.src javascriptFilesFor(filename).concat(["#{config.sourceDir}/javascripts/#{filename}.js"])
.pipe sourcemaps.init()
.pipe concat "#{filename}.js"
.pipe sourcemaps.write '.'
Expand All @@ -31,3 +31,4 @@ Object.keys(config.assets.javascripts).map jsTasks
gulp.task 'watch:javascripts', ->
for task in config.assets.javascripts
gulp.watch javascriptFilesFor(task), [ "build:javascripts:#{task}:development" ]
gulp.watch ["#{config.sourceDir}/javascripts/#{task}.js"], [ "build:javascripts:#{task}:development" ]
2 changes: 2 additions & 0 deletions source/_metadata.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
<%= partial 'metadata/facebook' %>
<%= partial 'metadata/twitter' %>
<%= partial 'metadata/schema' %>

<meta name="google-signin-client_id" content="923050889968-4o4u5a34mbnptfsv8aqa20m6gb7qjhr7.apps.googleusercontent.com">
107 changes: 106 additions & 1 deletion source/javascripts/all.js
Original file line number Diff line number Diff line change
@@ -1 +1,106 @@
// This is where it all goes :)
// This is where it all goes :)

$(function() {
sjcl.random.startCollectors();

AWS.config.region = 'us-east-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:04bf3f30-8ea2-44d5-961f-1b7f61450363',
Logins: {}
});

AWS.config.logger = console;

console.log("Hello world.");

AWS.config.credentials.get(function() {
var identityId = AWS.config.credentials.identityId;
console.log(identityId);

var syncClient = new AWS.CognitoSyncManager();
syncClient.openOrCreateDataset('rubyonrails.co.uk', function(err, dataset) {
dataset.put('hello', 'World', function(err, record) {
dataset.synchronize({
onSuccess: function(data, newRecords) {
console.log("Success!");
console.log(data);
console.log(newRecords);
}
});
});
});

var poolData = {
UserPoolId: 'us-east-1_XRvr3oTui',
ClientId: '7bqasb3pgsgt5cduktgqqugrgd',
Paranoia: 7
};

var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

var cognitoUser = userPool.getCurrentUser();
if(cognitoUser != null) {
cognitoUser.getSession(function(err, result) {
if(result) {
console.log('You are now logged in.');
}
});
}

});
});

function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);

if(response.status === 'connected') {
testAPI();
} else if(response.status === 'not_authorized') {
document.getElementById('status').innerHTML = 'Please log into this app';
} else {
document.getElementById('status').innerHTML = 'Please log into Facebook';
}
}

function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}

function testAPI() {
console.log('Welcome! Fetching your information...');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!';
});
}

function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log("Google user information");
console.log(profile);

AWS.config.credentials.params.Logins['accounts.google.com'] = googleUser.getAuthResponse().id_token;
AWS.config.credentials.expired = true;

AWS.config.credentials.get(function() {
console.log("Credentials gotten for Google. identityId = " + AWS.config.credentials.identityId);

var syncClient = new AWS.CognitoSyncManager();
syncClient.openOrCreateDataset('rubyonrails.co.uk', function(err, dataset) {
dataset.put('currentLogin', 'Google', function(err, record) {
dataset.put('name', profile.getName(), function(err, record) {
dataset.put('email', profile.getEmail(), function(err, record) {
dataset.synchronize({
onSuccess: function(data, newRecords) {
console.log('Google profile synchronised.');
}
});
});
});
});
});
});
}
30 changes: 30 additions & 0 deletions source/sign-in.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Sign in
layout: landing_page
---
<% content_for :navbar do %>
<li><%= link_to 'Home', '', fragment: 'page-top', class: 'page-scroll' %></li>
<% end %>

<section id="sign-in" class="features white-section">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<div class="navy-line"></div>
<h1>Sign in</h1>

<p class="lead">
Let's see if we can figure out some way of doing login...
</p>

<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>

<div class="g-signin2" data-onsuccess="onSignIn"></div>

<p id="status">
</p>
</div>
</div>
</div>
</section>
20 changes: 20 additions & 0 deletions vendor/amazon/javascripts/amazon-cognito-identity.min.js

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions vendor/amazon/javascripts/amazon-cognito.min.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions vendor/amazon/javascripts/aws-cognito-sdk.min.js

Large diffs are not rendered by default.