Skip to content

Instantly share code, notes, and snippets.

@tinganho
Created March 11, 2013 08:39
Show Gist options
  • Save tinganho/5132834 to your computer and use it in GitHub Desktop.
Save tinganho/5132834 to your computer and use it in GitHub Desktop.
var requirejs = require( 'requirejs' ),
querystring = require( 'querystring' ),
http = require( 'http' ),
globals = requirejs( 'profiles/p1/lib/constants/globals' );
var routes = function( app ) {
var options = {
hostname : globals.ACCOUNTS + '.' + globals.SERVER_DOMAIN,
port : globals.ACCOUNTS_PORT,
path : '/' + globals.VERSION + '/' + globals.OAUTH + '/',
method : 'POST',
headers : {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : globals.AUTHORIZATION,
'Connection' : 'keep-alive',
'Cache-Control' : 'no-cache',
'Accept' : '*/*',
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding' : 'gzip,deflate,sdch'
}
};
app.get( '/authenticate', function( req, res ) {
return res.redirect( '/login' );
});
app.post( '/authenticate', function( req, res ) {
var post_data = querystring.stringify({
'grant_type' : 'password',
'username' : req.body.username,
'password' : req.body.password
});
options.headers[ 'content-length' ] = post_data.length;
var post_req = http.request( options, function( auth_res ) {
// Setting encoding to get the response as a string
auth_res.setEncoding( 'utf8' );
auth_res.on( 'data', function( chunck ) {
var response = JSON.parse( chunck );
res.cookie( 'access_token', response.access_token, {
maxAge : response.expires_in,
httpOnly: true
});
// TODO : Replace with /home/
res.redirect( 'http://www.p1.com/home/' );
});
});
post_req.on( 'error', function( e ) {
console.log( e );
});
post_req.write( post_data );
post_req.end();
});
};
module.exports = routes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment