add logout, small improvements

This commit is contained in:
martin-mfg
2025-09-04 12:13:00 +02:00
parent 1fc7833ff3
commit ffe307e878
5 changed files with 49 additions and 59 deletions
+1
View File
@@ -0,0 +1 @@
../api.func
+2 -3
View File
@@ -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");
};
+17
View File
@@ -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");
};
+16 -54
View File
@@ -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<string|null>} 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;
}
*/
+13 -2
View File
@@ -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<string>} 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);
}