|
| 1 | +/** |
| 2 | + * Multi-Credential Authentication Example |
| 3 | + * |
| 4 | + * This example demonstrates how to use the multi-credential feature to manage |
| 5 | + * multiple provider credentials under a single connector. |
| 6 | + */ |
| 7 | + |
| 8 | +import Nylas from '../src/nylas'; |
| 9 | +import { CredentialType } from '../src/models/credentials'; |
| 10 | + |
| 11 | +const nylas = new Nylas({ |
| 12 | + apiKey: 'your-api-key', |
| 13 | +}); |
| 14 | + |
| 15 | +async function multiCredentialExample() { |
| 16 | + // Step 1: Create a connector (automatically creates a default credential) |
| 17 | + console.log('Step 1: Creating connector with default credentials...'); |
| 18 | + const connector = await nylas.connectors.create({ |
| 19 | + requestBody: { |
| 20 | + name: 'My Multi-Tenant Google Connector', |
| 21 | + provider: 'google', |
| 22 | + settings: { |
| 23 | + clientId: 'default-gcp-project-client-id', |
| 24 | + clientSecret: 'default-gcp-project-client-secret', |
| 25 | + topicName: 'default-topic', |
| 26 | + }, |
| 27 | + scope: ['https://www.googleapis.com/auth/gmail.readonly'], |
| 28 | + }, |
| 29 | + }); |
| 30 | + console.log('Connector created:', connector.data.provider); |
| 31 | + console.log('Active credential ID:', connector.data.activeCredentialId); |
| 32 | + |
| 33 | + // Step 2: Create additional credentials for different GCP projects |
| 34 | + console.log('\nStep 2: Creating additional credentials...'); |
| 35 | + |
| 36 | + const projectACredential = await nylas.connectors.credentials.create({ |
| 37 | + provider: 'google', |
| 38 | + requestBody: { |
| 39 | + name: 'GCP Project A', |
| 40 | + credentialType: CredentialType.CONNECTOR, |
| 41 | + credentialData: { |
| 42 | + clientId: 'project-a-client-id', |
| 43 | + clientSecret: 'project-a-client-secret', |
| 44 | + topicName: 'project-a-topic', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }); |
| 48 | + console.log('Created credential for Project A:', projectACredential.data.id); |
| 49 | + |
| 50 | + const projectBCredential = await nylas.connectors.credentials.create({ |
| 51 | + provider: 'google', |
| 52 | + requestBody: { |
| 53 | + name: 'GCP Project B', |
| 54 | + credentialType: CredentialType.CONNECTOR, |
| 55 | + credentialData: { |
| 56 | + clientId: 'project-b-client-id', |
| 57 | + clientSecret: 'project-b-client-secret', |
| 58 | + topicName: 'project-b-topic', |
| 59 | + }, |
| 60 | + }, |
| 61 | + }); |
| 62 | + console.log('Created credential for Project B:', projectBCredential.data.id); |
| 63 | + |
| 64 | + // Step 3: List all credentials for this connector |
| 65 | + console.log('\nStep 3: Listing all credentials...'); |
| 66 | + const credentialsList = await nylas.connectors.credentials.list({ |
| 67 | + provider: 'google', |
| 68 | + }); |
| 69 | + console.log(`Total credentials: ${credentialsList.data.length}`); |
| 70 | + |
| 71 | + // Step 4: Use specific credential in Hosted OAuth flow |
| 72 | + console.log('\nStep 4: Generating hosted auth URL with Project A credentials...'); |
| 73 | + const authUrlProjectA = nylas.auth.urlForOAuth2({ |
| 74 | + clientId: 'your-nylas-app-client-id', |
| 75 | + redirectUri: 'https://your-app.com/callback', |
| 76 | + provider: 'google', |
| 77 | + scope: ['https://www.googleapis.com/auth/gmail.readonly'], |
| 78 | + credentialId: projectACredential.data.id, // Use Project A credentials |
| 79 | + }); |
| 80 | + console.log('Auth URL for Project A:', authUrlProjectA); |
| 81 | + |
| 82 | + // Step 5: Use different credential for another user |
| 83 | + console.log('\nStep 5: Generating hosted auth URL with Project B credentials...'); |
| 84 | + const authUrlProjectB = nylas.auth.urlForOAuth2({ |
| 85 | + clientId: 'your-nylas-app-client-id', |
| 86 | + redirectUri: 'https://your-app.com/callback', |
| 87 | + provider: 'google', |
| 88 | + scope: ['https://www.googleapis.com/auth/gmail.readonly'], |
| 89 | + credentialId: projectBCredential.data.id, // Use Project B credentials |
| 90 | + }); |
| 91 | + console.log('Auth URL for Project B:', authUrlProjectB); |
| 92 | + |
| 93 | + // Step 6: Use credential with PKCE flow |
| 94 | + console.log('\nStep 6: Generating PKCE auth URL with specific credential...'); |
| 95 | + const pkceAuth = nylas.auth.urlForOAuth2PKCE({ |
| 96 | + clientId: 'your-nylas-app-client-id', |
| 97 | + redirectUri: 'https://your-app.com/callback', |
| 98 | + provider: 'google', |
| 99 | + scope: ['https://www.googleapis.com/auth/gmail.readonly'], |
| 100 | + credentialId: projectACredential.data.id, |
| 101 | + }); |
| 102 | + console.log('PKCE Auth URL:', pkceAuth.url); |
| 103 | + console.log('Secret (store this securely):', pkceAuth.secret); |
| 104 | + |
| 105 | + // Step 7: Custom authentication with specific credential |
| 106 | + console.log('\nStep 7: Using custom authentication with specific credential...'); |
| 107 | + const customGrant = await nylas.auth.customAuthentication({ |
| 108 | + requestBody: { |
| 109 | + provider: 'google', |
| 110 | + settings: { |
| 111 | + // Provider-specific settings |
| 112 | + refresh_token: 'user-refresh-token', |
| 113 | + credentialId: projectBCredential.data.id, // Use Project B credentials |
| 114 | + }, |
| 115 | + scope: ['https://www.googleapis.com/auth/gmail.readonly'], |
| 116 | + }, |
| 117 | + }); |
| 118 | + console.log('Grant created:', customGrant.data.id); |
| 119 | + console.log('Credential used:', customGrant.data.credentialId); |
| 120 | + |
| 121 | + // Step 8: Update connector to change default credential |
| 122 | + console.log('\nStep 8: Updating connector default credential...'); |
| 123 | + const updatedConnector = await nylas.connectors.update({ |
| 124 | + provider: 'google', |
| 125 | + requestBody: { |
| 126 | + activeCredentialId: projectACredential.data.id, // Set Project A as default |
| 127 | + }, |
| 128 | + }); |
| 129 | + console.log('Updated active credential:', updatedConnector.data.activeCredentialId); |
| 130 | + |
| 131 | + // Step 9: Use default credential (no credentialId specified) |
| 132 | + console.log('\nStep 9: Generating auth URL without specifying credential (uses default)...'); |
| 133 | + const defaultAuthUrl = nylas.auth.urlForOAuth2({ |
| 134 | + clientId: 'your-nylas-app-client-id', |
| 135 | + redirectUri: 'https://your-app.com/callback', |
| 136 | + provider: 'google', |
| 137 | + scope: ['https://www.googleapis.com/auth/gmail.readonly'], |
| 138 | + // No credentialId specified - will use activeCredentialId from connector |
| 139 | + }); |
| 140 | + console.log('Auth URL using default credential:', defaultAuthUrl); |
| 141 | + |
| 142 | + // Step 10: Cleanup - Delete a credential |
| 143 | + console.log('\nStep 10: Deleting Project B credential...'); |
| 144 | + await nylas.connectors.credentials.destroy({ |
| 145 | + provider: 'google', |
| 146 | + credentialsId: projectBCredential.data.id, |
| 147 | + }); |
| 148 | + console.log('Credential deleted successfully'); |
| 149 | +} |
| 150 | + |
| 151 | +// Microsoft Multi-Tenant Example |
| 152 | +async function microsoftMultiTenantExample() { |
| 153 | + console.log('\n=== Microsoft Multi-Tenant Example ===\n'); |
| 154 | + |
| 155 | + // Create Microsoft connector |
| 156 | + const msConnector = await nylas.connectors.create({ |
| 157 | + requestBody: { |
| 158 | + name: 'Multi-Tenant Microsoft Connector', |
| 159 | + provider: 'microsoft', |
| 160 | + settings: { |
| 161 | + clientId: 'default-tenant-client-id', |
| 162 | + clientSecret: 'default-tenant-client-secret', |
| 163 | + tenant: 'common', |
| 164 | + }, |
| 165 | + scope: ['https://graph.microsoft.com/Mail.Read'], |
| 166 | + }, |
| 167 | + }); |
| 168 | + console.log('Microsoft connector created'); |
| 169 | + |
| 170 | + // Create credential for Tenant A |
| 171 | + const tenantACredential = await nylas.connectors.credentials.create({ |
| 172 | + provider: 'microsoft', |
| 173 | + requestBody: { |
| 174 | + name: 'Tenant A', |
| 175 | + credentialType: CredentialType.CONNECTOR, |
| 176 | + credentialData: { |
| 177 | + clientId: 'tenant-a-client-id', |
| 178 | + clientSecret: 'tenant-a-client-secret', |
| 179 | + tenant: 'tenant-a-id', |
| 180 | + }, |
| 181 | + }, |
| 182 | + }); |
| 183 | + console.log('Created credential for Tenant A:', tenantACredential.data.id); |
| 184 | + |
| 185 | + // Create credential for Tenant B |
| 186 | + const tenantBCredential = await nylas.connectors.credentials.create({ |
| 187 | + provider: 'microsoft', |
| 188 | + requestBody: { |
| 189 | + name: 'Tenant B', |
| 190 | + credentialType: CredentialType.CONNECTOR, |
| 191 | + credentialData: { |
| 192 | + clientId: 'tenant-b-client-id', |
| 193 | + clientSecret: 'tenant-b-client-secret', |
| 194 | + tenant: 'tenant-b-id', |
| 195 | + }, |
| 196 | + }, |
| 197 | + }); |
| 198 | + console.log('Created credential for Tenant B:', tenantBCredential.data.id); |
| 199 | + |
| 200 | + // Use Tenant A credentials for auth |
| 201 | + const tenantAAuthUrl = nylas.auth.urlForOAuth2({ |
| 202 | + clientId: 'your-nylas-app-client-id', |
| 203 | + redirectUri: 'https://your-app.com/callback', |
| 204 | + provider: 'microsoft', |
| 205 | + scope: ['https://graph.microsoft.com/Mail.Read'], |
| 206 | + credentialId: tenantACredential.data.id, |
| 207 | + }); |
| 208 | + console.log('Auth URL for Tenant A:', tenantAAuthUrl); |
| 209 | + |
| 210 | + // Admin consent flow with specific credential |
| 211 | + const adminConsentUrl = nylas.auth.urlForAdminConsent({ |
| 212 | + clientId: 'your-nylas-app-client-id', |
| 213 | + redirectUri: 'https://your-app.com/callback', |
| 214 | + credentialId: tenantBCredential.data.id, |
| 215 | + }); |
| 216 | + console.log('Admin consent URL for Tenant B:', adminConsentUrl); |
| 217 | +} |
| 218 | + |
| 219 | +// Run examples |
| 220 | +if (require.main === module) { |
| 221 | + multiCredentialExample() |
| 222 | + .then(() => microsoftMultiTenantExample()) |
| 223 | + .then(() => console.log('\n✅ All examples completed successfully!')) |
| 224 | + .catch((error) => console.error('❌ Error:', error)); |
| 225 | +} |
| 226 | + |
| 227 | +export { multiCredentialExample, microsoftMultiTenantExample }; |
0 commit comments