Commit 60e3b4f8 by GeekTantra

Added success callback

parent 73ba7c6a
Showing with 19 additions and 17 deletions
...@@ -23,6 +23,10 @@ var passport = require('passport') ...@@ -23,6 +23,10 @@ var passport = require('passport')
function Strategy(options, verify) { function Strategy(options, verify) {
passport.Strategy.call(this); passport.Strategy.call(this);
this._host = options.host || '';
this._port = options.port || 993;
this._tls = options.tls || true;
this._success_callback = options.success_callback || null;
this.name = 'imap'; this.name = 'imap';
} }
...@@ -41,32 +45,30 @@ Strategy.prototype.authenticate = function(req, options) { ...@@ -41,32 +45,30 @@ Strategy.prototype.authenticate = function(req, options) {
options = options || {}; options = options || {};
this._username = req.body.username; this._username = req.body.username;
this._password = req.body.password; this._password = req.body.password;
this._host = options.hostName || 'imap.gmail.com';
this._port = options.port || 993;
this._tls = options.tls || true;
this._success_callback = options.success_callback || null;
if (!this._username || !this._password) { if (!this._username || !this._password) {
return this.fail(new BadRequestError(options.badRequestMessage || 'Missing credentials')); return this.fail(new BadRequestError(options.badRequestMessage || 'Missing credentials'));
} }
var self = this; var self = this;
var imap = new Imap({ var imap = new Imap({
user: this._username, user: self._username,
password: this._password, password: self._password,
host: this._host, host: self._host,
port: this._port, port: self._port,
tls: this._tls, tls: self._tls,
tlsOptions: { rejectUnauthorized: false } tlsOptions: { rejectUnauthorized: false }
}); });
imap.once('ready', function(){ imap.once('ready', function(){
var microtime = process.hrtime(); if (typeof self._success_callback == "function") {
var user = { var user = self._success_callback(this, imap);
email: this._username, if (!user.id) {
id: (microtime[0] * 1000 + microtime[1] / 1000)*1000 console.log("Success fallback must return an object with id parameter!");
}; return self.fail("Success fallback must return an object with id parameter!");
if (typeof this._success_callback == "function") { }
user = this._success_callback(); self.success(user, "success");
} else {
console.log("Success fallback is required to generate the user!");
return self.fail("Success fallback is required to generate the user!");
} }
self.success(user, "success");
}); });
imap.connect(); imap.connect();
imap.once('error', function(err) { imap.once('error', function(err) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment