I'm creating a nodeJS proxy that has two paths oauth and basic auth to communicate with github and get some simple information back (mainly just a successful request). I've gotten a token and everything is dandy there now but I'm working on a basic auth now and I'm a bit lost
Heres a pop of the code to see so far if it helps
Code:
var express = require('express');
var app = express();
//Server-api config
var port = process.env.PORT || 8080;
var router = express.Router();
//standard log all methods
var http = require('http'),
https = require ('https');
var auth = express.basicAuth('Maxoriley618', 'Gunner131');
router.use(function(req,res,next){
console.log(req.method + ' ' + req.url);
next();
});
function testGitHub(userVar) {
var GitHubApi = require("github");
var github = new GitHubApi({
// required
version: "3.0.0"
});
github.authenticate({
type: "basic",
username: "Maxoriley618",
password: "Gunner131"
});
var token = "db1e8c41ac1214f813ac00d02c3ca28e49b9a2f8";
github.authenticate({
type: "oauth",
token: token
});
github.user({ user: userVar} , function(err, res) {
console.log("GOT ERR?", err);
console.log("GOT RES?", res);
github.repos.getAll({}, function(err, res) {
console.log("GOT ERR?", err);
console.log("GOT RES?", res);
});
});
}
//ROUTES
//BASIC rout for basic auth
router.route('/basic')
.get(function(req,res) {
if(req.headers['user'] == null)
{
res.status(405).json({message: 'a user is required!'});
}
else res.json({message: 'Github users login: ' + JSON.stringify(req.headers['user'])});
})
//reject unsupported HTTP methods
.all(function(req, res) {
res.status(405).json({message: 'Does not support this HTTP method'});
});
router.route('/oauth')
.get(
function(req,res) {
if(req.headers['user'] == null)
{
res.status(405).json({message: 'a user is required!'});
}
else res.json({message: 'Github users login: ' + JSON.stringify(req.headers['user'])});
})
//reject unsupported HTTP methods
.all(function(req, res) {
res.status(405).json({message: 'Does not support this HTTP method'});
});
app.use('/api', router);
//BLOCK ANY OTHER REQUESTS
//==================================================================================================================
//catch all other requests and reject
app.all('*', function(req, res) {
res.status(404).send('Invalid/Rejected URN', req.query);
});
//START THE SERVER
//==================================================================================================================
app.listen(port);
console.log('Started. Listening on port:' + port);