Looging for an example for login/cookie based authentication a "n8n-nodes-starter"

Hello everybody,

I am following the examples of a custom n8n mode from GitHub - n8n-io/n8n-nodes-starter: Example starter module for custom n8n nodes. in this examples. Tutorial: Build a programmatic-style node | n8n Docs

At the moment I am struggeling with the authentications e.g.

There is a IAuthenticateGeneric and ICredentialTestRequest section in the ExampleCredentialsApi provider.

Can you guys point me to an example for a classic “web style” based authentication?

  • User logs in via Post (sends a Json User/Password)
  • Server sends back “a lot of things” - the only relevant item is a cookie with a short lifetime
  • Every call needs to decorate a header with this cookie

Every example I found is somehow based on an API Token with “long” lifetime.

So here is what I would love:

  • Something just being able to run an “authenticater” if the cookies are used and are “to old” and store them as credentials.
  • Use a login befor every call (lass nice performance) and grab the cookie.

Maybe the issue is solved?

At the moment I am mitigating with a custom workflow (but this is bad).

Greetings.

Hi @DerHarry :wave: Maybe our resident node-builder @marcus can take a look at this when he’s back on Wednesday after the public holiday in Germany? :slight_smile:

Hey @DerHarry,

That should be possible using preAuthentication in your credential, As a quick example if you were to create a credential / node for the internal rest api that requires logging into n8n your credential would likely have a cookie field that is set as expirable then in your preAuthentication method you would handle the authentication and set the http request to return the full response and use the set-cookie option that comes back in the request.

import type {
	IAuthenticateGeneric,
	ICredentialDataDecryptedObject,
	ICredentialTestRequest,
	ICredentialType,
	IHttpRequestHelper,
	INodeProperties,
} from 'n8n-workflow';

export class N8nInternalApi implements ICredentialType {
	name = 'n8nInternalApi';

	displayName = 'N8n Internal API';

	documentationUrl = 'httpRequest';

	properties: INodeProperties[] = [
		{
			displayName: 'url',
			name: 'url',
			type: 'string',
			default: '',
		},
		{
			displayName: 'Email',
			name: 'email',
			type: 'string',
			default: '',
		},
		{
			displayName: 'Password',
			name: 'password',
			type: 'string',
			typeOptions: {
				password: true,
			},
			default: '',
		},
		{
			displayName: 'cookie',
			name: 'cookie',
			type: 'hidden',
			default: '',
			typeOptions: {
				expirable: true,
			},
		},
	];

	async preAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) {
		const url = credentials.url as string;
		const resp = await this.helpers.httpRequest({
			method: 'POST',
			url: `${url.endsWith('/') ? url.slice(0, -1) : url}/rest/login`,
			body: {
				email: credentials.email,
				password: credentials.password,
			},
			returnFullResponse: true,
		});
		return { cookie: resp.headers['set-cookie'] };
	}

	authenticate: IAuthenticateGeneric = {
		type: 'generic',
		properties: {
			headers: {
				Cookie: '={{$credentials.cookie}}',
			},
		},
	};

	test: ICredentialTestRequest = {
		request: {
			baseURL: '={{$credentials?.url}}',
			url: '/rest/tags',
		},
	};
}

This in theory gives you everything needed and the end result looks like this…

Then when used in say an HTTP Request node you get this…

Hopefully this helps.

1 Like