Merge branch 'frontend-requests' of https://github.com/martin-mfg/github-readme-stats into frontend-requests
This commit is contained in:
@@ -9,7 +9,7 @@ import { default as patInfo } from "./api-renamed/status/pat-info.js";
|
||||
import { default as statusUp } from "./api-renamed/status/up.js";
|
||||
import { default as authenticate } from "./api-renamed/authenticate.js";
|
||||
import { default as deleteUser } from "./api-renamed/delete-user.js";
|
||||
import { default as privateAccess } from "./api-renamed/private-access.js";
|
||||
import { default as userAccess } from "./api-renamed/user-access.js";
|
||||
import { default as downgrade } from "./api-renamed/downgrade.js";
|
||||
|
||||
export default async (req, res) => {
|
||||
@@ -61,8 +61,8 @@ export default async (req, res) => {
|
||||
case "/api/delete-user":
|
||||
await deleteUser(req, res);
|
||||
break;
|
||||
case "/api/private-access":
|
||||
await privateAccess(req, res);
|
||||
case "/api/user-access":
|
||||
await userAccess(req, res);
|
||||
break;
|
||||
case "/api/downgrade":
|
||||
await downgrade(req, res);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { hasPrivateAccess, getUserToken, deleteUser } from "../src/common/database.js";
|
||||
import { getUserAccess, deleteUser } from "../src/common/database.js";
|
||||
import axios from "axios";
|
||||
import { logger } from "../src/index.js";
|
||||
|
||||
export default async (req, res) => {
|
||||
// We could optimize this method by doing all 3 database operations in one statement, using "DELETE ... RETURNING ..."
|
||||
// We could optimize this method by doing both database operations in one statement, using "DELETE ... RETURNING ..."
|
||||
|
||||
const { user_key } = req.query;
|
||||
if (!user_key) {
|
||||
@@ -22,19 +22,18 @@ export default async (req, res) => {
|
||||
);
|
||||
}
|
||||
|
||||
// verify that user has private access
|
||||
const privateAccess = await hasPrivateAccess(user_key);
|
||||
if (!privateAccess) {
|
||||
res.statusCode = 400;
|
||||
res.send("user does not have private access");
|
||||
// get token and private access status
|
||||
const userAccess = await getUserAccess(user_key);
|
||||
if (!userAccess) {
|
||||
res.statusCode = 404;
|
||||
res.send("user not found");
|
||||
return;
|
||||
}
|
||||
|
||||
// get access token for user
|
||||
const token = await getUserToken(user_key);
|
||||
if (!token) {
|
||||
res.statusCode = 404;
|
||||
res.send("user not found");
|
||||
// verify that user has private access
|
||||
if (!userAccess.privateAccess) {
|
||||
res.statusCode = 400;
|
||||
res.send("user does not have private access");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -47,7 +46,7 @@ export default async (req, res) => {
|
||||
username: process.env.OAUTH_CLIENT_ID,
|
||||
password: process.env.OAUTH_CLIENT_SECRET,
|
||||
},
|
||||
data: { access_token: token },
|
||||
data: { access_token: userAccess.token },
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { logger } from "../src/common/utils.js";
|
||||
import { hasPrivateAccess } from "../src/common/database.js";
|
||||
import { getUserAccess } from "../src/common/database.js";
|
||||
|
||||
/**
|
||||
* @param {any} req The request.
|
||||
@@ -8,8 +8,18 @@ import { hasPrivateAccess } from "../src/common/database.js";
|
||||
export default async (req, res) => {
|
||||
const { user_key } = req.query;
|
||||
try {
|
||||
const result = await hasPrivateAccess(user_key);
|
||||
res.send(result);
|
||||
const result = await getUserAccess(user_key);
|
||||
|
||||
if (!result) {
|
||||
res.statusCode = 404;
|
||||
res.send("user not found");
|
||||
return;
|
||||
}
|
||||
|
||||
res.send({
|
||||
privateAccess: result.privateAccess,
|
||||
token: result.token
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
res.send("Something went wrong: " + err.message);
|
||||
@@ -192,18 +192,18 @@ export async function deleteUser(userKey) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if private_access is true for the given user_key.
|
||||
* Fetches token and private access status for a given user_key.
|
||||
*
|
||||
* @param {string} userKey user key of the user to be checked
|
||||
* @returns {Promise<boolean>} true if private_access is true, false otherwise
|
||||
* @param {string} userKey user key of the user to fetch information for
|
||||
* @returns {Promise<{token: string, privateAccess: boolean} | null>} token and private access status, or null if user not found
|
||||
*/
|
||||
export async function hasPrivateAccess(userKey) {
|
||||
export async function getUserAccess(userKey) {
|
||||
if (!pool) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const query = `
|
||||
SELECT private_access
|
||||
SELECT access_token, private_access
|
||||
FROM authenticated_users
|
||||
WHERE user_key = $1
|
||||
LIMIT 1
|
||||
@@ -213,39 +213,10 @@ export async function hasPrivateAccess(userKey) {
|
||||
if (rows.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return rows[0].private_access;
|
||||
} catch (err) {
|
||||
if (err.code === "42P01") {
|
||||
return null;
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches access_token for a given user_key.
|
||||
*
|
||||
* @param {string} userKey user key of the user to fetch token for
|
||||
* @returns Returns user key if found, null otherwise
|
||||
*/
|
||||
export async function getUserToken(userKey) {
|
||||
if (!pool) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const query = `
|
||||
SELECT access_token
|
||||
FROM authenticated_users
|
||||
WHERE user_key = $1
|
||||
LIMIT 1
|
||||
`;
|
||||
try {
|
||||
const { rows } = await pool.query(query, [userKey]);
|
||||
if (rows.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return rows[0].access_token;
|
||||
return {
|
||||
token: rows[0].access_token,
|
||||
privateAccess: rows[0].private_access
|
||||
};
|
||||
} catch (err) {
|
||||
if (err.code === "42P01") {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user