-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created folder structure for backend
- Loading branch information
1 parent
95a2862
commit 95bb157
Showing
1,698 changed files
with
159,755 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
DATABASE_URL="mysql://root:root@localhost:3306/test" |
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,5 @@ | ||
const { PrismaClient } = require("@prisma/client"); | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
module.exports = prisma; |
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,60 @@ | ||
/* eslint-disable no-unused-vars */ | ||
const { | ||
PrismaClientKnownRequestError, | ||
PrismaClientValidationError, | ||
} = require("@prisma/client/runtime"); | ||
|
||
const handleErrors = (err, req, res, next) => { | ||
// Log the error for debugging purposes | ||
console.error(err); | ||
|
||
// Handle known Prisma client request errors | ||
if (err instanceof PrismaClientKnownRequestError) { | ||
return res.status(400).json({ | ||
status: "error", | ||
message: "Bad request - Prisma error", | ||
details: err.message, | ||
}); | ||
} | ||
|
||
// Handle Prisma client validation errors | ||
if (err instanceof PrismaClientValidationError) { | ||
return res.status(400).json({ | ||
status: "error", | ||
message: "Validation error", | ||
details: err.message, | ||
}); | ||
} | ||
|
||
// Handle SyntaxError (Invalid JSON) | ||
if (err.name === "SyntaxError") { | ||
return res.status(400).json({ | ||
status: "error", | ||
message: "Bad request - Invalid JSON", | ||
}); | ||
} | ||
|
||
// Handle MySQL database connection errors | ||
if (err.code === "PROTOCOL_CONNECTION_LOST") { | ||
return res.status(500).json({ | ||
status: "error", | ||
message: "Database connection lost", | ||
}); | ||
} | ||
|
||
// Handle MySQL query execution errors | ||
if (err.code === "ER_PARSE_ERROR") { | ||
return res.status(500).json({ | ||
status: "error", | ||
message: "Database query parse error", | ||
}); | ||
} | ||
|
||
// Handle other unhandled errors | ||
return res.status(500).json({ | ||
status: "error", | ||
message: "Internal server error", | ||
}); | ||
}; | ||
|
||
module.exports = handleErrors; |
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,19 @@ | ||
const express = require("express"); | ||
const cors = require("cors"); | ||
|
||
const AuthApiRoutes = require("./module_auth/routes/auth.routes"); | ||
|
||
const app = express(); | ||
|
||
app.use(express.json()); | ||
app.use(cors()); | ||
|
||
app.use("/auth", AuthApiRoutes); | ||
|
||
app.listen(8000, async () => { | ||
try { | ||
console.log(`Server ready at: http://localhost:8000`); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}); |
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,4 @@ | ||
const jwt = require("jsonwebtoken"); | ||
const { BadRequest } = require("../utils/errors"); | ||
|
||
exports.login = async (req, res, next) => {}; |
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,4 @@ | ||
const jwt = require("jsonwebtoken"); | ||
const bcrypt = require("bcryptjs"); | ||
|
||
exports.register = async (req, res, next) => {}; |
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,40 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
|
||
const verifyToken = (token) => { | ||
return jwt.verify(token, "1234"); | ||
}; | ||
|
||
|
||
const authorization = (req, res, next) => { | ||
|
||
try { | ||
const bearerToken = req?.headers?.authorization; | ||
|
||
if (!bearerToken || !bearerToken.startsWith('Bearer ')) { | ||
return res.status(400).json({ message: 'Please provide a valid token', status: 'Failed' }); | ||
} | ||
|
||
const token = bearerToken.split(" ")[1]; | ||
|
||
let user; | ||
try { | ||
user = verifyToken(token); | ||
} catch (e) { | ||
return res.status(400).json({ message: 'Please provide a valid token', status: 'Failed' }); | ||
} | ||
|
||
if (!user) { | ||
return res.status(400).json({ message: 'User not found', status: 'Failed' }); | ||
} | ||
|
||
req.user = user.user; | ||
|
||
return next(); | ||
|
||
} catch (e) { | ||
return res.status(500).json({ message: e.message, status: 'Failed' }); | ||
} | ||
}; | ||
|
||
module.exports = authorization; |
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,20 @@ | ||
/* eslint-disable no-unused-vars */ | ||
const { GeneralError } = require("../utils/errors"); | ||
|
||
const handleErrors = (err, req, res, next) => { | ||
if (err instanceof GeneralError) { | ||
return res.json({ | ||
status: "error", | ||
message: err.message, | ||
data: err.stack, | ||
}); | ||
} | ||
|
||
return res.status(200).json({ | ||
status: "error", | ||
message: err.message, | ||
data: err.stack, | ||
}); | ||
}; | ||
|
||
module.exports = handleErrors; |
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,20 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
|
||
const handleErrors = require("../middleware/handleErrors"); | ||
|
||
const authorization = require("../middleware/authorization"); | ||
|
||
const RegisterController = require("../controllers/RegisterController"); | ||
const LoginController = require("../controllers/LoginController"); | ||
|
||
// HANDLING ERRORS | ||
router.use(handleErrors); | ||
|
||
// REGISTER NEW USER | ||
router.post("/register", RegisterController.register); | ||
|
||
// LOGIN USING EMAIL/PASSWORD | ||
router.post("/login", LoginController.login); | ||
|
||
module.exports = router; |
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,25 @@ | ||
/* eslint-disable no-use-before-define */ | ||
// eslint-disable-next-line max-classes-per-file | ||
class GeneralError extends Error { | ||
constructor(message) { | ||
super(); | ||
this.message = message; | ||
} | ||
|
||
getCode() { | ||
if (this instanceof BadRequest) { | ||
return 200; | ||
} | ||
if (this instanceof NotFound) { | ||
return 404; | ||
} | ||
return 500; | ||
} | ||
} | ||
|
||
class BadRequest extends GeneralError { } | ||
class NotFound extends GeneralError { } | ||
|
||
module.exports = { | ||
GeneralError, BadRequest, NotFound, | ||
}; |
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 @@ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.