Created
December 20, 2017 21:03
-
-
Save aaguiarz/718aa8eb9cdb57888a396c336ba715e8 to your computer and use it in GitHub Desktop.
Account Linking using Access Token as Bearer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<script src="https://cdn.auth0.com/js/lock/11.0.0-beta.9/lock.min.js"></script> | |
<script src="https://cdn.auth0.com/js/auth0/9.0.0-beta.10/auth0.min.js"></script> | |
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | |
<script src="//use.typekit.net/iws6ohy.js"></script> | |
<script>try{Typekit.load();}catch(e){}</script> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- font awesome from BootstrapCDN --> | |
<link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.5/paper/bootstrap.min.css" rel="stylesheet"> | |
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"> | |
</head> | |
<body class="home"> | |
<div class="container"> | |
<div class="login-page clearfix"> | |
<div class="login-box auth0-box before"> | |
<h3>Linking Accounts Bug Sample</h3> | |
<div><ul> | |
<li>Login with a primary identity</li> | |
<li>Login with a secondary identity</li> | |
<li>Click 'Link Acount'</li> | |
</ul></div> | |
<button onclick="loginPrimary()" class="btn btn-primary btn-lg">Sign In Primary Identity</button> | |
<button onclick="loginSecondary()" class="btn btn-primary btn-lg">Sign In Secondary Identity</button> | |
<button onclick="linkAccount()" class="btn btn-primary btn-lg">Link Account</button> | |
</div> | |
</div> | |
</div> | |
</body> | |
<script> | |
var AUTH0_CLIENT_ID='uOzFw1rrgGSXxCxX5MsJljUnU5qV0n0n' | |
var AUTH0_DOMAIN='aaguiar0.auth0.com' | |
var lockPrimary; | |
var lockSecondary; | |
var primary = false; | |
function loginPrimary() | |
{ | |
primary = true; | |
lockPrimary.show(); | |
} | |
function loginSecondary() | |
{ | |
primary = false; | |
lockSecondary.show(); | |
} | |
function linkAccount(){ | |
// At this point you could fetch the secondary account's user_metadata for merging with the primary account. | |
// Otherwise, it will be lost after linking the accounts | |
var primaryJWT = localStorage.getItem('primary_access_token'); | |
var primaryUserId = localStorage.getItem('primary_user_id'); | |
var secondaryJWT = localStorage.getItem('secondary_id_token'); | |
$.ajax({ | |
type: 'POST', | |
url: 'https://' + AUTH0_DOMAIN +'/api/v2/users/' + primaryUserId + '/identities', | |
data: { | |
link_with: secondaryJWT | |
}, | |
headers: { | |
'Authorization': 'Bearer ' + primaryJWT | |
} | |
}).then(function(identities){ | |
alert('linked!'); | |
}).fail(function(jqXHR){ | |
alert('Error linking Accounts: ' + jqXHR.status + " " + jqXHR.responseText); | |
}); | |
} | |
$(document).ready(function() { | |
lockPrimary = new Auth0Lock(AUTH0_CLIENT_ID, AUTH0_DOMAIN, { | |
autoclose: true, | |
redirect: false, | |
auth: { | |
responseType: 'token id_token', | |
audience: 'https://' + AUTH0_DOMAIN + '/api/v2/', | |
params: { | |
scope: 'openid profile email read:current_user update:current_user_identities' | |
} | |
} | |
}); | |
lockSecondary = new Auth0Lock(AUTH0_CLIENT_ID, AUTH0_DOMAIN, | |
{ | |
autoclose: true, | |
redirect: false, | |
auth: { | |
responseType: 'token id_token', | |
audience: 'https://' + AUTH0_DOMAIN + '/userinfo', | |
params: { | |
scope: 'openid' | |
} | |
} | |
}); | |
// This handles events from both Lock instances | |
lockPrimary.on("authenticated", function(authResult) { | |
if (primary) { | |
localStorage.setItem('primary_access_token', authResult.accessToken); | |
localStorage.setItem('primary_id_token', authResult.idToken); | |
localStorage.setItem('primary_user_id', authResult.idTokenPayload.sub); | |
} else { | |
localStorage.setItem('secondary_access_token', authResult.accessToken); | |
localStorage.setItem('secondary_id_token', authResult.idToken); | |
localStorage.setItem('secondary_user_id', authResult.idTokenPayload.sub) | |
} | |
}); | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you click 'Link Account' you get:
{"statusCode":400,"error":"Bad Request","message":"JWT (link_with) contains an invalid aud claim.","errorCode":"invalid_body"}
payload for the id_token:
{
"iss": "https://aaguiar0.auth0.com/",
"sub": "auth0|597a2a2d497425796a81a8a1",
"aud": "uOzFw1rrgGSXxCxX5MsJljUnU5qV0n0n",
"iat": 1513803252,
"exp": 1513839252,
"at_hash": "Icyn6G6yHShle5exiHzNTA",
"nonce": "f1j~OfYH8sYRwjDc08Zxz9lbUA5UZBCx"
}