Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Last active June 12, 2018 15:37
Show Gist options
  • Save leaysgur/c52d8d7c50f2fc8d525d0cb727f21409 to your computer and use it in GitHub Desktop.
Save leaysgur/c52d8d7c50f2fc8d525d0cb727f21409 to your computer and use it in GitHub Desktop.
passwordless auth0 with amazon cognito
// const auth0 = require('auth0-js');
const webAuth = new auth0.WebAuth({
domain: '<YOUR_DOMAIN>',
clientID: '<YOUR_CLIENT_ID>',
});
const phoneNumber = '+819012345678';
webAuth.passwordlessStart({
connection: 'aws-sns',
send: 'code',
phoneNumber: phoneNumber,
}, (err, res) => {
if (err) {
console.error(err);
return;
}
// sms has sent to phoneNumber
});
const phoneNumber = '+819012345678';
const code = '123456';
webAuth.client.loginWithResourceOwner({
connection: 'aws-sns',
scope: 'openid',
username: phoneNumber,
password: code,
}, (err, res) => {
if (err) {
console.error(err);
return;
}
// pass this token as aws credentials
localStorage.setItem('idToken', res.idToken);
});
// const jwt_decode = require('jwt-decode');
function isTokenExpired(token) {
const decoded = jwt_decode(token);
if (!decoded.exp) {
return true;
}
const now = Math.floor(Date.now() / 1000);
if (now >= decoded.exp) {
return true;
}
return false;
}
const idToken = localStorage.getItem('idToken');
const isLogin = idToken !== null && isTokenExpired(idToken) === false;
@leaysgur
Copy link
Author

use auth0@v8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment