feat: track Memind product analytics
This commit is contained in:
@@ -21,6 +21,7 @@ export function createPortalApiAuthMiddleware({
|
||||
getLegacySessionToken = () => null,
|
||||
isPageDataPublicPath = () => false,
|
||||
isLegacyPageDataApiPath = () => false,
|
||||
isProductAnalyticsPublicPath = () => false,
|
||||
accessPolicyMode = PORTAL_ACCESS_POLICY_MODE.OFF,
|
||||
accessEnforcementConfig = Object.freeze({
|
||||
masterEnabled: false,
|
||||
@@ -99,13 +100,24 @@ export function createPortalApiAuthMiddleware({
|
||||
|
||||
const plazaPublic = isPortalPlazaOptionalUserPath(req.path, req.method);
|
||||
const pageDataPublic = isPageDataPublicPath(req.path, req.method);
|
||||
const productAnalyticsPublic = isProductAnalyticsPublicPath(
|
||||
req.path,
|
||||
req.method,
|
||||
);
|
||||
// The retired namespace must reach its explicit 410 route instead of
|
||||
// being converted into a misleading global 401/403 response.
|
||||
const legacyPageDataApi = isLegacyPageDataApiPath(req.path);
|
||||
|
||||
if (userAuth && tkmindProxy) {
|
||||
if (req.userSessionError) {
|
||||
if (plazaPublic || pageDataPublic || legacyPageDataApi) return next();
|
||||
if (
|
||||
plazaPublic ||
|
||||
pageDataPublic ||
|
||||
legacyPageDataApi ||
|
||||
productAnalyticsPublic
|
||||
) {
|
||||
return next();
|
||||
}
|
||||
return res.status(503).json({ message: '用户认证服务不可用,请稍后重试' });
|
||||
}
|
||||
try {
|
||||
@@ -113,7 +125,14 @@ export function createPortalApiAuthMiddleware({
|
||||
const me = await userAuth.getMe(req.userToken);
|
||||
if (me) req.currentUser = me;
|
||||
}
|
||||
if (plazaPublic || pageDataPublic || legacyPageDataApi) return next();
|
||||
if (
|
||||
plazaPublic ||
|
||||
pageDataPublic ||
|
||||
legacyPageDataApi ||
|
||||
productAnalyticsPublic
|
||||
) {
|
||||
return next();
|
||||
}
|
||||
if (!req.userSession) {
|
||||
return res.status(401).json({ message: '未授权,请重新登录' });
|
||||
}
|
||||
@@ -130,6 +149,7 @@ export function createPortalApiAuthMiddleware({
|
||||
}
|
||||
}
|
||||
|
||||
if (productAnalyticsPublic) return next();
|
||||
if (legacyAuth?.verify(getLegacySessionToken(req))) return next();
|
||||
return res.status(401).json({ message: '未授权,请重新登录' });
|
||||
};
|
||||
|
||||
@@ -57,6 +57,8 @@ function createMiddleware(overrides = {}) {
|
||||
return createPortalApiAuthMiddleware({
|
||||
isPageDataPublicPath,
|
||||
isLegacyPageDataApiPath,
|
||||
isProductAnalyticsPublicPath: (path, method) =>
|
||||
method === 'GET' && path === '/analytics/context',
|
||||
logger: createLogger(),
|
||||
...overrides,
|
||||
});
|
||||
@@ -143,6 +145,36 @@ test('multi-user public routes preserve optional user hydration', async () => {
|
||||
assert.equal(getMeCalls, 1);
|
||||
});
|
||||
|
||||
test('product analytics context preserves optional user hydration', async () => {
|
||||
let getMeCalls = 0;
|
||||
const user = { id: 'analytics-user' };
|
||||
const middleware = createMiddleware({
|
||||
getUserAuth: () => ({
|
||||
async getMe(token) {
|
||||
getMeCalls += 1;
|
||||
assert.equal(token, 'user-token');
|
||||
return user;
|
||||
},
|
||||
}),
|
||||
getTkmindProxy: () => ({}),
|
||||
});
|
||||
|
||||
const anonymous = await invoke(middleware, {
|
||||
path: '/analytics/context',
|
||||
userSession: null,
|
||||
});
|
||||
assert.equal(anonymous.nextCalls, 1);
|
||||
assert.equal(anonymous.req.currentUser, undefined);
|
||||
|
||||
const authenticated = await invoke(middleware, {
|
||||
path: '/analytics/context',
|
||||
userSession: { id: 'session-analytics' },
|
||||
});
|
||||
assert.equal(authenticated.nextCalls, 1);
|
||||
assert.equal(authenticated.req.currentUser, user);
|
||||
assert.equal(getMeCalls, 1);
|
||||
});
|
||||
|
||||
test('multi-user private routes preserve login, expiry, and double-check behavior', async () => {
|
||||
let currentUser = { id: 'user-1' };
|
||||
let getMeCalls = 0;
|
||||
|
||||
Reference in New Issue
Block a user