-
Notifications
You must be signed in to change notification settings - Fork 1
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
Server Files #1
Open
justuravgcoder
wants to merge
4
commits into
arbimaq:main
Choose a base branch
from
justuravgcoder:changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Server Files #1
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
const exp = require("constants"); | ||
const cookieParser = require("cookie-parser"); | ||
const express = require("express"); | ||
const { request} = require("http"); | ||
const { type } = require("os"); | ||
const app = express(); | ||
|
||
const PORT = 8080; | ||
|
||
app.use(express.json()); | ||
app.use(express.urlencoded({extended:false})); | ||
app.use(express.text()); | ||
app.use(cookieParser()); | ||
|
||
const USERNAME = "Shurahbeel"; | ||
const PASSWORD = "password"; | ||
|
||
const authMiddleware = (req,res,next)=>{ | ||
if (req.cookies.auth === "loggedin") { | ||
next(); | ||
} else { | ||
res.status(401).send("Unauthorized Access. Please Login"); | ||
} | ||
} | ||
|
||
const repovalidation = (req,res,next)=>{ | ||
const data = req.body; | ||
if (specialwords(data.repo_name) || specialwords(data.repo_author) || specialwords(data.details)) | ||
{ | ||
res.status(400).send("Please avoid special characters in Name, Author, and Details"); | ||
} | ||
else if (typeof data.year_created !== "number" || typeof data.commits !== "number") | ||
{ | ||
res.status(400).send("Please use numbers when providing commits and year created"); | ||
} | ||
else if (!data.repo_name || !data.repo_author || !data.details || !data.year_created || !data.commits) | ||
{ | ||
res.status(401).send("Missing Required Fields"); | ||
} | ||
|
||
next(); | ||
} | ||
|
||
function specialwords(data) { | ||
const notallowed = "!@#$%^&*()_+[]{}|;':\",./<>?`~\\-="; | ||
for (let i = 0; i < data.length; i++) { | ||
if (notallowed.includes(data[i])) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
const repo_data = [ | ||
{ | ||
"repo_name": "Hackathon prep", | ||
"details": "this repo comprises code regarding hackathon 2024", | ||
"repo_author": "Shurahbeel Peerzada", | ||
"commits": 214, | ||
"year_created": 2024, | ||
}, | ||
{ | ||
"repo_name": "Machine Learning Models", | ||
"details": "This repository contains machine learning models and datasets.", | ||
"repo_author": "Alexis Jordan", | ||
"commits": 150, | ||
"year_created": 2023 | ||
}, | ||
{ | ||
"repo_name": "Weather App", | ||
"details": "A weather forecasting application using React and Node.js.", | ||
"repo_author": "Liam Patterson", | ||
"commits": 75, | ||
"year_created": 2022 | ||
}, | ||
{ | ||
"repo_name": "E-commerce Backend", | ||
"details": "Backend code for an e-commerce platform built with Django.", | ||
"repo_author": "Emily Martinez", | ||
"commits": 340, | ||
"year_created": 2021 | ||
}, | ||
{ | ||
"repo_name": "Portfolio Website", | ||
"details": "Personal portfolio website showcasing projects and blogs.", | ||
"repo_author": "Shurahbeel Peerzada", | ||
"commits": 90, | ||
"year_created": 2024 | ||
}, | ||
{ | ||
"repo_name": "Data Visualization Tool", | ||
"details": "A tool for visualizing complex data sets using D3.js.", | ||
"repo_author": "Nina West", | ||
"commits": 125, | ||
"year_created": 2023 | ||
} | ||
] | ||
|
||
app.listen(PORT, () => console.log(`Running on port number: ${PORT}`)); | ||
|
||
app.post('/login', (req, res) => { | ||
const { username, password } = req.body; | ||
if((username == "") && (password == "")) | ||
{ | ||
res.status(400).send("Missing Username and Password") | ||
} | ||
else{ | ||
if (username === USERNAME && password === PASSWORD) { | ||
res.cookie('auth', 'loggedin', { httpOnly: true}); | ||
res.status(200).send("User Successfully Logged in"); | ||
} | ||
else | ||
{ | ||
res.status(401).send("Incorrect Credentials Entered"); | ||
} | ||
} | ||
|
||
}); | ||
|
||
app.get('/getallrepos',authMiddleware ,(req, res) => { | ||
res.status(200).send(repo_data); | ||
}); | ||
|
||
app.get('/getrepodetail',authMiddleware ,(req, res) => { | ||
let name = req.query.name; | ||
if (name) { | ||
if (specialwords(name)) { | ||
res.status(400).send("Please avoid using special characters"); | ||
} else { | ||
Comment on lines
+125
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets move this to middleware as well. In this middleware you can do |
||
let specificRepo = repo_data.find(repo => repo.repo_name === name); | ||
if (specificRepo === undefined) { | ||
res.status(404).send("No repo matching the provided name exists"); | ||
} else { | ||
res.status(200).send(specificRepo); | ||
} | ||
} | ||
} else { | ||
res.status(400).send("Please provide a repo name"); | ||
} | ||
}); | ||
|
||
app.post('/create-repo',authMiddleware,repovalidation,(req, res) => { | ||
const data = req.body; | ||
repo_data.push(data); | ||
res.status(201).send("Repo Created"); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if username and password are provided. If not return 400 response with message to let user know of the missing fields
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the required changes