AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { isNil } from '@activepieces/core-utils';
import {
AuthenticationResponse,
Principal,
PrincipalType,
} from '@activepieces/shared';
import dayjs from 'dayjs';
import { jwtDecode } from 'jwt-decode';
import { authenticationApi } from '@/api/authentication-api';
import { queryClient } from '@/app/query-client';
import { ApStorage } from './ap-browser-storage';
import { telemetryUtils } from './telemetry-utils';
const tokenKey = 'token';
const projectIdKey = 'projectId';
export const authenticationSession = {
setProjectId(projectId: string) {
ApStorage.getInstance().setItem(projectIdKey, projectId);
},
saveResponse(response: AuthenticationResponse, isEmbedding: boolean) {
if (isEmbedding) {
ApStorage.setInstanceToSessionStorage();
}
ApStorage.getInstance().setItem(tokenKey, response.token);
saveProjectIdOrClear(response.projectId);
queryClient.invalidateQueries({ queryKey: ['flags'] });
window.dispatchEvent(new Event('storage'));
},
isJwtExpired(token: string): boolean {
if (!token) {
return true;
}
try {
const decoded = jwtDecode(token);
if (decoded && decoded.exp && dayjs().isAfter(dayjs.unix(decoded.exp))) {
return true;
}
return false;
} catch (e) {
return true;
}
},
getToken(): string | null {
return ApStorage.getInstance().getItem(tokenKey) ?? null;
},
getProjectId(): string | null {
const token = this.getToken();
if (isNil(token)) {
return null;
}
const projectId = ApStorage.getInstance().getItem(projectIdKey);
if (!isNil(projectId)) {
return projectId;
}
const decodedJwt = getDecodedJwt(token);
if ('projectId' in decodedJwt && typeof decodedJwt.projectId === 'string') {
return decodedJwt.projectId;
}
return null;
},
getCurrentUserId(): string | null {
const token = this.getToken();
if (isNil(token)) {
return null;
}
const decodedJwt = getDecodedJwt(token);
return decodedJwt.id;
},
appendProjectRoutePrefix(path: string): string {
const projectId = this.getProjectId();
if (isNil(projectId)) {
return path;
}
return `/projects/${projectId}${path.startsWith('/') ? path : `/${path}`}`;
},
getPlatformId(): string | null {
const token = this.getToken();
if (isNil(token)) {
return null;
}
const decodedJwt = getDecodedJwt(token);
if ('platform' in decodedJwt && decodedJwt.platform) {
return decodedJwt.platform.id;
}
return null;
},
isOnboarding(): boolean {
const token = this.getToken();
if (isNil(token)) {
return false;
}
const decodedJwt = jwtDecode<{ type: string }>(token);
return decodedJwt.type === PrincipalType.ONBOARDING;
},
async switchToPlatform(platformId: string) {
if (authenticationSession.getPlatformId() === platformId) {
return;
}
const result = await authenticationApi.switchPlatform({
platformId,
});
ApStorage.getInstance().setItem(tokenKey, result.token);
saveProjectIdOrClear(result.projectId);
window.location.href = '/';
},
switchToProject(projectId: string) {
if (authenticationSession.getProjectId() === projectId) {
return;
}
ApStorage.getInstance().setItem(projectIdKey, projectId);
window.dispatchEvent(new Event('storage'));
},
isLoggedIn(): boolean {
const token = this.getToken();
if (isNil(token)) {
return false;
}
return !this.isJwtExpired(token);
},
clearSession() {
ApStorage.getInstance().removeItem(projectIdKey);
ApStorage.getInstance().removeItem(tokenKey);
},
logOut() {
const hadSession = !isNil(this.getToken());
this.clearSession();
if (hadSession) {
telemetryUtils.resetIdentity();
}
window.location.href = '/sign-in';
},
};
function getDecodedJwt(token: string): Principal {
return jwtDecode<Principal>(token);
}
function saveProjectIdOrClear(projectId: string | null | undefined): void {
if (isNil(projectId)) {
ApStorage.getInstance().removeItem(projectIdKey);
return;
}
ApStorage.getInstance().setItem(projectIdKey, projectId);
}