Files
upstream-rocketchat/apps/meteor/client/lib/RestApiClient.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

73 lines
2.6 KiB
TypeScript

/* eslint-disable react-hooks/rules-of-hooks */
import { RestClient } from '@rocket.chat/api-client';
import { invokeTwoFactorModal } from './2fa/process2faReturn';
import { baseURI } from './baseURI';
import { clearStoredCredentials } from './sdk/ddpSdk';
import { STORAGE_KEYS, getStoredItem } from './sdk/storage';
class RestApiClient extends RestClient {
override getCredentials():
| {
'X-User-Id': string;
'X-Auth-Token': string;
}
| undefined {
const [uid, token] = [getStoredItem(STORAGE_KEYS.USER_ID), getStoredItem(STORAGE_KEYS.LOGIN_TOKEN)];
if (!uid || !token) {
return;
}
return {
'X-User-Id': uid,
'X-Auth-Token': token,
};
}
}
export const APIClient = new RestApiClient({
baseUrl: baseURI.replace(/\/$/, ''),
});
APIClient.handleTwoFactorChallenge(invokeTwoFactorModal);
/**
* The original rest api code throws the Response object, which is very useful
* for the client sometimes, if the developer wants to access more information about the error
* unfortunately/fortunately Rocket.Chat expects an error object (from Response.json()
* This middleware will throw the error object instead.
* */
/**
* A 401 means the request reached an authRequired route without a session the server recognizes.
* That is only evidence of an expired session when the request was an action the user just took:
* boot-time reads (OmnichannelProvider's livechat/config/routing, custom-sounds.list) fire before
* the session is established and answer 401 with the same message, and clearing on those logs out
* a session that is coming up. Restricting the wipe to mutations mirrors the DDP path this replaced
* — ddpOverREST only clears credentials for method calls, never for background fetches — so a
* session that dies while idle is noticed on the user's next write, exactly as before.
*/
const isMutation = (method: string): boolean => method === 'POST' || method === 'PUT' || method === 'DELETE';
APIClient.use(async (request, next) => {
const [, method] = request;
const tokenAtSend = getStoredItem(STORAGE_KEYS.LOGIN_TOKEN);
try {
return await next(...request);
} catch (error) {
if (error instanceof Response) {
// Never 403 (authenticated but lacking permission) — that must not log the user out.
// The token comparison keeps a request that was in flight across a re-login from
// evicting the session it does not belong to.
if (error.status === 401 && isMutation(method) && tokenAtSend && tokenAtSend === getStoredItem(STORAGE_KEYS.LOGIN_TOKEN)) {
clearStoredCredentials();
}
const e = await error.json();
throw e;
}
throw error;
}
});