UNPKG

apollo-passport-local

Version:

Local strategy using email address and hashed, bcrypted password

138 lines (107 loc) 5.51 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.hashPassword = undefined; var _templateObject = _taggedTemplateLiteral(['\n mutation login (\n $email: String!\n $password: String!\n ) {\n apCreateUserEmailPassword (\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 apCreateUserEmailPassword (\n email: $email\n password: $password\n ) {\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 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), setUserPassword: (0, _graphqlTag2.default)(_templateObject3) }; var extensionMethods = { createUserEmailPassword: function createUserEmailPassword(email, password) { var result; return regeneratorRuntime.async(function createUserEmailPassword$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: this.loginStart(); _context.next = 3; return regeneratorRuntime.awrap(this.apolloClient.mutate({ mutation: mutation.createUserEmailPassword, variables: { email: email, password: hashPassword(password) } })); case 3: result = _context.sent; this.loginComplete(result, 'apCreateUserEmailPassword'); case 5: case 'end': return _context.stop(); } } }, null, this); }, loginWithEmailPassword: function loginWithEmailPassword(email, password) { var result; return regeneratorRuntime.async(function loginWithEmailPassword$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: this.loginStart(); _context2.next = 3; return regeneratorRuntime.awrap(this.apolloClient.mutate({ mutation: mutation.loginWithEmailPassword, variables: { email: email, password: hashPassword(password) } })); case 3: result = _context2.sent; this.loginComplete(result, 'apLoginEmailPassword'); case 5: case 'end': return _context2.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$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return regeneratorRuntime.awrap(this.apolloClient.mutate({ mutation: mutation.setUserPassword, variables: { userId: userId, oldPassword: hashPassword(oldPassword), newPassword: hashPassword(newPassword) } })); case 2: return _context3.abrupt('return', _context3.sent); case 3: case 'end': return _context3.stop(); } } }, null, this); } }; var LocalStrategy = function LocalStrategy(apolloPassport) { _classCallCheck(this, LocalStrategy); this.ap = apolloPassport; apolloPassport.extendWith(extensionMethods); }; exports.hashPassword = hashPassword; exports.default = LocalStrategy;