UNPKG

5.46 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.useTour = undefined;
7
8var useTour = exports.useTour = function () {
9 var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(app, config) {
10 var db, localAuthenticateAndRedirect, returnTo, routePath, getFormatUser, adminUser, tours, users, adminTour;
11 return regeneratorRuntime.wrap(function _callee2$(_context2) {
12 while (1) {
13 switch (_context2.prev = _context2.next) {
14 case 0:
15 // unpack
16 db = config.db, localAuthenticateAndRedirect = config.localAuthenticateAndRedirect, returnTo = config.returnTo, routePath = config.routePath;
17 getFormatUser = config.getFormatUser || _format.getFormatUser;
18 adminUser = config.adminUser;
19 tours = void 0;
20 users = config.users;
21 // use
22
23 app.use(_bodyParser2.default.json()); // to support JSON-encoded bodies
24 // check for the users
25
26 if (!(db && !users)) {
27 _context2.next = 16;
28 break;
29 }
30
31 _context2.next = 9;
32 return db.collection('tours').find().toArray();
33
34 case 9:
35 tours = _context2.sent;
36 _context2.next = 12;
37 return Promise.all(tours.map(function () {
38 var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(tour) {
39 var user;
40 return regeneratorRuntime.wrap(function _callee$(_context) {
41 while (1) {
42 switch (_context.prev = _context.next) {
43 case 0:
44 _context.t0 = getFormatUser;
45 _context.next = 3;
46 return db.collection('users').findOne({ id: tour.userId });
47
48 case 3:
49 _context.t1 = _context.sent;
50 user = (0, _context.t0)(_context.t1);
51
52 user.modeName = tour.modeName;
53 return _context.abrupt('return', user);
54
55 case 7:
56 case 'end':
57 return _context.stop();
58 }
59 }
60 }, _callee, this);
61 }));
62
63 return function (_x3) {
64 return _ref2.apply(this, arguments);
65 };
66 }()));
67
68 case 12:
69 users = _context2.sent;
70
71 // search for admin user
72 adminTour = tours.find(function (_ref3) {
73 var modeName = _ref3.modeName;
74 return modeName === 'admin';
75 });
76
77 adminUser = Object.assign({}, users.find(function (_ref4) {
78 var modeName = _ref4.modeName;
79 return modeName === 'admin';
80 }));
81 if (adminUser && adminTour) {
82 adminUser.password = adminTour.password;
83 }
84
85 case 16:
86 // automatic signin with a mock user
87 app.post(routePath, function (req, res, next) {
88 // determine which tour user to use given the password
89 var matchedUser = users.find(function (_ref5) {
90 var email = _ref5.email;
91 return email === req.body.email;
92 });
93 // if find then pass the password
94 if (matchedUser) {
95 var password = matchedUser.password;
96 if (!password && tours) {
97 var matchedTour = tours.find(function (_ref6) {
98 var userId = _ref6.userId;
99 return userId === matchedUser.id;
100 });
101 if (matchedTour) {
102 password = matchedTour.password;
103 }
104 }
105 if (password) {
106 req.body.password = password;
107 } else {
108 console.warn('transactions-express-passport tour: did not find such tour');
109 }
110 } else {
111 console.warn('transactions-express-passport tour: did not find such user ' + email);
112 }
113 // next
114 next();
115 }, returnTo, localAuthenticateAndRedirect);
116 // return
117 return _context2.abrupt('return', { adminUser: adminUser,
118 users: users
119 });
120
121 case 18:
122 case 'end':
123 return _context2.stop();
124 }
125 }
126 }, _callee2, this);
127 }));
128
129 return function useTour(_x, _x2) {
130 return _ref.apply(this, arguments);
131 };
132}();
133
134var _bodyParser = require('body-parser');
135
136var _bodyParser2 = _interopRequireDefault(_bodyParser);
137
138var _lodash = require('lodash.get');
139
140var _lodash2 = _interopRequireDefault(_lodash);
141
142var _format = require('./format');
143
144function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
145
146function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
\No newline at end of file