Skip to content

Instantly share code, notes, and snippets.

@wannymiarelli

wannymiarelli/app.js

Last active Dec 21, 2017
Embed
What would you like to do?
Auth0 ExpressJS
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