Skip to content

Instantly share code, notes, and snippets.

@iamonuwa
Created March 15, 2018 08:40
Show Gist options
  • Save iamonuwa/3115b4160524f3483018caa6d9390226 to your computer and use it in GitHub Desktop.
Save iamonuwa/3115b4160524f3483018caa6d9390226 to your computer and use it in GitHub Desktop.
var multer = require('multer'),
fs = require('fs'),
path = require('path'),
axios = require('axios'),
nodemailer = require('nodemailer'),
etherVerify = require('./config/utils'),
onfido = require('onfido'),
defaultClient = onfido.ApiClient.instance,
cloudinary = require('cloudinary'),
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads/')
},
filename: function (req, file, callback) {
callback(null, Date.now()+file.originalname);
}
});
var upload = multer({ storage: storage });
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
})
var Token = defaultClient.authentications['Token'];
Token.apiKey = 'token=' + process.env.ONFIDO;
Token.apiKeyPrefix = 'Token';
var api = new onfido.DefaultApi();
module.exports = function (app) {
app.post('/upload/document', upload.single('file'), function (req, res, next) {;
var applicant_id = req.body.applicant_id;
var type = 'passport';
var opts = {
side: 'front',
file: req.file.path
};
var callback = function (error, data, response) {
if (error) {
console.error(error.response.body);
} else {
console.log('API called successfully. Returned data: '+ data);
}
};
api.uploadDocument(applicant_id, type, opts, callback);
// api.uploadDocument(applicant_id, type, opts, function (err, data, response) {
// console.log('Err', err);
// console.log('Data', data);
// console.log('Response', response);
// })
// axios.post(`https://api.onfido.com/v2/applicants/${applicant_id}/documents`, type, opts, {
// headers: {'Authorization': 'Token token='+process.env.ONFIDO, 'content-type': 'multipart/form-data', 'accept': 'application/json'}
// }).then(function (response) {
// console.log(response);
// })
// .catch(function (err) {
// console.log(err.response.data.error.fields);
// })
});
app.post('/validate/id', upload.single('file'), function (req, res, next) {
var date = new Date('1970-01-01'); //or your date here
cloudinary.uploader.upload(req.file.path, function (result) {
if (result.url) {
var applicantDetail = {
file: result.secure_url,
applicant_id: req.body.applicant_id
};
axios.post('https://api.onfido.com/v2/live_photos', applicantDetail, {
headers: {
'Authorization': 'Token token='+process.env.ONFIDO
}
}).then(function (result) {
return res.status(200)
.json({
result
})
})
.catch(function (err) {
return res.status(err.response.status)
.json({
message: err.response.statusText
})
})
} else {
return res.status(400)
.json({
message: 'Error uploading photo'
})
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment