Skip to content

Commit 30cd8a0

Browse files
committed
feat: the created instance token can now also be optionally defined manually in the creation endpoint
1 parent 1b7015c commit 30cd8a0

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Added configuration of events by webhook of instances
1414
* Now the api key can be exposed in fetch instances if the EXPOSE_IN_FETCH_INSTANCES variable is set to true
1515
* Added option to generate qrcode as soon as the instance is created
16+
* The created instance token can now also be optionally defined manually in the creation endpoint
1617

1718
### Fixed
1819

src/validate/validate.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const instanceNameSchema: JSONSchema7 = {
5656
},
5757
},
5858
qrcode: { type: 'boolean', enum: [true, false] },
59+
token: { type: 'string' },
5960
},
6061
...isNotEmpty('instanceName'),
6162
};

src/whatsapp/controllers/instance.controller.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ export class InstanceController {
2323

2424
private readonly logger = new Logger(InstanceController.name);
2525

26-
public async createInstance({ instanceName, webhook, events, qrcode }: InstanceDto) {
26+
public async createInstance({
27+
instanceName,
28+
webhook,
29+
events,
30+
qrcode,
31+
token,
32+
}: InstanceDto) {
2733
const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
2834

2935
if (mode === 'container') {
@@ -43,9 +49,12 @@ export class InstanceController {
4349
this.waMonitor.waInstances[instance.instanceName] = instance;
4450
this.waMonitor.delInstanceTime(instance.instanceName);
4551

46-
const hash = await this.authService.generateHash({
47-
instanceName: instance.instanceName,
48-
});
52+
const hash = await this.authService.generateHash(
53+
{
54+
instanceName: instance.instanceName,
55+
},
56+
token,
57+
);
4958

5059
let getEvents: string[];
5160

@@ -78,9 +87,12 @@ export class InstanceController {
7887
this.waMonitor.waInstances[instance.instanceName] = instance;
7988
this.waMonitor.delInstanceTime(instance.instanceName);
8089

81-
const hash = await this.authService.generateHash({
82-
instanceName: instance.instanceName,
83-
});
90+
const hash = await this.authService.generateHash(
91+
{
92+
instanceName: instance.instanceName,
93+
},
94+
token,
95+
);
8496

8597
let getEvents: string[];
8698

src/whatsapp/dto/instance.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export class InstanceDto {
33
webhook?: string;
44
events?: string[];
55
qrcode?: boolean;
6+
token?: string;
67
}

src/whatsapp/services/auth.service.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ export class AuthService {
5656
return { jwt: token };
5757
}
5858

59-
private async apikey(instance: InstanceDto) {
60-
const apikey = v4().toUpperCase();
59+
private async apikey(instance: InstanceDto, token?: string) {
60+
const apikey = token ? token : v4().toUpperCase();
6161

6262
const auth = await this.repository.auth.create({ apikey }, instance.instanceName);
6363

6464
if (auth['error']) {
6565
this.logger.error({
66-
localError: AuthService.name + '.jwt',
66+
localError: AuthService.name + '.apikey',
6767
error: auth['error'],
6868
});
6969
throw new BadRequestException('Authentication error', auth['error']?.toString());
@@ -72,9 +72,11 @@ export class AuthService {
7272
return { apikey };
7373
}
7474

75-
public async generateHash(instance: InstanceDto) {
75+
public async generateHash(instance: InstanceDto, token?: string) {
7676
const options = this.configService.get<Auth>('AUTHENTICATION');
77-
return (await this[options.TYPE](instance)) as { jwt: string } | { apikey: string };
77+
return (await this[options.TYPE](instance, token)) as
78+
| { jwt: string }
79+
| { apikey: string };
7880
}
7981

8082
public async refreshToken({ oldToken }: OldToken) {

0 commit comments

Comments
 (0)