Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
}
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
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',
},
{
password: accountsPassword,
}
);
accountsServer.on(ServerHooks.ValidateLogin, ({ user }) => {
// This hook is called every time a user try to login.
// You can use it to only allow users with verified email to login.
// If you throw an error here it will be returned to the client.
});
app.use(accountsExpress(accountsServer));
});
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,
}
);
// Creates resolvers, type definitions, and schema directives used by accounts-js
const accountsGraphQL = AccountsModule.forRoot({
accountsServer,
});
const typeDefs = gql`
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',
text: (user: User, url: string) => convertUrlToToken(url),
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',
text: (user: User, url: string) => convertUrlToToken(url),
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);
}
},
},
{ password }
);
const accounts = AccountsModule.forRoot({
accountsServer,
headerName: 'x-prime-token',
constructor(options: AccountsBoostOptions, services: { [key: string]: AuthenticationService }) {
this.accountsServer = new AccountsServer(options, services);
this.options = options;
this.accountsGraphQL = AccountsModule.forRoot({
accountsServer: this.accountsServer,
});
const { schema, context } = this.accountsGraphQL;
this.apolloServer = new ApolloServer({
schema,
context,
});
}