fix: Fix edge case in log in (no-changelog) (#10610)

This commit is contained in:
Tomi Turtiainen 2024-08-30 08:47:35 +03:00 committed by GitHub
parent 1c5164c786
commit 1b409b4e3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 7 deletions

View file

@ -1,4 +1,3 @@
import { strict as assert } from 'node:assert';
import type { AxiosRequestConfig } from 'axios';
import { N8nApiClient } from './n8nApiClient';
@ -16,15 +15,33 @@ export class AuthenticatedN8nApiClient extends N8nApiClient {
email: string;
password: string;
},
) {
): Promise<AuthenticatedN8nApiClient> {
const response = await apiClient.restApiRequest('/login', {
method: 'POST',
data: loginDetails,
});
if (response.data === 'n8n is starting up. Please wait') {
await apiClient.delay(1000);
return await this.createUsingUsernameAndPassword(apiClient, loginDetails);
}
const cookieHeader = response.headers['set-cookie'];
const authCookie = Array.isArray(cookieHeader) ? cookieHeader.join('; ') : cookieHeader;
assert(authCookie);
if (!authCookie) {
throw new Error(
'Did not receive authentication cookie even tho login succeeded: ' +
JSON.stringify(
{
status: response.status,
headers: response.headers,
data: response.data,
},
null,
2,
),
);
}
return new AuthenticatedN8nApiClient(apiClient.apiBaseUrl, authCookie);
}

View file

@ -75,11 +75,11 @@ export class N8nApiClient {
}
}
async delay(ms: number): Promise<void> {
return await new Promise((resolve) => setTimeout(resolve, ms));
}
protected getRestEndpointUrl(endpoint: string) {
return `${this.apiBaseUrl}/rest${endpoint}`;
}
private async delay(ms: number): Promise<void> {
return await new Promise((resolve) => setTimeout(resolve, ms));
}
}