Skip to content

Instantly share code, notes, and snippets.

@Spyna
Last active February 1, 2018 13:04
Show Gist options
  • Save Spyna/a2a435948a7443fd46d9e22334101e3b to your computer and use it in GitHub Desktop.
Save Spyna/a2a435948a7443fd46d9e22334101e3b to your computer and use it in GitHub Desktop.
var express = require( 'express' );
var bodyParser = require( 'body-parser' );
var googleAuth = require( './googleAuth.js' );
var app = express();
var jwt = require('./jwt.js')
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded( { extended: false } ) );
app.post( '/auth/token', ( req, res ) => {
try {
var login = req.body;
googleAuth.getGoogleUser( login.code )
.then( response => {
var content = {
token: jwt.createToken( response ),
user: response
}
return content
} )
.then( credentials => {
res.setHeader( 'Content-Type', 'application/json' );
res.end( JSON.stringify( credentials ) );
} )
.catch( e => {
console.log( e )
throw new Error( e )
} )
} catch ( error ) {
res.sendStatus( 500 ).end( JSON.stringify( { error: "Internal server error" } ) )
return console.error( error )
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment