Last active
December 21, 2017 13:50
-
-
Save wannymiarelli/d9cbc27d4ff732c0f03cd5f27806ac19 to your computer and use it in GitHub Desktop.
Auth0 ExpressJS
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 express = require('express'); | |
const bodyParser = require('body-parser'); | |
const axios = require('axios'); | |
const app = express(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
/* | |
Login endpoint | |
API reference: https://auth0.com/docs/api/authentication?http#resource-owner-password | |
*/ | |
router.post('/login', (req, res, next) => { | |
axios.post('https://yourapp.eu.auth0.com/oauth/token', { | |
grant_type: 'password', | |
username: req.body.username, | |
password: req.body.password, | |
audience: 'https://yourapp.eu.auth0.com/api/v2/', | |
client_id: 'xx', | |
client_secret: 'xx', | |
json: true | |
}).then((response) => { | |
// access_token! | |
res.json(response); | |
}).catch(error => next(error)); | |
}); | |
/* | |
Login endpoint | |
API reference: https://auth0.com/docs/api/authentication?http#signup | |
If the call is successful you get the access_token :) | |
*/ | |
router.post('/signup', (req, res, next) => { | |
axios.post('https://reliabitaly.eu.auth0.com/dbconnections/signup', { | |
client_id: 'xx', | |
email: req.body.email, | |
password: req.body.password, | |
connection: 'Username-Password-Authentication' | |
}).then((response) => { | |
// here we are! user profile data | |
}).catch(error => next(error)); | |
}); | |
// start the app on port 8080 | |
app.listen(process.env.PORT || 8080, () => { | |
console.log('App is running at http://localhost:8080'); | |
console.log(' Press CTRL-C to stop\n'); | |
}); | |
export default app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment