Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
console.error('createApolloAccounts: db is a required parameter')
}
if (!givenOptions.tokenSecret) {
console.log(
'Warning: Must provide a tokenSecret (long random string) to createApolloAccounts()'
)
}
const mongoStorage = new MongoDBInterface(db, {
convertUserIdToMongoObjectId: false,
convertSessionIdToMongoObjectId: false,
idProvider,
dateProvider
})
const dbManager = new DatabaseManager({
sessionStorage: mongoStorage,
userStorage: mongoStorage
})
const options = defaultsDeep(givenOptions, defaultOptions)
const accountsServer = new AccountsServer(
{ db: dbManager, ...options },
{
password: new AccountsPassword()
}
)
// full list of hooks:
// https://github.com/accounts-js/accounts/blob/master/packages/server/src/utils/server-hooks.ts
accountsServer.on('LoginSuccess', callOnLoginHooks)
const start = async () => {
// Create database connection
await mongoose.connect('mongodb://localhost:27017/accounts-js-graphql-example', {
useNewUrlParser: true,
});
const mongoConn = mongoose.connection;
// Build a storage for storing users
const userStorage = new MongoDBInterface(mongoConn);
// Create database manager (create user, find users, sessions etc) for accounts-js
const accountsDb = new DatabaseManager({
sessionStorage: userStorage,
userStorage,
});
const accountsPassword = new AccountsPassword({
// This option is called when a new user create an account
// Inside we can apply our logic to validate the user fields
validateNewUser: user => {
// For example we can allow only some kind of emails
if (user.email.endsWith('.xyz')) {
throw new Error('Invalid email');
}
return user;
},
});
const storage = await Object.keys(databasePackages).reduce(
async (res, packageName: string): Promise => {
const requiredPackage = requirePackage(packageName);
if (requiredPackage) {
return (databasePackages as any)[packageName](requiredPackage);
}
return res;
},
Promise.resolve([])
);
if (!storage) {
throw new Error('A database package could not be loaded. Did you install one?');
}
options.db = new DatabaseManager({
userStorage: storage,
sessionStorage: storage,
});
const servicePackages = {
['@accounts/password']: async (requiredPackage: any): Promise => {
const AccountsPassword = requiredPackage.default;
return new AccountsPassword(get(options, ['services', 'password']));
},
};
const services = await Object.keys(servicePackages).reduce(
async (res, packageName: string): Promise => {
const requiredPackage = requirePackage(packageName);
if (requiredPackage) {
const service = await (servicePackages as any)[packageName](requiredPackage);