Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkout are signup,updateUser inpusts valid! #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
"pm2": "^5.2.0",
"sqlite": "^4.0.23",
"sqlite3": "^5.1.1",
"ts-node": "^10.9.1"
"ts-node": "^10.9.1",
"validator": "^13.9.0"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jest": "^28.1.8",
"@types/jsonwebtoken": "^8.5.8",
"@types/supertest": "^2.0.12",
"@types/validator": "^13.7.17",
"jest": "^29.0.3",
"supertest": "^6.2.4",
"ts-jest": "^29.0.0",
Expand Down
9 changes: 9 additions & 0 deletions server/src/handlers/userHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import crypto from 'crypto';
import { signJwt } from '../auth';
import { Datastore } from '../datastore';
import { ExpressHandler, ExpressHandlerWithParams } from '../types';
import validator from 'validator'

export class UserHandler {
private db: Datastore;
Expand Down Expand Up @@ -57,6 +58,12 @@ export class UserHandler {
return res.status(400).send({ error: ERRORS.USER_REQUIRED_FIELDS });
}

if (!validator.isEmail(email)) return res.status(400).send({ error: ERRORS.INVALID_EMAIL});
if (!validator.isAlphanumeric(userName)) return res.status(400).send({ error: ERRORS.INVALID_USERNAME});
if (!validator.isStrongPassword(password)) return res.status(400).send({ error: ERRORS.WEAK_PASSWORD});
if (firstName && !validator.isAlphanumeric(firstName)) return res.status(400).send({ error: ERRORS.INVALID_FIRSTNAME});
if (lastName && !validator.isAlphanumeric(lastName)) return res.status(400).send({ error: ERRORS.INVALID_LASTNAME});

if (await this.db.getUserByEmail(email)) {
return res.status(403).send({ error: ERRORS.DUPLICATE_EMAIL });
}
Expand Down Expand Up @@ -121,6 +128,8 @@ export class UserHandler {
const currentUserId = res.locals.userId;
const { userName } = req.body;

if (userName && !validator.isAlphanumeric(userName)) return res.status(400).send({ error: ERRORS.INVALID_USERNAME});

if (userName && (await this.isDuplicateUserName(currentUserId, userName))) {
return res.status(403).send({ error: ERRORS.DUPLICATE_USERNAME });
}
Expand Down
5 changes: 5 additions & 0 deletions shared/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export enum ERRORS {
USER_REQUIRED_FIELDS = 'Email, username, and password are required',
DUPLICATE_EMAIL = 'An account with this email already exists',
DUPLICATE_USERNAME = 'An account with this username already exists',
INVALID_EMAIL = 'Invalid email',
WEAK_PASSWORD = 'Weak password',
INVALID_USERNAME = 'Username must have only letters and numbers',
INVALID_FIRSTNAME = 'First name must have only letters and numbers',
INVALID_LASTNAME = 'Last name must have only letters and numbers',

POST_ID_MISSING = 'Post ID is missing',
POST_NOT_FOUND = 'Post not found',
Expand Down