|
| 1 | +# Authentication - Vectorize Approach |
| 2 | + |
| 3 | +Authenticate users using Vectorize's managed OAuth flow. |
| 4 | + |
| 5 | +## One-Time Token Generation |
| 6 | + |
| 7 | +Create an API endpoint to generate one-time tokens securely on the server: |
| 8 | + |
| 9 | +```typescript |
| 10 | +// app/api/get-one-time-connector-token/route.ts |
| 11 | +import { getOneTimeConnectorToken, VectorizeAPIConfig } from "@vectorize-io/vectorize-connect"; |
| 12 | +import { NextRequest, NextResponse } from "next/server"; |
| 13 | + |
| 14 | +export async function GET(request: NextRequest) { |
| 15 | + try { |
| 16 | + // Get authentication details from environment variables |
| 17 | + const apiKey = process.env.VECTORIZE_API_KEY; |
| 18 | + const organizationId = process.env.VECTORIZE_ORGANIZATION_ID; |
| 19 | + |
| 20 | + if (!apiKey || !organizationId) { |
| 21 | + return NextResponse.json({ |
| 22 | + error: 'Missing Vectorize API configuration' |
| 23 | + }, { status: 500 }); |
| 24 | + } |
| 25 | + |
| 26 | + // Configure the Vectorize API client |
| 27 | + const config: VectorizeAPIConfig = { |
| 28 | + authorization: apiKey, |
| 29 | + organizationId: organizationId |
| 30 | + }; |
| 31 | + |
| 32 | + // Get userId and connectorId from request url |
| 33 | + const { searchParams } = new URL(request.url); |
| 34 | + const userId = searchParams.get('userId'); |
| 35 | + const connectorId = searchParams.get('connectorId'); |
| 36 | + |
| 37 | + // Validate userId and connectorId |
| 38 | + if (!userId || !connectorId) { |
| 39 | + return NextResponse.json({ |
| 40 | + error: 'Missing userId or connectorId' |
| 41 | + }, { status: 400 }); |
| 42 | + } |
| 43 | + |
| 44 | + // Call Vectorize API to get the token |
| 45 | + const tokenResponse = await getOneTimeConnectorToken( |
| 46 | + config, |
| 47 | + userId, |
| 48 | + connectorId |
| 49 | + ); |
| 50 | + |
| 51 | + // Return the token to the client |
| 52 | + return NextResponse.json(tokenResponse, { status: 200 }); |
| 53 | + |
| 54 | + } catch (error) { |
| 55 | + console.error('Error generating token:', error); |
| 56 | + return NextResponse.json({ |
| 57 | + error: 'Failed to generate token', |
| 58 | + message: error instanceof Error ? error.message : 'Unknown error' |
| 59 | + }, { status: 500 }); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Frontend Authentication Flow |
| 65 | + |
| 66 | +```typescript |
| 67 | +import { PlatformOAuth } from '@vectorize-io/vectorize-connect'; |
| 68 | + |
| 69 | +const handleAuthenticate = async () => { |
| 70 | + try { |
| 71 | + // Get one-time token from API endpoint |
| 72 | + const tokenResponse = await fetch( |
| 73 | + `/api/get-one-time-connector-token?userId=${userId}&connectorId=${connectorId}` |
| 74 | + ).then(response => { |
| 75 | + if (!response.ok) { |
| 76 | + throw new Error(`Failed to generate token. Status: ${response.status}`); |
| 77 | + } |
| 78 | + return response.json(); |
| 79 | + }); |
| 80 | + |
| 81 | + // Redirect to Vectorize authentication flow |
| 82 | + await PlatformOAuth.redirectToVectorizeConnect( |
| 83 | + tokenResponse.token, |
| 84 | + organizationId |
| 85 | + ); |
| 86 | + |
| 87 | + } catch (error) { |
| 88 | + console.error('Authentication failed:', error); |
| 89 | + } |
| 90 | +}; |
| 91 | +``` |
| 92 | + |
| 93 | +## Complete Component Example |
| 94 | + |
| 95 | +```typescript |
| 96 | +'use client'; |
| 97 | + |
| 98 | +import { useState } from 'react'; |
| 99 | +import { PlatformOAuth } from '@vectorize-io/vectorize-connect'; |
| 100 | + |
| 101 | +export default function VectorizeConnector() { |
| 102 | + const [connectorId, setConnectorId] = useState<string | null>(null); |
| 103 | + const [isLoading, setIsLoading] = useState(false); |
| 104 | + const [error, setError] = useState<string | null>(null); |
| 105 | + |
| 106 | + const handleConnect = async () => { |
| 107 | + setIsLoading(true); |
| 108 | + setError(null); |
| 109 | + |
| 110 | + try { |
| 111 | + // Get one-time token from API endpoint |
| 112 | + const tokenResponse = await fetch( |
| 113 | + `/api/get-one-time-connector-token?userId=user123&connectorId=${connectorId}` |
| 114 | + ).then(response => { |
| 115 | + if (!response.ok) { |
| 116 | + throw new Error(`Failed to generate token. Status: ${response.status}`); |
| 117 | + } |
| 118 | + return response.json(); |
| 119 | + }); |
| 120 | + |
| 121 | + // Redirect to Vectorize authentication flow |
| 122 | + await PlatformOAuth.redirectToVectorizeConnect( |
| 123 | + tokenResponse.token, |
| 124 | + 'your-org-id' |
| 125 | + ); |
| 126 | + |
| 127 | + setIsLoading(false); |
| 128 | + } catch (err: any) { |
| 129 | + const errorMessage = err instanceof Error ? err.message : 'Failed to connect'; |
| 130 | + setError(errorMessage); |
| 131 | + setIsLoading(false); |
| 132 | + } |
| 133 | + }; |
| 134 | + |
| 135 | + return ( |
| 136 | + <div className="space-y-4"> |
| 137 | + <h2 className="text-lg font-semibold">Platform Connection</h2> |
| 138 | + |
| 139 | + {error && ( |
| 140 | + <div className="p-4 bg-red-50 text-red-700 rounded-lg"> |
| 141 | + {error} |
| 142 | + </div> |
| 143 | + )} |
| 144 | + |
| 145 | + <button |
| 146 | + onClick={handleConnect} |
| 147 | + disabled={!connectorId || isLoading} |
| 148 | + className="bg-green-600 text-white px-4 py-2 rounded-lg" |
| 149 | + > |
| 150 | + {isLoading ? "Connecting..." : "Connect to Platform"} |
| 151 | + </button> |
| 152 | + </div> |
| 153 | + ); |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## Error Handling |
| 158 | + |
| 159 | +```typescript |
| 160 | +try { |
| 161 | + const tokenResponse = await getOneTimeConnectorToken(config, userId, connectorId); |
| 162 | +} catch (error) { |
| 163 | + if (error.response?.status === 401) { |
| 164 | + console.error('Invalid Vectorize API token'); |
| 165 | + } else if (error.response?.status === 404) { |
| 166 | + console.error('Connector or user not found'); |
| 167 | + } else { |
| 168 | + console.error('Token generation failed:', error.message); |
| 169 | + } |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +## Next Steps |
| 174 | + |
| 175 | +- [User Management](../../user-management/vectorize/) |
| 176 | +- [Frontend Implementation](../../frontend-implementation/vectorize/) |
0 commit comments