diff --git a/package-lock.json b/package-lock.json index 0e8e03a..c3b9610 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,6 +38,7 @@ "chalk": "^5.0.1", "cookie-parser": "^1.4.6", "date-fns": "^2.28.0", + "envfile": "^6.17.0", "eslint": "^8.12.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-svelte3": "^4.0.0", @@ -1904,6 +1905,21 @@ "node": ">= 0.6" } }, + "node_modules/envfile": { + "version": "6.17.0", + "resolved": "https://registry.npmjs.org/envfile/-/envfile-6.17.0.tgz", + "integrity": "sha512-RnhtVw3auDZeeh5VtaNrbE7s6Kq8BoRtGIzcbMpMsJ+wIpRgs5jiDG4gQjW+vfws5QPlizE57/fUU0Tj6Nrs8A==", + "dev": true, + "bin": { + "envfile": "bin.cjs" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://bevry.me/fund" + } + }, "node_modules/es6-promise": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz", @@ -7674,6 +7690,12 @@ "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.4.tgz", "integrity": "sha512-+nVFp+5z1E3HcToEnO7ZIj3g+3k9389DvWtvJZz0T6/eOCPIyyxehFcedoYrZQrp0LgQbD9pPXhpMBKMd5QURg==" }, + "envfile": { + "version": "6.17.0", + "resolved": "https://registry.npmjs.org/envfile/-/envfile-6.17.0.tgz", + "integrity": "sha512-RnhtVw3auDZeeh5VtaNrbE7s6Kq8BoRtGIzcbMpMsJ+wIpRgs5jiDG4gQjW+vfws5QPlizE57/fUU0Tj6Nrs8A==", + "dev": true + }, "es6-promise": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz", diff --git a/package.json b/package.json index 00cc644..519e6f8 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "chalk": "^5.0.1", "cookie-parser": "^1.4.6", "date-fns": "^2.28.0", + "envfile": "^6.17.0", "eslint": "^8.12.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-svelte3": "^4.0.0", diff --git a/src/ao-lib/api.js b/src/ao-lib/api.js index d5c9711..5a708d5 100644 --- a/src/ao-lib/api.js +++ b/src/ao-lib/api.js @@ -3,11 +3,11 @@ import { v1 as uuidV1 } from 'uuid' import { io } from 'socket.io-client' import { createHash, hmacHex } from './crypto.js' import { isObject } from './util.js' -import { aoEnv } from './settings.js' +//import { aoEnv } from './settings.js' // The AO API server endpoint this ao-cli client will attempt to connect to export const AO_DEFAULT_HOSTNAME = 'localhost:8003' -const HOSTNAME = aoEnv('AO_CLI_TARGET_HOSTNAME') || AO_DEFAULT_HOSTNAME +const HOSTNAME = /*aoEnv('AO_CLI_TARGET_HOSTNAME') ||*/ AO_DEFAULT_HOSTNAME const [HOST, PORT] = HOSTNAME.split(':') // The AO API server websocket endpoint this ao-cli client will attempt to connect to @@ -20,9 +20,9 @@ export const socket = io(AO_SOCKET_URL, { export let socketStatus // Load the current session cookies from the AO .env file -let currentMemberId = aoEnv('AO_CLI_SESSION_MEMBERID') -let currentSessionId = aoEnv('AO_CLI_SESSION_ID') -let currentSessionToken = aoEnv('AO_CLI_SESSION_TOKEN') +let currentMemberId = '13844ae0-2270-11ea-b45c-83ea6e9b1aa1' //aoEnv('AO_CLI_SESSION_MEMBERID') +let currentSessionId = 'c5c9ccd0-f28d-11ec-9614-15596c298860' //aoEnv('AO_CLI_SESSION_ID') +let currentSessionToken = '51ad3b959b89d859786e232c72cda792ce5e99ff9dbb639d24f8ae45a70ff753' //aoEnv('AO_CLI_SESSION_TOKEN') // Performs a GET request to the specified endpoint, sending the given payload export async function getRequest(endpoint, payload = null, alternateHost = null, verbose = true) { @@ -49,6 +49,7 @@ export async function postRequest(endpoint, payload = null, verbose = true) { } try { if(payload) { + console.log("about to post to", HOSTNAME, currentSessionToken) return await request .post(HOSTNAME + endpoint) .send(payload) @@ -70,6 +71,25 @@ export async function postEvent(event, verbose) { return await postRequest('/events', event, verbose) } +// Attempts login with a new guest session. If successful, returns the generated session and token (login cookies). +// Guest sessions be be convertable to permanent sessions by setting a password on them +export async function createGuestSession() { + const session = uuidV1() + //const user = 'anon' + let sessionKey = createHash(session) + const token = hmacHex(session, sessionKey) + const result = await request + .post(HOSTNAME + '/session') + .set('authorization', token) + .set('session', session) + //.set('name', user) + .on('error', () => false) + currentMemberId = result.body.memberId + currentSessionToken = token + currentSessionId = session + return { session, token, memberId: currentMemberId } +} + // Attempts login with the given username and password combo. If successful, returns the generated session and token (login cookies). export async function createSession(user, pass) { const session = uuidV1() @@ -281,18 +301,22 @@ export async function cacheMeme(taskId) { // Cards feature // Returns the card and other cards as specified by the alsoGetRelevant arg // If multiple cards are returned, they will be returned in their global deck order (global creation order on server) -export async function getCard(taskId, alsoGetRelevant = 'subcards') { +export async function getCard(taskId, alsoGetRelevant = 'subcards', verbose = true) { + console.log('about to getCard, taskId: ', taskId) taskId = taskId.trim().toLowerCase() - let payload = { taskId: taskId } - const result = await postRequest('/fetchTaskByID', payload, false) // todo: change to flat text, not JSON (?) + let payload = { taskIds: [ taskId ] } + console.log('payload', payload) + const result = await postRequest('/fetchTasks', payload, verbose) // todo: change to flat text, not JSON (?) + console.log('result', result) if(!result || !result.body) { - //console.log('Error fetching task.') + console.log('Error fetching task.') return null } if(alsoGetRelevant) { let relevantCards = await getAllRelevantCards(result.body, alsoGetRelevant) return [result.body, ...relevantCards] } + console.log('result', result.body) return [result.body] } diff --git a/src/routes/api/index.json.js b/src/routes/api/index.json.js index 02bd70f..b49070d 100644 --- a/src/routes/api/index.json.js +++ b/src/routes/api/index.json.js @@ -1,6 +1,6 @@ export async function get() { return { status: 200, - body: 'Hello World! ' + process.env['NODE_AO_DB_FILE'], + body: 'Hello, World! ' + process.env['NODE_AO_DB_FILE'], } } diff --git a/src/routes/index.svelte b/src/routes/index.svelte index d55ecc9..cd836e1 100644 --- a/src/routes/index.svelte +++ b/src/routes/index.svelte @@ -10,12 +10,15 @@ } } } + import { createGuestSession } from '../ao-lib/api.js' + + const sessionId = createGuestSession()