-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathaccountServer.js
61 lines (55 loc) · 1.79 KB
/
accountServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// eslint-disable-next-line node/no-extraneous-import
import mongoConnectWithRetry from "@reactioncommerce/api-core/src/util/mongoConnectWithRetry.js";
import { Mongo } from "@accounts/mongo";
import { AccountsServer } from "@accounts/server";
import { AccountsPassword } from "@accounts/password";
import mongoose from "mongoose";
import { AccountsModule } from "@accounts/graphql-api";
import config from "../config.js";
let accountsServer;
let accountsGraphQL;
export default async (app) => {
if (accountsServer && accountsGraphQL) {
return { accountsServer, accountsGraphQL };
}
const { MONGO_URL, STORE_URL, TOKEN_SECRET } = config;
const { context } = app;
const client = await mongoConnectWithRetry(MONGO_URL);
const db = client.db();
const accountsMongo = new Mongo(db, {
convertUserIdToMongoObjectId: false,
convertSessionIdToMongoObjectId: false,
idProvider: () => mongoose.Types.ObjectId().toString()
});
const password = new AccountsPassword();
accountsServer = new AccountsServer(
{
siteUrl: STORE_URL,
tokenSecret: TOKEN_SECRET,
db: accountsMongo,
enableAutologin: true,
ambiguousErrorMessages: false,
sendMail: async ({ to, text }) => {
const query = text.split("/");
const token = query[query.length - 1];
const url = `${STORE_URL}/?resetToken=${token}`;
await context.mutations.sendResetAccountPasswordEmail(context, {
email: to,
url
});
},
emailTemplates: {
resetPassword: {
from: null,
// hack to pass the URL to sendMail function
text: (user, url) => url
}
}
},
{
password
}
);
accountsGraphQL = AccountsModule.forRoot({ accountsServer });
return { accountsServer, accountsGraphQL };
};