Skip to content

Instantly share code, notes, and snippets.

@rkmax
Forked from leaysgur/1-main.js
Created September 4, 2017 09:38
Show Gist options
  • Save rkmax/db87653d067c52e1f481759d96d17fde to your computer and use it in GitHub Desktop.
Save rkmax/db87653d067c52e1f481759d96d17fde 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment