Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function main() {
const mongoClient = await MongoClient.connect(MONGO_URI, {
useNewUrlParser: true,
native_parser: true
});
const db = mongoClient.db();
// Create accounts server that holds a lower level of all accounts operations
const accountsServer = new AccountsServer(
{
db: new AccountsMongoDB(db),
tokenSecret: TOKEN_SECRET
},
{
password: new AccountsPassword(),
}
);
const { schema, context } = AppModule.forRoot({
accountsServer,
db
});
const apolloServer = new ApolloServer({
schema,
context,
introspection: true
});
const { url } = await apolloServer.listen(PORT);
console.log(`Server listening: ${url}`);
}
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;
},
});
// Create accounts server that holds a lower level of all accounts operations
const accountsServer = new AccountsServer(
{ db: accountsDb, tokenSecret: 'secret' },
{
password: accountsPassword,
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)
accountsServer.on('CreateUserSuccess', callOnCreateUserHooks)
return createAccountsGraphQL(accountsServer, { extend: true })
}
import * as cors from 'cors';
import * as mongoose from 'mongoose';
import { AccountsServer, ServerHooks } from '@accounts/server';
import { AccountsPassword } from '@accounts/password';
import accountsExpress, { userLoader } from '@accounts/rest-express';
import MongoDBInterface from '@accounts/mongo';
mongoose.connect('mongodb://localhost:27017/accounts-js-rest-example', { useNewUrlParser: true });
const db = mongoose.connection;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
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 accountsServer = new AccountsServer(
{
db: new MongoDBInterface(db),
tokenSecret: 'secret',
},
export const createAccounts = async () => {
const connection = await connect(process.env.DATABASE_URL);
// Like, fix this man!
const tokenSecret = 'process.env.ACCOUNTS_SECRET' || 'change this in .env';
const db = new AccountsTypeorm({ connection, cache: 1000 });
const password = new AccountsPassword();
const accountsServer = new AccountsServer(
{
db,
tokenSecret,
siteUrl: 'http://localhost:3000',
},
{ password }
);
// Creates resolvers, type definitions, and schema directives used by accounts-js
const accountsGraphQL = AccountsModule.forRoot({
accountsServer,
});
const typeDefs = `
type PrivateType @auth {
field: String
constructor() {
this.databaseTest = new DatabaseTest();
this.accountsDatabase = this.databaseTest.accountsDatabase;
this.accountsPassword = new AccountsPassword();
this.accountsServer = new AccountsServer(
{
db: this.accountsDatabase,
tokenSecret: 'test',
emailTemplates: {
from: 'accounts-js ',
verifyEmail: {
subject: () => 'Verify your account email',
text: (user: User, url: string) => convertUrlToToken(url),
},
resetPassword: {
subject: () => 'Reset your password',
text: (user: User, url: string) => convertUrlToToken(url),
},
enrollAccount: {
subject: () => 'Set your password',
constructor() {
this.databaseTest = new DatabaseTest();
this.accountsDatabase = this.databaseTest.accountsDatabase;
this.accountsPassword = new AccountsPassword();
this.accountsServer = new AccountsServer(
{
db: this.accountsDatabase,
tokenSecret: 'test',
emailTemplates: {
from: 'accounts-js ',
verifyEmail: {
subject: () => 'Verify your account email',
text: (user: User, url: string) => convertUrlToToken(url),
},
resetPassword: {
subject: () => 'Reset your password',
text: (user: User, url: string) => convertUrlToToken(url),
},
enrollAccount: {
subject: () => 'Set your password',
export const createAccounts = async (connection: Connection) => {
const tokenSecret = ACCOUNTS_SECRET || 'not very secret secret';
const db = new AccountsTypeorm({
connection,
cache: 1000,
});
const password = new AccountsPassword({
twoFactor: {
appName: 'Prime',
},
});
const accountsServer = new AccountsServer(
{
db,
tokenSecret,
siteUrl: CORE_URL,
sendMail(mail) {
if (mailgun) {
return mailgun.messages().send(mail);
}
},
},