Handle updating of credentials

This commit is contained in:
Adhiraj
2020-10-02 14:19:04 +05:30
parent 67d82b4169
commit 0d13a15904
6 changed files with 54 additions and 13 deletions

View File

@@ -33,10 +33,15 @@ describe('Test Connect', () => {
console.log('please be ready to scan with your phone')
const conn = new WAConnection()
let credentialsUpdateCalled = false
conn.on ('credentials-updated', () => credentialsUpdateCalled = true)
await conn.connect ()
assert.ok(conn.user?.jid)
assert.ok(conn.user?.phone)
assert.ok (conn.user?.imgUrl || conn.user.imgUrl === '')
assert.ok (credentialsUpdateCalled)
assertChatDBIntegrity (conn)
@@ -45,15 +50,19 @@ describe('Test Connect', () => {
})
it('should reconnect', async () => {
const conn = new WAConnection()
let credentialsUpdateCalled = false
conn.on ('credentials-updated', () => credentialsUpdateCalled = true)
await conn.loadAuthInfo (auth).connect ()
assert.ok(conn.user)
assert.ok(conn.user.jid)
assert.ok (credentialsUpdateCalled)
assertChatDBIntegrity (conn)
await conn.logout()
conn.loadAuthInfo(auth)
await conn.connect()
.then (() => assert.fail('should not have reconnected'))
.catch (err => {

View File

@@ -112,10 +112,22 @@ export class WAConnection extends Base {
}) as WAUser
if (!json.secret) {
let credsChanged = false
// if we didn't get a secret, we don't need it, we're validated
if (json.clientToken && json.clientToken !== this.authInfo.clientToken) {
console.log (`change: ${this.authInfo.clientToken}, ${json.clientToken}`)
this.authInfo = { ...this.authInfo, clientToken: json.clientToken }
credsChanged = true
}
if (json.serverToken && json.serverToken !== this.authInfo.serverToken) {
this.authInfo = { ...this.authInfo, serverToken: json.serverToken }
credsChanged = true
}
if (credsChanged) {
this.emit ('credentials-updated', this.authInfo)
}
return onValidationSuccess()
}
const secret = Buffer.from(json.secret, 'base64')
if (secret.length !== 144) {
throw new Error ('incorrect secret length received: ' + secret.length)
@@ -153,6 +165,8 @@ export class WAConnection extends Base {
serverToken: json.serverToken,
clientID: this.authInfo.clientID,
}
this.emit ('credentials-updated', this.authInfo)
return onValidationSuccess()
}
/**

View File

@@ -1,6 +1,6 @@
import * as QR from 'qrcode-terminal'
import { WAConnection as Base } from './3.Connect'
import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, MessageLogLevel, PresenceUpdate, BaileysEvent, DisconnectReason, WANode, WAOpenResult, Presence } from './Constants'
import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, MessageLogLevel, PresenceUpdate, BaileysEvent, DisconnectReason, WANode, WAOpenResult, Presence, AuthenticationCredentials } from './Constants'
import { whatsappID, unixTimestampSeconds, isGroupID, toNumber, GET_MESSAGE_ID, WA_MESSAGE_ID, WA_MESSAGE_KEY } from './Utils'
import KeyedDB from '@adiwajshing/keyed-db'
import { Mutex } from './Mutex'
@@ -322,6 +322,8 @@ export class WAConnection extends Base {
on (event: 'close', listener: (err: {reason?: DisconnectReason | string, isReconnecting: boolean}) => void): this
/** when the connection has closed */
on (event: 'intermediate-close', listener: (err: {reason?: DisconnectReason | string}) => void): this
/** when WA updates the credentials */
on (event: 'credentials-updated', listener: (auth: AuthenticationCredentials) => void): this
/** when a new QR is generated, ready for scanning */
on (event: 'qr', listener: (qr: string) => void): this
/** when the connection to the phone changes */

View File

@@ -431,4 +431,5 @@ export type BaileysEvent =
'group-participants-demote' |
'group-settings-update' |
'group-description-update' |
'received-pong'
'received-pong' |
'credentials-updated'