Skip to content
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
10 changes: 5 additions & 5 deletions backend/src/controller/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export const refreshAccessToken = async (req: Request, res: Response) => {
})
}

blackListToken(decodeRefreshToken.jti, 604800);
blackListToken(decodeAccessToken.jti, 900);
await blackListToken(decodeRefreshToken.jti, 604800);
await blackListToken(decodeAccessToken.jti, 900);

generateToken(decodeRefreshToken.id, res);

Expand All @@ -156,7 +156,7 @@ export const refreshAccessToken = async (req: Request, res: Response) => {
}


export const logoutUser = (req: Request, res: Response) => {
export const logoutUser = async (req: Request, res: Response) => {
const refreshToken = req.cookies.refreshToken;
const accessToken = req.cookies.accessToken;

Expand All @@ -171,8 +171,8 @@ export const logoutUser = (req: Request, res: Response) => {
const decodeAccessToken = jwt.verify(accessToken, process.env.JWT_SECRET!) as jwtPayload;

//blacklist both tokens
blackListToken(decodeRefreshToken.jti, 604800);
blackListToken(decodeAccessToken.jti, 900);
await blackListToken(decodeRefreshToken.jti, 604800);
await blackListToken(decodeAccessToken.jti, 900);

res.clearCookie("refreshToken");
res.clearCookie("accessToken");
Expand Down
18 changes: 12 additions & 6 deletions backend/src/controller/command.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export async function init(argv: any) {
}

} catch (error) {
console.log("Error in intialising the repository", error);
console.error("Error initializing the repository:", error);
process.exitCode = 1;
}
}

Expand All @@ -96,8 +97,9 @@ export async function addRepo(argv: any) {
if (error.code === 'ENOENT') {
console.error(`Error: The file "${argv.file}" does not exist.`);
} else {
console.error("Error adding file: ", error.message);
console.error("Error adding file:", error.message);
}
process.exitCode = 1;
}
}

Expand Down Expand Up @@ -156,7 +158,8 @@ export async function commitRepo(argv: ArgumentsCamelCase<CommitArgs>) {
console.log(`Message: ${argv.message}`)

} catch (error) {
console.log("Error in commiting: ", error);
console.error("Error in committing:", error);
process.exitCode = 1;
}
}

Expand Down Expand Up @@ -198,7 +201,8 @@ export async function pushRepo() {
console.log("All commits pushed to S3.");

} catch (error) {
console.log("Error pushing to s3: ", error);
console.error("Error pushing to S3:", error);
process.exitCode = 1;
}
}

Expand Down Expand Up @@ -251,7 +255,8 @@ export async function pullRepo() {
console.log("All commits pulled from s3 successfully");

} catch (error) {
console.log("Error in pulling from s3: ", error);
console.error("Error pulling from S3:", error);
process.exitCode = 1;
}
}

Expand Down Expand Up @@ -281,6 +286,7 @@ export async function revertRepo({commitId}: {commitId: string}) {
console.log(`Commit ${commitId} reverted successfully.`);

} catch (error) {
console.log("Unable to revert: ", error);
console.error("Unable to revert:", error);
process.exitCode = 1;
}
}
30 changes: 25 additions & 5 deletions backend/src/controller/issue.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export const createIssue = async (req: Request, res: Response) => {
}

const userId = req.user?.id;
if(!userId) return;
if(!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

try {
const ownerName = parsedParams.data.owner;
Expand Down Expand Up @@ -203,7 +207,11 @@ export const getIssueById = async (req: Request, res: Response) => {

export const updateIssue = async (req: Request, res: Response) => {
const userId = req.user?.id;
if(!userId) return;
if(!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

const parsedParams = issueByIdSchema.safeParse(req.params);
if(!parsedParams.success) {
Expand Down Expand Up @@ -281,7 +289,11 @@ export const updateIssue = async (req: Request, res: Response) => {

export const deleteIssue = async (req: Request, res: Response) => {
const userId = req.user?.id;
if(!userId) return;
if(!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

const parsedParams = issueByIdSchema.safeParse(req.params);
if(!parsedParams.success) {
Expand Down Expand Up @@ -327,7 +339,11 @@ export const deleteIssue = async (req: Request, res: Response) => {
//own issues
export const getMyIssues = async (req: Request, res: Response) => {
const userId = req.user?.id;
if(!userId) return;
if(!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

try {
const issuesAcrossAllRepos = await prismaClient.issue.findMany({
Expand Down Expand Up @@ -378,7 +394,11 @@ export const updateMyIssuesById = async (req: Request, res: Response) => {
}

const userId = req.user?.id;
if(!userId) return;
if(!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

const parsedData = issueUpdateSchema.safeParse(req.body);
if(!parsedData.success) {
Expand Down
16 changes: 14 additions & 2 deletions backend/src/controller/repo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ export const getRepositoryByFullName = async (req: Request, res: Response) => {
//Authenticated Operations
export const getMyRepositories = async (req: Request, res: Response) => {
const userId = req.user?.id;
if(!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

try {
const userReopos = await prismaClient.user.findUnique({
Expand Down Expand Up @@ -279,6 +284,11 @@ export const getMyRepositories = async (req: Request, res: Response) => {

export const getMyStarredRepos = async (req: Request, res: Response) => {
const userId = req.user?.id;
if(!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

try {
const userStarredReopos = await prismaClient.user.findUnique({
Expand Down Expand Up @@ -471,7 +481,9 @@ export const deleteRepository = async (req: Request, res: Response) => {
if (error.code === 'P2025') { //Prisma's "record not found" error code
return res.status(404).json({ message: "Repo not found" })
}
throw error
res.status(500).json({
message: "Error in server"
})
}
}

Expand Down Expand Up @@ -651,7 +663,7 @@ export const unstarRepository = async (req: Request, res: Response) => {
})
}
if(targetRepo.staredBy.length === 0) {
res.status(400).json({
return res.status(400).json({
message: "You haven't starred this repo"
})
}
Expand Down
28 changes: 22 additions & 6 deletions backend/src/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export const getCurrentUser = async (req: Request, res: Response) => {

export const updateUserProfile = async (req: Request, res: Response) => {
const userId = req.user?.id;
if (!userId) return;
if (!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

const parsedData = userUpdateSchema.safeParse(req.body);
if (!parsedData.success) {
Expand Down Expand Up @@ -147,7 +151,11 @@ export const deleteUserProfile = async (req: Request, res: Response) => {
const refreshToken = req.cookies.refreshToken;
const accessToken = req.cookies.accessToken;
const userId = req.user?.id; //from cookies
if (!userId) return;
if (!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

try {
const user = await prismaClient.user.delete({
Expand All @@ -160,8 +168,8 @@ export const deleteUserProfile = async (req: Request, res: Response) => {
const decodeAccessToken = jwt.verify(accessToken, process.env.JWT_SECRET!) as jwtPayload;

//blacklist both tokens
blackListToken(decodeRefreshToken.jti, 604800);
blackListToken(decodeAccessToken.jti, 900);
await blackListToken(decodeRefreshToken.jti, 604800);
await blackListToken(decodeAccessToken.jti, 900);

res.clearCookie("refreshToken");
res.clearCookie("accessToken");
Expand All @@ -181,7 +189,11 @@ export const deleteUserProfile = async (req: Request, res: Response) => {

export const getMyFollowers = async (req: Request, res: Response) => {
const userId = req.user?.id;
if (!userId) return;
if (!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

try {
//following me
Expand Down Expand Up @@ -216,7 +228,11 @@ export const getMyFollowers = async (req: Request, res: Response) => {

export const getMyFollowing = async (req: Request, res: Response) => {
const userId = req.user?.id;
if (!userId) return;
if (!userId) {
return res.status(401).json({
message: "Unauthorized"
})
}

try {
const user = await prismaClient.user.findUnique({
Expand Down
7 changes: 6 additions & 1 deletion backend/src/middlewares/isAuthenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export default async function isAuthenticated(req: Request, res: Response, next:
}

const secret = process.env.JWT_SECRET;
if(!secret) return;
if(!secret) {
console.error("JWT_SECRET environment variable is not configured");
return res.status(500).json({
message: "Server configuration error"
});
}

try {
const decodeAccessToken = jwt.verify(accessToken, secret) as jwtPayload;
Expand Down
7 changes: 6 additions & 1 deletion backend/src/middlewares/optionalAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import type { jwtPayload } from "../utils/generateToken.js";
export default async function optionalAuth(req: Request, res: Response, next: NextFunction) {
const accessToken = req.cookies.accessToken;
const secret = process.env.JWT_SECRET;
if(!secret) return;
if(!secret) {
console.error("JWT_SECRET environment variable is not configured");
return res.status(500).json({
message: "Server configuration error"
});
}

if(!accessToken) {
return next();
Expand Down
3 changes: 2 additions & 1 deletion backend/src/utils/blacklistToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default async function blackListToken(token: string, expiryInSeconds: num
EX: expiryInSeconds
});
} catch (error) {
console.log("Error in token blacklisting funtion", error)
console.error("Error in token blacklisting function:", error);
throw new Error("Failed to blacklist token");
}
}
64 changes: 37 additions & 27 deletions backend/src/utils/copyRepoFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,50 @@ import { s3 } from "../config/aws.config.js";
export default async function copyRepoFilesInS3(originalRepoId: string, forkedByUserId: string) {
const prefix = `repos/${originalRepoId}/commits/`;
const S3_BUCKET = process.env.S3_BUCKET;
const forkedRepoId = `${originalRepoId}_${forkedByUserId}`
if(!S3_BUCKET) {
throw new Error("S3_BUCKET environment variable is not configured");
}

//get all files from s3
const listCommad = new ListObjectsV2Command({
Bucket: S3_BUCKET,
Prefix: prefix,
})
const forkedRepoId = `${originalRepoId}_${forkedByUserId}`

const listed = await s3.send(listCommad);
try {
//get all files from s3
const listCommad = new ListObjectsV2Command({
Bucket: S3_BUCKET,
Prefix: prefix,
})

if(!listed.Contents || listed.Contents?.length === 0){
console.log("Original repo has no pushed commits to copy.");
return forkedRepoId;
}
const listed = await s3.send(listCommad);

for(const obj of listed.Contents) {
if(!obj.Key) {
continue;
if(!listed.Contents || listed.Contents?.length === 0){
console.log("Original repo has no pushed commits to copy.");
return forkedRepoId;
}

const newKey = obj.Key.replace(
`repos/${originalRepoId}/`, `repos/${originalRepoId}_${forkedByUserId}/`
)
for(const obj of listed.Contents) {
if(!obj.Key) {
continue;
}

//this command will copy in the new created forked repo
const copyCommand = new CopyObjectCommand({
Bucket: S3_BUCKET,
CopySource: `${S3_BUCKET}/${obj.Key}`,
Key: newKey,
})
const newKey = obj.Key.replace(
`repos/${originalRepoId}/`, `repos/${originalRepoId}_${forkedByUserId}/`
)

await s3.send(copyCommand);
}
//this command will copy in the new created forked repo
const copyCommand = new CopyObjectCommand({
Bucket: S3_BUCKET,
CopySource: `${S3_BUCKET}/${obj.Key}`,
Key: newKey,
})

await s3.send(copyCommand);
}

console.log("S3 fiels copied to forked path.");
return forkedRepoId;
console.log("S3 files copied to forked path.");
return forkedRepoId;

} catch (error) {
console.error("Error copying repo files in S3:", error);
throw new Error("Failed to copy repository files in S3");
}
}
Loading