-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented authorization middleware before being able to access DB
- Loading branch information
Showing
2 changed files
with
33 additions
and
2 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,30 @@ | ||
import { queryDatabase } from "routes/databaseFunctions"; | ||
import { Client } from "pg"; | ||
|
||
const authorize = function (client: Client) { | ||
return async (req: any, res: any, next: any) => { | ||
const name = req.headers.name.toLowerCase(); | ||
const key = req.headers.authorization?.split(" ")[1]; | ||
const query = { | ||
text: "SELECT * FROM apikey WHERE name = $1 AND apikey = $2", | ||
values: [name, key] | ||
} | ||
|
||
if (!name || !key) { | ||
return res.status(400).json({ message: "Please enter your name and key before accessing the database!" }); | ||
} | ||
|
||
try { | ||
const result = await queryDatabase(client, query.text, query.values); | ||
if (result.rows.length === 0) { | ||
return res.status(401).json({ message: "Invalid name or key!" }); | ||
} | ||
next(); | ||
} | ||
catch (e: any) { | ||
return res.status(500).json({ message: e.message }); | ||
} | ||
} | ||
} | ||
|
||
export default authorize; |
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