apollo-passport-local-strategy
Version:
Local strategy using email address and hashed, bcrypted password
225 lines (187 loc) • 9.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hashPassword = undefined;
var _templateObject = _taggedTemplateLiteral(['\n mutation createUserEmailPassword (\n $input : apUserInput!\n ) {\n apCreateUserEmailPassword(input: $input){\n error\n token\n }\n }\n '], ['\n mutation createUserEmailPassword (\n $input : apUserInput!\n ) {\n apCreateUserEmailPassword(input: $input){\n error\n token\n }\n }\n ']),
_templateObject2 = _taggedTemplateLiteral(['\n mutation login (\n $email: String!\n $password: String!\n ) {\n apLoginEmailPassword (\n email: $email\n password: $password\n ) {\n error\n token\n }\n }\n '], ['\n mutation login (\n $email: String!\n $password: String!\n ) {\n apLoginEmailPassword (\n email: $email\n password: $password\n ) {\n error\n token\n }\n }\n ']),
_templateObject3 = _taggedTemplateLiteral(['\n mutation verifyAccount ($userId : String!, $verificationToken: String!){\n apVerifyAccount (userId: $userId, verificationToken: $verificationToken){\n errCode\n errMessage\n }\n }\n '], ['\n mutation verifyAccount ($userId : String!, $verificationToken: String!){\n apVerifyAccount (userId: $userId, verificationToken: $verificationToken){\n errCode\n errMessage\n }\n }\n ']),
_templateObject4 = _taggedTemplateLiteral(['\n mutation recoverPasswordRequest (\n $email: String!\n ) {\n apRecoverPasswordRequest (\n email: $email\n )\n }\n '], ['\n mutation recoverPasswordRequest (\n $email: String!\n ) {\n apRecoverPasswordRequest (\n email: $email\n )\n }\n ']),
_templateObject5 = _taggedTemplateLiteral(['\n mutation recoverPassword (\n $userId: String!\n $token: String!\n $newPassword: String!\n ) {\n apRecoverPassword (\n userId: $userId\n token: $token\n newPassword: $newPassword\n )\n }\n '], ['\n mutation recoverPassword (\n $userId: String!\n $token: String!\n $newPassword: String!\n ) {\n apRecoverPassword (\n userId: $userId\n token: $token\n newPassword: $newPassword\n )\n }\n ']),
_templateObject6 = _taggedTemplateLiteral(['\n mutation login (\n $userId: String!\n $oldPassword: String!\n $newPassword: String!\n ) {\n apUpdateUserPassword (\n userId: $userId\n oldPassword: $oldPassword\n newPassword: $newPassword\n )\n }\n '], ['\n mutation login (\n $userId: String!\n $oldPassword: String!\n $newPassword: String!\n ) {\n apUpdateUserPassword (\n userId: $userId\n oldPassword: $oldPassword\n newPassword: $newPassword\n )\n }\n ']);
var _graphqlTag = require('graphql-tag');
var _graphqlTag2 = _interopRequireDefault(_graphqlTag);
var _sha = require('sha.js');
var _sha2 = _interopRequireDefault(_sha);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
function hashPassword(plaintext) {
return (0, _sha2.default)('sha256').update(plaintext).digest('hex');
}
var mutation = {
createUserEmailPassword: (0, _graphqlTag2.default)(_templateObject),
loginWithEmailPassword: (0, _graphqlTag2.default)(_templateObject2),
verifyAccount: (0, _graphqlTag2.default)(_templateObject3),
recoverPasswordRequest: (0, _graphqlTag2.default)(_templateObject4),
recoverPassword: (0, _graphqlTag2.default)(_templateObject5),
setUserPassword: (0, _graphqlTag2.default)(_templateObject6)
};
var extensionMethods = {
createUserEmailPassword: function createUserEmailPassword(userInput) {
var result;
return regeneratorRuntime.async(function createUserEmailPassword$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
// this.loginStart();
if (userInput.password) {
userInput.password = hashPassword(userInput.password);
}
_context.next = 3;
return regeneratorRuntime.awrap(this.apolloClient.mutate({
mutation: mutation.createUserEmailPassword,
variables: {
input: userInput
}
}));
case 3:
result = _context.sent;
return _context.abrupt('return', result);
case 5:
case 'end':
return _context.stop();
}
}
}, null, this);
},
verifyAccount: function verifyAccount(userId, verificationToken) {
var result;
return regeneratorRuntime.async(function verifyAccount$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return regeneratorRuntime.awrap(this.apolloClient.mutate({
mutation: mutation.verifyAccount,
variables: {
userId: userId,
verificationToken: verificationToken
}
}));
case 2:
result = _context2.sent;
return _context2.abrupt('return', result);
case 4:
case 'end':
return _context2.stop();
}
}
}, null, this);
},
recoverPasswordRequest: function recoverPasswordRequest(email) {
var result;
return regeneratorRuntime.async(function recoverPasswordRequest$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return regeneratorRuntime.awrap(this.apolloClient.mutate({
mutation: mutation.recoverPasswordRequest,
variables: {
email: email
}
}));
case 2:
result = _context3.sent;
return _context3.abrupt('return', result);
case 4:
case 'end':
return _context3.stop();
}
}
}, null, this);
},
recoverPassword: function recoverPassword(userId, token, newPassword) {
var result;
return regeneratorRuntime.async(function recoverPassword$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return regeneratorRuntime.awrap(this.apolloClient.mutate({
mutation: mutation.recoverPassword,
variables: {
userId: userId,
token: token,
newPassword: hashPassword(newPassword)
}
}));
case 2:
result = _context4.sent;
return _context4.abrupt('return', result);
case 4:
case 'end':
return _context4.stop();
}
}
}, null, this);
},
loginWithEmailPassword: function loginWithEmailPassword(email, password) {
var result;
return regeneratorRuntime.async(function loginWithEmailPassword$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
this.loginStart();
_context5.next = 3;
return regeneratorRuntime.awrap(this.apolloClient.mutate({
mutation: mutation.loginWithEmailPassword,
variables: {
email: email,
password: hashPassword(password)
}
}));
case 3:
result = _context5.sent;
this.loginComplete(result, 'apLoginEmailPassword');
case 5:
case 'end':
return _context5.stop();
}
}
}, null, this);
},
// what status updates should this get?
// that logic could also be used for re-requesting additional permissions on services
updateUserPassword: function updateUserPassword(userId, oldPassword, newPassword) {
return regeneratorRuntime.async(function updateUserPassword$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return regeneratorRuntime.awrap(this.apolloClient.mutate({
mutation: mutation.setUserPassword,
variables: {
userId: userId,
oldPassword: hashPassword(oldPassword),
newPassword: hashPassword(newPassword)
}
}));
case 2:
return _context6.abrupt('return', _context6.sent);
case 3:
case 'end':
return _context6.stop();
}
}
}, null, this);
}
};
var LocalStrategy = function LocalStrategy(apolloPassport) {
_classCallCheck(this, LocalStrategy);
this.ap = apolloPassport;
apolloPassport.extendWith(extensionMethods);
};
exports.hashPassword = hashPassword;
exports.default = LocalStrategy;