Last active
January 21, 2021 23:14
-
-
Save thameera/16fd5d9307dfb248d6f83fcd19fd1947 to your computer and use it in GitHub Desktop.
Auth0 Node SDK Passwordless example
This file contains hidden or 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 AuthenticationClient = require('auth0').AuthenticationClient; | |
const prompts = require('prompts') | |
const auth0 = new AuthenticationClient({ | |
domain: 'EXAMPLE.auth0.com', | |
clientId: 'CLIENT_ID', | |
clientSecret: 'CLIENT_SECRET' | |
}) | |
const sendEmail = async() => { | |
const res = await prompts({ message: 'Enter the email:', type: 'text', name: 'email' }) | |
const data = { | |
email: res.email, | |
send: 'code' | |
} | |
const options = { | |
forwardedFor: '1.2.3.5', // Replace with customer IP | |
} | |
await auth0.passwordless.sendEmail(data, options) | |
return res.email | |
} | |
const login = async(email) => { | |
const res = await prompts({ message: 'Enter the code:', type: 'text', name: 'code' }) | |
const data = { | |
otp: res.code, | |
realm: 'email', | |
username: email | |
} | |
const options = { | |
forwardedFor: '1.2.3.6', // Replace with customer IP | |
} | |
const tokens = await auth0.passwordless.signIn(data, options) | |
return tokens | |
} | |
const run = async () => { | |
try { | |
const email = await sendEmail() | |
const tokens = await login(email) | |
console.log('ID token: ' + tokens.id_token) | |
console.log('Access token: ' + tokens.access_token) | |
} catch (e) { | |
console.log(e.message) | |
} | |
} | |
run() |
This file contains hidden or 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
{ | |
"name": "node_pwless", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"license": "MIT", | |
"dependencies": { | |
"auth0": "^2.32.0", | |
"prompts": "^2.4.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment