Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return {
passwordless: true,
email: user.email
};
} else {
throw new MoleculerClientError("Passwordless login is not allowed.", 400, "ERR_PASSWORDLESS_DISABLED");
}
// Check Two-factor authentication
if (user.totp && user.totp.enabled) {
if (!ctx.params.token)
throw new MoleculerClientError("Two-factor authentication is enabled. Please give the 2FA code.", 400, "ERR_MISSING_2FA_CODE");
if (!(await this.verify2FA(user.totp.secret, ctx.params.token)))
throw new MoleculerClientError("Invalid 2FA token!", 400, "TWOFACTOR_INVALID_TOKEN");
}
return {
token: await this.getToken(user)
};
}
},
const entity = {};
// Verify email
let found = await this.getUserByEmail(ctx, params.email);
if (found)
throw new MoleculerClientError("Email has already been registered.", 400, "ERR_EMAIL_EXISTS");
// Verify username
if (this.config["accounts.username.enabled"]) {
if (!ctx.params.username) {
throw new MoleculerClientError("Username can't be empty.", 400, "ERR_USERNAME_EMPTY");
}
let found = await this.getUserByUsername(ctx, params.username);
if (found)
throw new MoleculerClientError("Username has already been registered.", 400, "ERR_USERNAME_EXISTS");
entity.username = params.username;
}
// Set basic data
entity.email = params.email;
entity.firstName = params.firstName;
entity.lastName = params.lastName;
entity.roles = this.config["accounts.defaultRoles"];
entity.plan = this.config["accounts.defaultPlan"];
entity.avatar = params.avatar;
entity.socialLinks = {};
entity.createdAt = Date.now();
entity.verified = true;
entity.status = 1;
return bcrypt.compare(password, user.password).then(res => {
if (!res)
return Promise.reject(new MoleculerClientError("Wrong password!", 422, "", [{ field: "email", message: "is not found"}]));
// Transform user entity (remove password and all protected fields)
return this.transformDocuments(ctx, {}, user);
});
})
return async function CheckPermissionsMiddleware(ctx) {
const roles = ctx.meta.roles;
if (roles) {
let res = false;
if (permNames.length > 0) {
res = await ctx.call("v1.acl.hasAccess", { roles, permissions: permNames });
}
if (res !== true) {
if (permFuncs.length > 0) {
const results = await ctx.broker.Promise.all(permFuncs.map(async fn => fn.call(this, ctx)));
res = results.find(r => !!r);
}
if (res !== true)
throw new MoleculerClientError("You have no right for this operation!", 401, "ERR_HAS_NO_ACCESS", { action: action.name });
}
}
// Call the handler
return handler(ctx);
}.bind(this);
}
async handler(ctx) {
const token = this.generateToken();
const user = await this.getUserByEmail(ctx, ctx.params.email);
// Check email is exist
if (!user)
throw new MoleculerClientError("Email is not registered.", 400, "ERR_EMAIL_NOT_FOUND");
// Check verified
if (!user.verified)
throw new MoleculerClientError("Please activate your account!", 400, "ERR_ACCOUNT_NOT_VERIFIED");
// Check status
if (user.status !== 1)
throw new MoleculerClientError("Account is disabled!", 400, "ERR_ACCOUNT_DISABLED");
// Save the token to user
await this.adapter.updateById(user._id, { $set: {
resetToken: token,
resetTokenExpires: Date.now() + 3600 * 1000 // 1 hour
} });
// Send a passwordReset email
// Send magic link
await this.sendMagicLink(ctx, user);
return {
passwordless: true,
email: user.email
};
} else {
throw new MoleculerClientError("Passwordless login is not allowed.", 400, "ERR_PASSWORDLESS_DISABLED");
}
// Check Two-factor authentication
if (user.totp && user.totp.enabled) {
if (!ctx.params.token)
throw new MoleculerClientError("Two-factor authentication is enabled. Please give the 2FA code.", 400, "ERR_MISSING_2FA_CODE");
if (!(await this.verify2FA(user.totp.secret, ctx.params.token)))
throw new MoleculerClientError("Invalid 2FA token!", 400, "TWOFACTOR_INVALID_TOKEN");
}
return {
token: await this.getToken(user)
};
}
},
async handler(ctx) {
const decoded = await this.verifyJWT(ctx.params.token);
if (!decoded.id)
throw new MoleculerClientError("Invalid token", 401, "INVALID_TOKEN");
const user = await this.getById(decoded.id);
if (!user)
throw new MoleculerClientError("User is not registered", 401, "USER_NOT_FOUND");
if (!user.verified)
throw new MoleculerClientError("Please activate your account!", 401, "ERR_ACCOUNT_NOT_VERIFIED");
if (user.status !== 1)
throw new MoleculerClientError("User is disabled", 401, "USER_DISABLED");
return await this.transformDocuments(ctx, {}, user);
}
},
async handler(ctx) {
const post = this.findByID(ctx.params.id);
if (!post) {
throw new MoleculerClientError("Post is not found");
}
const has = post.voters.find(voter => voter == ctx.params.userID);
if (!has) {
throw new MoleculerClientError("User has not voted this post yet");
}
post.voters = post.voters.filter(voter => voter != ctx.params.userID);
post.votes = post.voters.length;
await ctx.broadcast("graphql.publish", {
tag: "VOTE",
payload: { type: "down", userID: ctx.params.userID },
});
return _.cloneDeep(post);
async handler(ctx) {
const post = this.findByID(ctx.params.id);
if (!post) {
throw new MoleculerClientError("Post is not found");
}
const has = post.voters.find(voter => voter == ctx.params.userID);
if (has) {
throw new MoleculerClientError("User has already voted this post");
}
post.voters.push(ctx.params.userID);
post.votes = post.voters.length;
await ctx.broadcast("graphql.publish", {
tag: "VOTE",
payload: { type: "up", userID: ctx.params.userID },
});
return _.cloneDeep(post);
},
},
.then(users => {
if (users.length == 0)
return this.Promise.reject(new MoleculerClientError("Author not found"));
params.query.author = users[0]._id;
});
}