-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move functions from init.js to init/
- Loading branch information
Showing
3 changed files
with
58 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
/** | ||
* @file Define initial handlers, | ||
* | ||
* Trade grant_tokens for access_tokens. | ||
* | ||
*/ | ||
var restify = require('restify'); | ||
|
||
var TempToken = require('../../models/temp-token.js'); | ||
|
||
/** | ||
* This handler starts providing a new account onto AnyFetch. | ||
* We check the request is valid, create a new tempToken and call the handler for new account. | ||
* The tempToken is then saved. | ||
* | ||
* @param {Object} req Request object from the client | ||
* @param {Object} res Response we want to return | ||
* @param {Function} next Callback to call once res has been populated. | ||
*/ | ||
module.exports = function(redirectToService, config) { | ||
return function (req, res, next) { | ||
if(!req.params.code) { | ||
return next(new restify.MissingParameterError("Missing code parameter.")); | ||
} | ||
|
||
// Hold temporary state | ||
var tempToken = new TempToken({ | ||
anyfetchCode: req.params.code, | ||
returnTo: (req.params.returnTo) ? req.params.returnTo : "https://manager.anyfetch.com" | ||
}); | ||
|
||
redirectToService(config.providerUrl + '/init/callback?code=' + req.params.code, function(err, redirectUrl, preData) { | ||
if(err) { | ||
return next(err); | ||
} | ||
|
||
tempToken.data = preData; | ||
tempToken.save(function(err) { | ||
if(err) { | ||
return next(err); | ||
} | ||
|
||
res.send(302, null, { | ||
Location: redirectUrl | ||
}); | ||
next(); | ||
}); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters