Skip to content

Commit

Permalink
Use strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yash22arora committed May 24, 2024
1 parent 9abb667 commit 21bf2d2
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 23 deletions.
2 changes: 2 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ RUN npm ci --omit=dev
# Bundle app source
COPY . .

# Because we have the postinstall script in the package.json file, we dont need to run the tsc command separately. npm install will take care of it.

# Expose the port the app runs on
EXPOSE 5000

Expand Down
4 changes: 3 additions & 1 deletion server/package-lock.json

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

3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"jsonwebtoken": "^9.0.0",
"mongoose": "^7.0.3",
"morgan": "^1.10.0",
"stripe": "^12.16.0"
"stripe": "^12.16.0",
"typescript": "^5.4.5"
},
"devDependencies": {
"@types/axios": "^0.14.0",
Expand Down
9 changes: 7 additions & 2 deletions server/src/api/middleware/check-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ const checkAuth = (
next: NextFunction
) => {
try {
const token = req.headers.authorization.split(" ")[1];
const decoded = jwt.verify(token, process.env.JWT_KEY);
const token = req.headers.authorization?.split(" ")[1];
if (!token) {
return res.status(401).json({
message: "Auth failed",
});
}
const decoded = jwt.verify(token, process.env.JWT_KEY!);
req.userData = decoded as AuthenticatedRequest["userData"];
next();
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/api/routes/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ router.post(
"/",
checkAuth,
async (req: DomainPostRequest, res: Response, next: NextFunction) => {
const userID = req.userData.userID;
const userID = req.userData!.userID;
const { cfAuthToken, cfZoneID, restrictedSubdomains = [] } = req.body;
try {
let cf_response = await axios
Expand Down Expand Up @@ -77,7 +77,7 @@ router.post(
error: "Invalid Cloudflare Zone ID or Auth Token",
});
}
} catch (error) {
} catch (error: any) {
if (error.response.status === 403) {
return res.status(500).json({
error: "Invalid Cloudflare Auth Token",
Expand Down
6 changes: 3 additions & 3 deletions server/src/api/routes/subdomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ router.get(
});

if (!domain) {
res.status(400).json({
return res.status(400).json({
error: "Invalid domain",
});
}
Expand Down Expand Up @@ -104,15 +104,15 @@ router.get(
);

const getSubdomainList = async (cfZoneID: string, cfAuthToken: string) => {
const subdomainList = [];
const subdomainList: string[] = [];
await axiosInstance
.get(`/${cfZoneID}/dns_records`, {
headers: {
Authorization: `Bearer ${decrypt(cfAuthToken)}`,
},
})
.then((response) => {
response.data.result.forEach((record) => {
response.data.result.forEach((record: { name: string }) => {
subdomainList.push(record.name);
});
})
Expand Down
24 changes: 14 additions & 10 deletions server/src/api/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ router.post(
phoneNumber: phoneNumber,
firebaseUID: firebaseUID,
},
process.env.JWT_KEY,
process.env.JWT_KEY!,
{
expiresIn: "7d",
}
Expand Down Expand Up @@ -57,7 +57,7 @@ router.post(
phoneNumber: phoneNumber,
firebaseUID: firebaseUID,
},
process.env.JWT_KEY,
process.env.JWT_KEY!,
{
expiresIn: "7d",
}
Expand Down Expand Up @@ -91,7 +91,7 @@ router.get(
"/",
checkAuth,
(req: AuthenticatedRequest, res: Response, next: NextFunction) => {
const userID = req.userData.userID;
const userID = req.userData!.userID;
User.findOne({ _id: userID })
.then((result) => {
res.status(200).json({
Expand All @@ -113,7 +113,7 @@ router.patch(
"/update",
checkAuth,
async (req: UpdateUserRequest, res: Response, next: NextFunction) => {
const userID = req.userData.userID;
const userID = req.userData!.userID;

const doc = await User.findOneAndUpdate({ _id: userID }, req.body, {
new: true,
Expand All @@ -138,7 +138,7 @@ router.post(
"/waitlist",
checkAuth,
async (req: UpdateUserRequest, res: Response, next: NextFunction) => {
const userID = req.userData.userID;
const userID = req.userData!.userID;
const onWaitlist = req.body.onWaitlist;

const doc = await User.findOneAndUpdate(
Expand All @@ -162,10 +162,14 @@ router.post(
}
);

router.post("/verify", checkAuth, (req, res, next) => {
res.status(200).json({
message: "User verified",
});
});
router.post(
"/verify",
checkAuth,
(req: AuthenticatedRequest, res: Response, next: NextFunction) => {
res.status(200).json({
message: "User verified",
});
}
);

export default router;
2 changes: 1 addition & 1 deletion server/src/utils/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request } from "express";

export interface AuthenticatedRequest extends Request {
userData: {
userData?: {
userID: string;
phoneNumber: string;
firebaseUID: string;
Expand Down
4 changes: 2 additions & 2 deletions server/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require("dotenv").config();
import * as crypto from "crypto";

// Encryption and decryption key
const ENCRYPTION_KEY = process.env.CIPHER_KEY; // Must be 256 bits (32 characters)
const ENCRYPTION_KEY = process.env.CIPHER_KEY!; // Must be 256 bits (32 characters)
const IV_LENGTH = 16; // For AES, this is always 16

function encrypt(text: string) {
Expand All @@ -22,7 +22,7 @@ function encrypt(text: string) {

function decrypt(text: string) {
let textParts = text.split(":");
let iv = Buffer.from(textParts.shift(), "hex");
let iv = Buffer.from(textParts.shift()!, "hex");
let encryptedText = Buffer.from(textParts.join(":"), "hex");
let decipher = crypto.createDecipheriv(
"aes-256-cbc",
Expand Down
2 changes: 1 addition & 1 deletion server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

/* Type Checking */
"strict": false /* Enable all strict type-checking options. */,
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
Expand Down

0 comments on commit 21bf2d2

Please sign in to comment.