-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from PRATHAM1ST/master
Added Middleware and Handler
- Loading branch information
Showing
7 changed files
with
80 additions
and
26 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
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 @@ | ||
import { Request, Response } from "express"; | ||
|
||
class ResponseHandler { | ||
static success(req: Request, res: Response, data: any, message: string) { | ||
return res.status(200).json({ | ||
status: true, | ||
data, | ||
message, | ||
}); | ||
} | ||
|
||
static error(req: Request, res: Response, message: string) { | ||
return res.status(500).json({ | ||
status: false, | ||
message, | ||
}); | ||
} | ||
} | ||
|
||
export default ResponseHandler; |
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
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 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
function auth(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const token = req.header("Authorization"); | ||
if (!token) throw Error("No token, authorization denied"); | ||
|
||
const [user, password] = token?.split(" "); | ||
if (!user || !password) throw Error("No token, authorization denied"); | ||
if ( | ||
user !== process.env.USERNAME?.toLowerCase() || | ||
password !== process.env.PASSWORD?.toLowerCase() | ||
) | ||
throw Error("Invalid token, authorization denied"); | ||
|
||
next(); | ||
} catch (err) { | ||
return res.status(500).json({ | ||
status: false, | ||
message: err, | ||
}); | ||
} | ||
} | ||
|
||
export default auth; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import express from "express"; | ||
import controllers from "../controllers/members"; | ||
import auth from "../middleware/auth"; | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/", controllers.GET); | ||
router.patch("/", controllers.PATCH); | ||
router.patch("/", auth, controllers.PATCH); | ||
|
||
export default router; |