Skip to content

Commit

Permalink
feat: Added WPP.conn.on('require_auth') event
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Feb 27, 2022
1 parent c2da66d commit f75b372
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/conn/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
import './registerAuthCodeChangeEvent';
import './registerQRCodeIdleEvent';
import './registerLogoutEvent';
import './registerRequireAuthEvent';
54 changes: 54 additions & 0 deletions src/conn/events/registerRequireAuthEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Debug from 'debug';

import * as webpack from '../../webpack';
import { Socket } from '../../whatsapp';
import { SOCKET_STATE } from '../../whatsapp/enums/SOCKET_STATE';
import { isAuthenticated } from '..';
import { eventEmitter } from '../eventEmitter';

const debug = Debug('WA-JS:conn');

webpack.onInjected(registerRequireAuthEvent);

function registerRequireAuthEvent() {
let fired = false;
const trigger = async () => {
if (isAuthenticated()) {
fired = false;
return;
}

if (fired) {
return;
}

if (
Socket.state === SOCKET_STATE.UNPAIRED ||
Socket.state === SOCKET_STATE.UNPAIRED_IDLE
) {
fired = true;
eventEmitter.emit('require_auth');
}
};

trigger();
Socket.on('change:state', trigger);

debug('change event registered');
}
2 changes: 1 addition & 1 deletion src/whatsapp/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export * from './QuickReplyModel';
export * from './RecentEmojiModel';
export * from './RecentStickerModel';
export * from './ReplyButtonModel';
export * from './StateModel';
export * from './SocketModel';
export * from './StatusModel';
export * from './StatusV3Model';
export * from './StickerModel';
Expand Down

0 comments on commit f75b372

Please sign in to comment.