Last active
June 12, 2018 15:37
-
-
Save leaysgur/c52d8d7c50f2fc8d525d0cb727f21409 to your computer and use it in GitHub Desktop.
passwordless auth0 with amazon cognito
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
// const auth0 = require('auth0-js'); | |
const webAuth = new auth0.WebAuth({ | |
domain: '<YOUR_DOMAIN>', | |
clientID: '<YOUR_CLIENT_ID>', | |
}); |
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
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 | |
}); |
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
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); | |
}); |
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
// 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
use
auth0@v8