Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Graphql input validation checks start
const validationErrors = {};
if (!service) {
validationErrors[service] = 'MISSING_VALUE';
throw new UserInputError('Missing user login service type', { validationErrors });
}
if (serviceType === ServiceTypes.Password) {
if (!params.password || !params.email) {
validationErrors[params.password || params.email] = 'MISSING_VALUE';
throw new UserInputError('Missing user login password or email', { validationErrors });
}
} else {
if (!params.accessToken || !params.accessTokenSecret) {
validationErrors[params.accessToken || params.accessTokenSecret] = 'MISSING_VALUE';
throw new UserInputError(`Missing user access token or secret for ${service.toString().toLowerCase()} strategy`, { validationErrors });
}
}
if (service === ServiceTypes.Password) {
// Authenticate against passport local strategy
const auth = await context.authenticate('graphql-local', { email: params.email, password: params.password });
auth.user.whoImI = new IdentifyMachineUtils(context.req).sender();
context.login(auth.user);
return {
id: auth.user.id,
sessionId: context.req.sessionID,
};
}
async projectMutations(@Args() input: ProjectMutations, @Context() ctx: any, @CurrentUser() user: UserEntity): Promise {
const { create, delete: remove, update } = input;
if (create === null && remove === null && update === null) { // Check to make sure input is not null
throw new UserInputError('Mutation inputs missing'); // Throw an apollo input error
}
if (create) {
// return await this.commandBus.execute(new CreateProjectCommand(create));
return await this.projectService.create(create);
} else if (update) {
// return await this.commandBus.execute(new UpdateProjectCommand(update));
return await this.projectService.update(update);
} else if (remove) {
// return await this.commandBus.execute(new DeleteProjectCommand(remove));
return await this.projectService.delete(remove);
} else {
throw new UserInputError('Mutation inputs missing'); // Throw an apollo input error
}
}
async execute(query: GetPlanQuery): Promise {
this.logger.log(`Async ${query.constructor.name}...`);
const { id } = query;
if (!id) { throw new UserInputError('Missing plan id input'); }
try {
// Check cache to see if data exist
const cacheData = await this.cacheStore.get('service-payment/plan/' + id);
if (cacheData !== undefined && typeof cacheData !== 'undefined') {
return cacheData;
}
const product = await this.stripeClient.products.retrieve(id);
const plans = await this.stripeClient.plans.list({
product: product.id,
});
const result = convertToPlan(plans, product);
await this.cacheStore.set('service-payment/plan/' + id, result, {ttl: 50});
return result;
throw new UserInputError("Email not found", {});
}
// Check password
// It is not recommended in production. Use libraries like bscript.
if (password == user.password) {
// User matched
// Create JWT Payload
const payload = {
id: user.id,
name: user.name,
};
return await sign(payload);
} else {
throw new UserInputError("Password incorrect", {});
}
},
}
async login(
@Args('input') { service, params }: LoginInput,
@Context() context: any): Promise {
const serviceType = ServiceTypes[service];
// Graphql input validation checks start
const validationErrors = {};
if (!service) {
validationErrors[service] = 'MISSING_VALUE';
throw new UserInputError('Missing user login service type', { validationErrors });
}
if (serviceType === ServiceTypes.Password) {
if (!params.password || !params.email) {
validationErrors[params.password || params.email] = 'MISSING_VALUE';
throw new UserInputError('Missing user login password or email', { validationErrors });
}
} else {
if (!params.accessToken || !params.accessTokenSecret) {
validationErrors[params.accessToken || params.accessTokenSecret] = 'MISSING_VALUE';
throw new UserInputError(`Missing user access token or secret for ${service.toString().toLowerCase()} strategy`, { validationErrors });
}
}
if (service === ServiceTypes.Password) {
function issueValidate(issue) {
const errors = [];
if (issue.title.length < 3) {
errors.push('Field "title" must be at least 3 characters long.');
}
if (issue.status === 'Assigned' && !issue.owner) {
errors.push('Field "owner" is required when status is "Assigned"');
}
if (errors.length > 0) {
throw new UserInputError('Invalid input(s)', { errors });
}
}
function validate(issue) {
const errors = [];
if (issue.title.length < 3) {
errors.push('Field "title" must be at least 3 characters long.');
}
if (issue.status === 'Assigned' && !issue.owner) {
errors.push('Field "owner" is required when status is "Assigned"');
}
if (errors.length > 0) {
throw new UserInputError('Invalid input(s)', { errors });
}
}
const getUser = baseResolver.createResolver(async (root, { input }) => {
const { id, username } = input;
if ((!id && !username) || (id && username)) {
throw new UserInputError(
'You need to provide the ID or the username, but not both'
);
}
if (id) return await findUserById(id);
if (username) return await findUserByUsername(username);
return null;
});