Creates a new authenticated session
Initializes a session with the provided authentication context. The session manages the current user's identity, organization membership, and access permissions.
Session data including: - user: Current user details - org: Active organization - org_user: Organization membership - client: OAuth client context - permissions: Access permissions
Basic user session:
const session = new Session({
user: {
id: 'user-123',
name: 'Jane Developer',
email: 'jane@example.com'
}
});
Full organization context:
const session = new Session({
user: {
id: 'user-123',
name: 'Jane Developer'
},
org: {
id: 'org-456',
name: 'Tech Corp'
},
org_user: {
permission: 'admin'
},
client: {
id: 'client-789',
name: 'Web Dashboard'
},
permissions: {
canManageUsers: true,
canManageApps: true
}
});
Get the current authenticated user
This getter provides access to the current user's profile and identity information. It returns a User instance that can be used to access and manage user-specific data.
User instance for the authenticated user, or null if not available
Basic user access:
const user = session.user;
if (user) {
console.log(`Authenticated as: ${user.name}`);
console.log(`Email: ${user.email}`);
console.log(`Status: ${user.isActive() ? 'Active' : 'Inactive'}`);
} else {
console.log('Not authenticated');
}
Profile management:
async function updateUserProfile(session: Session) {
const user = session.user;
if (!user) {
throw new Error('Not authenticated');
}
// Update profile
user.update({
name: 'Updated Name',
metadata: {
title: 'Senior Developer',
department: 'Engineering'
}
});
await user.save();
console.log('Profile updated successfully');
}
Set the current authenticated user
This setter updates the current user context in the session. It's typically used when switching users or updating user information after authentication changes.
Complete user data including: - id: User's unique identifier - name: User's full name - email: User's email address - metadata: Additional user data
Basic user switch:
session.user = {
id: 'user-123',
name: 'New User',
email: 'new@example.com'
};
Detailed user context:
session.user = {
id: 'user-456',
name: 'Jane Smith',
email: 'jane@example.com',
metadata: {
title: 'Engineering Manager',
department: 'R&D',
location: 'San Francisco',
timezone: 'America/Los_Angeles'
}
};
// Verify switch
const user = session.user;
console.log(`Switched to: ${user.name}`);
Get the current active organization
This getter provides access to the current organization context in the session. It returns an Organization instance that can be used to access and manage organization-specific resources.
Organization instance for active organization, or null if not available
Basic organization access:
const org = session.org;
if (org) {
console.log(`Organization: ${org.name}`);
console.log(`Description: ${org.short_description}`);
console.log(`Status: ${org.isActive() ? 'Active' : 'Inactive'}`);
} else {
console.log('No organization context');
}
Resource management:
async function manageOrgResources(session: Session) {
const org = session.org;
if (!org) {
throw new Error('No organization context');
}
// Access organization resources
const [agents, apps, models] = await Promise.all([
org.agents.get(),
org.apps.get(),
org.models.get()
]);
console.log('Organization Resources:');
console.log(`- ${agents.length} AI agents`);
console.log(`- ${apps.length} applications`);
console.log(`- ${models.length} models`);
}
Set the current active organization
This setter updates the current organization context in the session. It's typically used when switching between organizations or updating organization information.
Complete organization data including: - id: Organization's unique identifier - name: Organization name - short_description: Brief description - metadata: Additional organization data
Basic organization switch:
session.org = {
id: 'org-123',
name: 'New Organization',
short_description: 'Updated context'
};
Detailed organization context:
session.org = {
id: 'org-456',
name: 'Enterprise Corp',
short_description: 'Global enterprise solutions',
long_description: 'Leading provider of enterprise AI solutions',
metadata: {
industry: 'technology',
size: 'enterprise',
region: 'global',
features: ['agents', 'apps', 'models']
}
};
// Update related context
session.orgUser = {
permission: 'admin',
metadata: {
role: 'organization-admin'
}
};
Get the current organization membership
This getter provides access to the current user's membership and role within the active organization. It returns an OrgUser instance that manages the relationship between user and organization.
OrgUser instance for current membership, or null if not available
Basic membership check:
const membership = session.orgUser;
if (membership) {
console.log(`Role: ${membership.permission}`);
console.log(`Active: ${membership.isActive()}`);
} else {
console.log('No organization membership');
}
Permission management:
async function checkAccess(session: Session) {
const membership = session.orgUser;
if (!membership) {
throw new Error('No organization access');
}
// Check permissions
const perms = session.permissions;
if (perms?.canManageUsers) {
// Manage team
const team = await membership.orgs.get();
console.log('Team Members:');
team.forEach(member => {
console.log(`- ${member.user.name} (${member.permission})`);
});
} else {
console.log('Insufficient permissions');
}
}
Set the current organization membership
This setter updates the current user's membership and role within the active organization. It's typically used when switching roles or updating membership details.
Complete membership data including: - org: Organization reference - user: User reference - permission: Access level - metadata: Additional membership data
Basic role update:
session.orgUser = {
org: 'org-123',
user: 'user-456',
permission: 'admin'
};
Detailed membership update:
session.orgUser = {
org: 'org-123',
user: 'user-456',
permission: 'member',
metadata: {
department: 'engineering',
title: 'Senior Developer',
start_date: new Date().toISOString(),
access_level: 'full',
teams: ['frontend', 'platform']
}
};
// Verify membership
const membership = session.orgUser;
if (membership?.isActive()) {
console.log('Membership updated successfully');
}
Get the current OAuth client
This getter provides access to the current OAuth client context in the session. It returns a Client instance that manages authentication and authorization for external applications.
Client instance for current OAuth client, or null if not available
Basic client access:
const client = session.client;
if (client) {
console.log(`Client: ${client.name}`);
console.log(`ID: ${client.client_id}`);
console.log(`Status: ${client.isActive() ? 'Active' : 'Inactive'}`);
} else {
console.log('No client context');
}
Client verification:
async function verifyClient(session: Session) {
const client = session.client;
if (!client) {
throw new Error('No client context');
}
// Check client configuration
console.log('Client Configuration:');
console.log(`Name: ${client.name}`);
console.log(`ID: ${client.client_id}`);
console.log(`Redirect URIs: ${client.redirect_uris.join(', ')}`);
console.log(`Scopes: ${client.scopes.join(', ')}`);
if (client.metadata?.type === 'service-account') {
console.log('Service Account Details:');
console.log(`Service: ${client.metadata.service}`);
console.log(`Environment: ${client.metadata.environment}`);
}
}
Set the current OAuth client
This setter updates the current OAuth client context in the session. It's typically used when switching clients or updating client configuration for authentication.
Complete client data including: - id: Client's unique identifier - name: Client application name - client_id: OAuth client ID - client_secret: OAuth client secret - redirect_uris: Authorized redirect URIs - scopes: Authorized scopes - metadata: Additional client data
Basic client update:
session.client = {
id: 'client-123',
name: 'Web Dashboard',
client_id: 'client-id',
redirect_uris: ['https://app.example.com/callback']
};
Service account setup:
session.client = {
id: 'client-456',
name: 'Background Service',
client_id: process.env.SERVICE_CLIENT_ID,
client_secret: process.env.SERVICE_CLIENT_SECRET,
grant_types: ['client_credentials'],
scopes: ['service:full'],
metadata: {
type: 'service-account',
service: 'data-processor',
environment: 'production',
rate_limit: 1000
}
};
// Verify client
const client = session.client;
if (client?.isActive()) {
console.log('Client configured successfully');
}
Get the current session permissions
This getter provides access to the current session's permission set. It returns a permissions object that defines what actions the current user can perform within the active organization context.
Permission object defining allowed actions, or null if not available
Basic permission check:
const perms = session.permissions;
if (perms) {
console.log('User Permissions:');
console.log(`- Manage Users: ${perms.canManageUsers}`);
console.log(`- Manage Apps: ${perms.canManageApps}`);
console.log(`- Manage Models: ${perms.canManageModels}`);
} else {
console.log('No permission data available');
}
Permission-based operations:
async function performAdminTask(session: Session) {
const perms = session.permissions;
if (!perms) {
throw new Error('No permission data');
}
// Check admin capabilities
const canManageTeam = perms.canManageUsers;
const canConfigureApps = perms.canManageApps;
const canDeployModels = perms.canManageModels;
if (canManageTeam && canConfigureApps && canDeployModels) {
console.log('Full administrative access');
// Perform admin tasks
const org = session.org;
if (org) {
const [users, apps, models] = await Promise.all([
org.orgs.get(),
org.apps.get(),
org.models.get()
]);
console.log('Resource Summary:');
console.log(`- ${users.length} team members`);
console.log(`- ${apps.length} applications`);
console.log(`- ${models.length} AI models`);
}
} else {
console.log('Limited permissions:');
console.log(`- Team management: ${canManageTeam}`);
console.log(`- App configuration: ${canConfigureApps}`);
console.log(`- Model deployment: ${canDeployModels}`);
}
}
Check if the entity is active
This method checks the active status of the entity. Most entities in the system can be active or inactive, which affects their availability and usability in the platform.
True if the entity is active, false otherwise
Convert model instance to interface data
This method serializes the model instance to a plain object that matches the interface type. This is useful for:
The model data as a plain object matching the interface type
Convert model instance to API payload
This method creates a payload suitable for API requests by:
A clean object suitable for API requests
Update model data with new values
This method updates the model's data and instance properties with new values. It performs a shallow merge of the updates with existing data, allowing for partial updates of the model's properties.
Object containing properties to update
const user = new User({
email: 'old@example.com',
firstName: 'John'
});
// Update multiple properties
user.update({
email: 'new@example.com',
lastName: 'Doe'
});
// Save changes to API
await user.save();
This method only updates the local model instance. To persist changes to the API, call save after updating.
Save the model instance to the API
This method persists the current state of the model to the API using a PUT request. It requires the model to have an ID (existing instance). For new instances, use the collection's create method instead.
The method:
Promise resolving to the updated model data
Delete the model instance from the API
This method permanently deletes the model instance from the API and clears the local data. This operation cannot be undone.
The method:
Promise that resolves when deletion is successful
Session class for managing authenticated contexts
This class represents an authenticated session in the Mosaia platform. It manages the current context including user identity, organization membership, client information, and permissions.
Features:
Remarks
Sessions provide access to:
The session maintains the active context for:
Example
Basic session usage:
Example
Permission checking: