1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var database_manager_1 = require("@accounts/database-manager");
|
5 | var graphql_api_1 = require("@accounts/graphql-api");
|
6 | exports.AccountsModule = graphql_api_1.AccountsModule;
|
7 | exports.authenticated = graphql_api_1.authenticated;
|
8 | var server_1 = require("@accounts/server");
|
9 | var apollo_server_1 = require("apollo-server");
|
10 | var jsonwebtoken_1 = require("jsonwebtoken");
|
11 | var lodash_1 = require("lodash");
|
12 | var defaultAccountsBoostOptions = {
|
13 | storage: {
|
14 | uri: 'mongodb://localhost:27017',
|
15 | name: 'accounts-js',
|
16 | },
|
17 | };
|
18 | var requirePackage = function (packageName) {
|
19 | if (require.resolve(packageName)) {
|
20 | return require(packageName);
|
21 | }
|
22 | };
|
23 | exports.accountsBoost = function (userOptions) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
24 | var options, databasePackages, storage, servicePackages, services;
|
25 | var _a, _b;
|
26 | return tslib_1.__generator(this, function (_c) {
|
27 | switch (_c.label) {
|
28 | case 0:
|
29 | options = lodash_1.merge({}, defaultAccountsBoostOptions, userOptions);
|
30 | databasePackages = (_a = {},
|
31 | _a['@accounts/mongo'] = function (requiredPackage) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
32 | var mongodb, mongoClient;
|
33 | return tslib_1.__generator(this, function (_a) {
|
34 | switch (_a.label) {
|
35 | case 0:
|
36 | mongodb = require('mongodb');
|
37 | return [4 , mongodb.MongoClient.connect(lodash_1.get(options, 'storage.uri'))];
|
38 | case 1:
|
39 | mongoClient = (_a.sent()).db(lodash_1.get(options, 'storage.name'));
|
40 | return [2 , new requiredPackage.Mongo(mongoClient, options)];
|
41 | }
|
42 | });
|
43 | }); },
|
44 | _a);
|
45 | return [4 , Object.keys(databasePackages).reduce(function (res, packageName) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
46 | var requiredPackage;
|
47 | return tslib_1.__generator(this, function (_a) {
|
48 | requiredPackage = requirePackage(packageName);
|
49 | if (requiredPackage) {
|
50 | return [2 , databasePackages[packageName](requiredPackage)];
|
51 | }
|
52 | return [2 , res];
|
53 | });
|
54 | }); }, Promise.resolve([]))];
|
55 | case 1:
|
56 | storage = _c.sent();
|
57 | if (!storage) {
|
58 | throw new Error('A database package could not be loaded. Did you install one?');
|
59 | }
|
60 | options.db = new database_manager_1.DatabaseManager({
|
61 | userStorage: storage,
|
62 | sessionStorage: storage,
|
63 | });
|
64 | servicePackages = (_b = {},
|
65 | _b['@accounts/password'] = function (requiredPackage) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
66 | var AccountsPassword;
|
67 | return tslib_1.__generator(this, function (_a) {
|
68 | AccountsPassword = requiredPackage.default;
|
69 | return [2 , new AccountsPassword(lodash_1.get(options, ['services', 'password']))];
|
70 | });
|
71 | }); },
|
72 | _b);
|
73 | return [4 , Object.keys(servicePackages).reduce(function (res, packageName) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
74 | var requiredPackage, service;
|
75 | var _a;
|
76 | return tslib_1.__generator(this, function (_b) {
|
77 | switch (_b.label) {
|
78 | case 0:
|
79 | requiredPackage = requirePackage(packageName);
|
80 | if (!requiredPackage) return [3 , 2];
|
81 | return [4 , servicePackages[packageName](requiredPackage)];
|
82 | case 1:
|
83 | service = _b.sent();
|
84 | return [2 , tslib_1.__assign(tslib_1.__assign({}, res), (_a = {}, _a[service.serviceName] = service, _a))];
|
85 | case 2: return [2 , res];
|
86 | }
|
87 | });
|
88 | }); }, Promise.resolve({}))];
|
89 | case 2:
|
90 | services = _c.sent();
|
91 |
|
92 | return [2 , new AccountsBoost(options, services)];
|
93 | }
|
94 | });
|
95 | }); };
|
96 | var defaultAccountsBoostListenOptions = {
|
97 | port: 4003,
|
98 | };
|
99 | var AccountsBoost = (function () {
|
100 | function AccountsBoost(options, services) {
|
101 | this.accountsServer = new server_1.AccountsServer(options, services);
|
102 | this.options = options;
|
103 | this.accountsGraphQL = graphql_api_1.AccountsModule.forRoot({
|
104 | accountsServer: this.accountsServer,
|
105 | });
|
106 | var _a = this.accountsGraphQL, schema = _a.schema, context = _a.context;
|
107 | this.apolloServer = new apollo_server_1.ApolloServer({
|
108 | schema: schema,
|
109 | context: context,
|
110 | });
|
111 | }
|
112 | AccountsBoost.prototype.listen = function (options) {
|
113 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
114 | var res;
|
115 | return tslib_1.__generator(this, function (_a) {
|
116 | switch (_a.label) {
|
117 | case 0: return [4 , this.apolloServer.listen(lodash_1.merge({}, defaultAccountsBoostListenOptions, options))];
|
118 | case 1:
|
119 | res = _a.sent();
|
120 | console.log("Accounts GraphQL server running at " + res.url);
|
121 | return [2 , res];
|
122 | }
|
123 | });
|
124 | });
|
125 | };
|
126 | AccountsBoost.prototype.graphql = function () {
|
127 | var _this = this;
|
128 |
|
129 | if (this.accountsGraphQL) {
|
130 | return this.accountsGraphQL;
|
131 | }
|
132 | if (this.options.micro) {
|
133 | this.accountsServer.resumeSession = function (accessToken) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
134 | var decoded, secretOrPublicKey;
|
135 | return tslib_1.__generator(this, function (_a) {
|
136 | if (!lodash_1.isString(accessToken)) {
|
137 | throw new Error('An access token is required');
|
138 | }
|
139 | try {
|
140 | secretOrPublicKey = typeof this.options.tokenSecret === 'string'
|
141 | ? this.options.tokenSecret
|
142 | : this.options.tokenSecret.publicKey;
|
143 | decoded = jsonwebtoken_1.verify(accessToken, secretOrPublicKey);
|
144 | }
|
145 | catch (err) {
|
146 | throw new Error('Access token is not valid');
|
147 | }
|
148 | return [2 , {
|
149 | id: decoded.data.userId,
|
150 | }];
|
151 | });
|
152 | }); };
|
153 | }
|
154 | return this.accountsGraphQL;
|
155 | };
|
156 | return AccountsBoost;
|
157 | }());
|
158 | exports.AccountsBoost = AccountsBoost;
|
159 | exports.default = exports.accountsBoost;
|
160 |
|
\ | No newline at end of file |