Skip to content

Instantly share code, notes, and snippets.

@siddhant1
Created March 19, 2019 19:23
Show Gist options
  • Save siddhant1/8534fc7c88dfbf40c83e97542025c6e3 to your computer and use it in GitHub Desktop.
Save siddhant1/8534fc7c88dfbf40c83e97542025c6e3 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
var requestClient = require('request');
app.route('/').get((request, response) => {
var token = request.get('Authorization');
if (!token) {
response.json({'x-hasura-role': 'anonymous'});
return;
} else {
// Fetch information about this user from
// auth0 to validate this token
// NOTE: Replace the URL with your own auth0 app url
var options = {
url: `https://${your_auth0_url_here}/userinfo`,
headers: {
Authorization: token,
'Content-Type': 'application/json'
}
};
requestClient(options, (err, res, body) => {
if (!err && res.statusCode == 200) {
var userInfo = JSON.parse(body);
console.log(userInfo); //debug
var hasuraVariables = {
'X-Hasura-User-Id': userInfo.sub,
'X-Hasura-Role': 'user'
};
console.log(hasuraVariables); // For debug
response.json(hasuraVariables);
} else {
// Error response from auth0
console.log(err, res, body);
response.json({'x-hasura-role': 'anonymous'});
return;
}
});
}
});
app.listen(3000,()=>{console.log('running')});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment