Skip to content

Instantly share code, notes, and snippets.

@yohachiSuga
Created December 9, 2018 01:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohachiSuga/930d4f3581d9ee8e024b1fa2808ffbc2 to your computer and use it in GitHub Desktop.
Save yohachiSuga/930d4f3581d9ee8e024b1fa2808ffbc2 to your computer and use it in GitHub Desktop.
AWS_Serverless_Architecture Sample migration from Auth0 lock v9 to v11.
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang=""> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang=""> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
body {
padding-top: 50px;
padding-bottom: 20px;
}
</style>
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/vendor/modernizr-2.8.3-respond-1.4.2.min.js"></script>
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<button id="user-profile" class="btn btn-default">
<img id="profilepicture" />&nbsp;<span id="profilename"></span>
</button>
<button id="auth0-login" class="btn btn-success">Sign in</button>
<button id="auth0-logout" class="btn btn-success">Sign Out</button>
</div><!--/.navbar-collapse -->
</div>
</nav>
<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="jumbotron">
<div class="container">
<h1>Hello, world!</h1>
<p>This is a template for a simple marketing or informational website. It includes a large callout called a jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more &raquo;</a></p>
</div>
</div>
<div class="container">
<!-- Example row of columns -->
<div class="row">
<div class="col-md-4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Heading</h2>
<p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
<p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
</div>
</div>
<hr>
<footer>
<p>&copy; Company 2015</p>
</footer>
</div> <!-- /container --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.2.min.js"><\/script>')</script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/main.js"></script>
<!-- Auth 11 の利用-->
<script src="https://cdn.auth0.com/js/lock/11.11.0/lock.min.js"></script>
<!-- <script src="https://cdn.auth0.com/js/lock-9.min.js"></script> -->
<script src="js/user-controller.js"></script>
<script src="js/config.js"></script>
<!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
<script>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create','UA-XXXXX-X','auto');ga('send','pageview');
</script>
</body>
</html>
var userController = {
data: {
auth0Lock: null,
config: null
},
uiElements: {
loginButton: null,
logoutButton: null,
profileButton: null,
profileNameLabel: null,
profileImage: null
},
init: function(config) {
var that = this;
this.uiElements.loginButton = $('#auth0-login');
this.uiElements.logoutButton = $('#auth0-logout');
this.uiElements.profileButton = $('#user-profile');
this.uiElements.profileNameLabel = $('#profilename');
this.uiElements.profileImage = $('#profilepicture');
this.data.config = config;
// v9
// this.data.auth0Lock = new Auth0Lock(config.auth0.clientId, config.auth0.domain);
this.data.auth0Lock = new Auth0Lock(config.auth0.clientId, config.auth0.domain,{
popup:true,
auth: {
params:{
scope: 'openid email user_metadata picture'
}
}
});
this.data.auth0Lock.on('authenticated',function(authResult){
console.log("debug");
localStorage.setItem('userToken', authResult.accessToken);
that.configureAuthenticatedRequests();
that.data.auth0Lock.getUserInfo(authResult.accessToken,function(err,userInfo) {
if(!err){
console.log("can get user info");
that.showUserAuthenticationDetails(userInfo);
}else{
console.log("cannot get user info");
}
});
});
var idToken = localStorage.getItem('userToken');
if (idToken) {
this.configureAuthenticatedRequests();
this.data.auth0Lock.getUserInfo(idToken,function (err,userInfo) {
if (err) {
return alert('There was an error getting the profile: ' + err.message);
}
that.showUserAuthenticationDetails(userInfo);
});
}
this.wireEvents();
},
configureAuthenticatedRequests: function() {
$.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('userToken'));
}
});
},
showUserAuthenticationDetails: function(profile) {
var showAuthenticationElements = !!profile;
if (showAuthenticationElements) {
// auth0 lock cannot get nickname profile pic when we run applications on localhost. https://auth0.com/docs/libraries/lock/v11/sending-authentication-parameters
// auth0 lockでは、localhost実行の間は、ニックネームとユーザーアイコンの取得は404になる
this.uiElements.profileNameLabel.text(profile.email);
this.uiElements.profileImage.attr('src', profile.picture);
}
this.uiElements.loginButton.toggle(!showAuthenticationElements);
this.uiElements.logoutButton.toggle(showAuthenticationElements);
this.uiElements.profileButton.toggle(showAuthenticationElements);
},
wireEvents: function() {
var that = this;
this.uiElements.loginButton.click(function(e) {
that.data.auth0Lock.show();
});
this.uiElements.logoutButton.click(function(e) {
localStorage.removeItem('userToken');
that.uiElements.logoutButton.hide();
that.uiElements.profileButton.hide();
that.uiElements.loginButton.show();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment