diff --git a/_dot_vercel_copy/output/functions/api/logout.func b/_dot_vercel_copy/output/functions/api/logout.func new file mode 120000 index 00000000..2e79e533 --- /dev/null +++ b/_dot_vercel_copy/output/functions/api/logout.func @@ -0,0 +1 @@ +../api.func \ No newline at end of file diff --git a/api-renamed/login.js b/api-renamed/login.js index 5344a917..b5852982 100644 --- a/api-renamed/login.js +++ b/api-renamed/login.js @@ -7,12 +7,11 @@ import { authenticate } from "../src/users.js"; */ export default async (req, res) => { const { code, private_access, user_key } = req.query; - res.setHeader("Content-Type", "application/json"); try { - await authenticate(code, private_access === "true", user_key); + let userId = await authenticate(code, private_access === "true", user_key); + res.send(userId); } catch (err) { logger.error(err); res.send("Something went wrong: " + err.message); } - res.send("ok"); }; diff --git a/api-renamed/logout.js b/api-renamed/logout.js new file mode 100644 index 00000000..4fb93910 --- /dev/null +++ b/api-renamed/logout.js @@ -0,0 +1,17 @@ +import { logger } from "../src/common/utils.js"; +import { logout } from "../src/users.js"; + +/** + * @param {any} req The request. + * @param {any} res The response. + */ +export default async (req, res) => { + const { user_key } = req.query; + try { + await logout(user_key); + } catch (err) { + logger.error(err); + res.send("Something went wrong: " + err.message); + } + res.send("ok"); +}; diff --git a/src/common/database.js b/src/common/database.js index b7866fbb..293b3e63 100644 --- a/src/common/database.js +++ b/src/common/database.js @@ -7,8 +7,8 @@ export const pool = process.env.POSTGRES_URL }) : null; -/* - * Creates all required tables if they do not exist +/** + * Creates all required tables if they do not exist. */ async function createAllTables() { if (!pool) { @@ -27,10 +27,6 @@ async function createAllTables() { user_key TEXT, private_access BOOLEAN NOT NULL DEFAULT false ); - -- CREATE TABLE IF NOT EXISTS code_key_map ( - -- code TEXT PRIMARY KEY, - -- user_key TEXT NOT NULL - -- ); `); } @@ -88,8 +84,7 @@ export async function deleteOldRequests() { result = await pool.query(deleteQuery); } catch (err) { if (err.code === "42P01") { - await createAllTables(); - result = await pool.query(deleteQuery); + console.log("Error deleting requests, table doesn't exist"); } else { throw err; } @@ -118,8 +113,7 @@ export async function getRecentRequests() { ({ rows } = await pool.query(query)); } catch (err) { if (err.code === "42P01") { - await createAllTables(); - ({ rows } = await pool.query(query)); + console.log("Error fetching requests, table doesn't exist"); } else { throw err; } @@ -172,60 +166,28 @@ export async function storeUser(userId, accessToken, userKey, privateAccess) { } /** - * Stores or updates an Oauth code/userKey pair in the database. - * @param {string} code OAuth code - * @param {string} userKey userKey to associate + * Delete a user from the database. + * + * @param userKey user key of the user which is to be deleted. */ -/* -export async function storeCodeKey(code, userKey) { +export async function deleteUser(userKey) { if (!pool) { return; } - const insertQuery = ` - INSERT INTO code_key_map (code, user_key) - VALUES ($1, $2) - ON CONFLICT (code) - DO UPDATE SET user_key = EXCLUDED.user_key - `; + const deleteQuery = ` + DELETE FROM authenticated_users + WHERE user_key = $1 + `; + let result; try { - await pool.query(insertQuery, [code, userKey]); + result = await pool.query(deleteQuery, [userKey]); } catch (err) { if (err.code === "42P01") { - await createAllTables(); - await pool.query(insertQuery, [code, userKey]); + console.log("Error deleting user, table doesn't exist"); } else { throw err; } + console.log(`Deleted ${result.rowCount} user(s).`); } } -*/ - -/** - * Retrieves the userKey for a given Oauth code from the database. - * @param {string} code OAuth code - * @returns {Promise} userKey or null if not found - */ -/* -export async function getCodeKey(code) { - if (!pool) { - return null; - } - - const query = ` - SELECT user_key FROM code_key_map WHERE code = $1 - `; - let rows; - try { - ({ rows } = await pool.query(query, [code])); - } catch (err) { - if (err.code === "42P01") { - await createAllTables(); - ({ rows } = await pool.query(query, [code])); - } else { - throw err; - } - } - return rows.length > 0 ? rows[0].user_key : null; -} -*/ diff --git a/src/users.js b/src/users.js index e076f41a..93a1b8c8 100644 --- a/src/users.js +++ b/src/users.js @@ -1,5 +1,5 @@ import axios from "axios"; -import { storeUser } from "./common/database.js"; +import { deleteUser, storeUser } from "./common/database.js"; /** * Set user key for a given code @@ -90,15 +90,26 @@ async function githubAuthenticate(code) { throw err; } } + /** * Authenticate using the OAuth code and update DB with associated user info. * * @param {string} code GitHub authentication code from OAuth process * @param {boolean} privateAccess whether private access was requested * @param {string} userKey user key to associate with the user + * @returns {Promise} user_id of authenticated user */ export async function authenticate(code, privateAccess, userKey) { const { userId, accessToken } = await githubAuthenticate(code); - // const userKey = await getCodeKey(code); await storeUser(userId, accessToken, userKey, privateAccess); + return userId; } + +/** + * Delete an authenticated user from the database. + * + * @param userKey user key of the user which is to be deleted. + */ +export async function logout(userKey) { + await deleteUser(userKey); +} \ No newline at end of file