Skip to content

Commit

Permalink
ref: use pouchdb-session-authentication for session
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpascal committed May 30, 2024
1 parent 00825e3 commit 6b6cdd3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 81 deletions.
89 changes: 89 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"pouchdb-adapter-http": "^7.2.2",
"pouchdb-core": "^7.2.2",
"pouchdb-mapreduce": "^7.2.2",
"pouchdb-session-authentication": "^1.2.0",
"properties": "^1.2.1",
"queue-promise": "^2.2.1",
"readline-sync": "^1.4.10",
Expand Down
7 changes: 6 additions & 1 deletion src/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const log = require('./log');
const url = require('url');

const cache = new Map();
const sessionCookieName = 'AuthSession';

// Helper function to create request headers with session token (if available)
const withSessionCookie = (...args) => {
Expand All @@ -19,7 +20,11 @@ const withSessionCookie = (...args) => {

const sessionToken = environment.sessionToken;
if (sessionToken || options.headers) {
options.headers = Object.assign({}, options.headers || {}, { Cookie: sessionToken });
options.headers = Object.assign(
{},
options.headers || {},
{ Cookie: `${sessionCookieName}=${sessionToken}` }
);
}

return options;
Expand Down
30 changes: 2 additions & 28 deletions src/lib/db.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,18 @@
const { Headers } = require('cross-fetch');
const PouchDB = require('pouchdb-core');
PouchDB.plugin(require('pouchdb-adapter-http'));
PouchDB.plugin(require('pouchdb-mapreduce'));
PouchDB.plugin(require('pouchdb-session-authentication'));

const ArchivingDB = require('./archiving-db');
const environment = require('./environment');

const sessionCookieAwareFetch = () => (url, opts = {}) => {
const sessionToken = environment.sessionToken;

if (sessionToken) {
const setHeader = (headers, name, value) => {
if (headers instanceof Headers) {
headers.set(name, value);
} else if (Array.isArray(headers)) {
headers.push([name, value]);
} else if (typeof headers === 'object') {
headers[name] = value;
}
};

// Ensure opts.headers exists
if (!opts.headers) {
opts.headers = new Headers();
}

// Set the 'Cookie' header
setHeader(opts.headers, 'Cookie', sessionToken);
}

return PouchDB.fetch(url, opts);
};

module.exports = () => {
if (environment.isArchiveMode) {
return new ArchivingDB(environment.archiveDestination);
}

return new PouchDB(environment.apiUrl, {
ajax: { timeout: 60000 },
fetch: sessionCookieAwareFetch(),
session: environment.sessionToken,
});
};
52 changes: 0 additions & 52 deletions test/lib/db.spec.js

This file was deleted.

0 comments on commit 6b6cdd3

Please sign in to comment.