Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { isEmail } from '../../../../utils/validate';
import { maybe, required, restrictToSchema}from 'rulr';
import { ModelNotFoundError} from '../../../../utils/errors';
import {v4} from 'uuid';
import {OK} from 'http-status-codes';
const validateForgetPassword = maybe(
restrictToSchema({
email: required(isEmail)
})
);
export default (config: Config) => {
return catchErrors(config, async (req, res)=> {
validateForgetPassword(req.body, ['user']);
const {email} = req.body;
const errorId = v4();
try {
await config.service.forgetPassword({email});
const message = config.translator.passwordReminderSent(email);
res.status(OK).json({message});
}catch(err){
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_CREATE_COMMENT, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';
const validateCreateComment = maybe(composeRules([
restrictToSchema({
user_id: required(checkType(Number)),
post_id: required(checkType(Number)),
body: required(maxLength(TEXT_FIELD_LENGTH)),
})
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_CREATE_COMMENT});
validateCreateComment(req.body, ['comment']);
const createdComment = await config.service.createComment(req.body);
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_CREATE_POST, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';
const validateCreatePost = maybe(composeRules([
restrictToSchema({
user_id: required(checkType(String)),
title: required(maxLength(VARCHAR_FIELD_LENGTH)),
body: required(maxLength(TEXT_FIELD_LENGTH)),
})
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_CREATE_POST});
validateCreatePost(req.body, ['post']);
const createdPost = await config.service.createPost(req.body);
res.status(CREATED).json(createdPost);
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import { minLength, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, required, checkType, composeRules, first, restrictToSchema } from 'rulr';
const validateResetPassword = maybe(composeRules([
restrictToSchema({
token: required(checkType(String)),
password: required(minLength(6)),
password_confirmation: required(checkType(String)),
}),
first(checkType(Object), validateMatchingPasswords)
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
validateResetPassword(req.body, ['user']);
const {token, password } = req.body;
await config.service.resetPassword({token, password});
const message = config.translator.passwordChangedSuccessfully();
res.status(OK).json({message});
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import { minLength, isEmail, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, required, restrictToSchema, checkType, composeRules, first }from 'rulr';
const validateRegister = maybe(composeRules([
restrictToSchema({
email: required(isEmail),
password: required(minLength(6)),
password_confirmation: required(checkType(String)),
}),
first(checkType(Object), validateMatchingPasswords)
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
validateRegister(req.body, ['user']);
const {email, password, bio, firstname, lastname} = req.body;
const {user, token} = await config.service.register({
email, password, bio, firstname, lastname
});
res.status(CREATED).json({user,token});
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import { minLength, isEmail, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, required, restrictToSchema, checkType, composeRules, first }from 'rulr';
const validateRegister = maybe(composeRules([
restrictToSchema({
email: required(isEmail),
password: required(minLength(6)),
password_confirmation: required(checkType(String)),
}),
first(checkType(Object), validateMatchingPasswords)
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
validateRegister(req.body, ['user']);
const {email, password, bio, firstname, lastname} = req.body;
const {user, token} = await config.service.register({
email, password, bio, firstname, lastname
});
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_CREATE_COMMENT, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';
const validateCreateComment = maybe(composeRules([
restrictToSchema({
user_id: required(checkType(Number)),
post_id: required(checkType(Number)),
body: required(maxLength(TEXT_FIELD_LENGTH)),
})
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_CREATE_COMMENT});
validateCreateComment(req.body, ['comment']);
const createdComment = await config.service.createComment(req.body);
res.status(CREATED).json(createdComment);
});
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_REVOKE_PERMISSION, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maybe, required, checkType,composeRules, restrictToSchema } from 'rulr';
const validateRevokeRolePermission = maybe(composeRules([
restrictToSchema({
permission_id: required(checkType(String)),
role_id: required(checkType(String))
})
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_REVOKE_PERMISSION});
validateRevokeRolePermission(req.params, ['role']);
await config.service.revokeRolePermission(req.params);
res.status(OK).json({success: true});
});