Skip to content

Commit

Permalink
feat: Added WPP.auth.logout function (wppconnect-team/wppconnect#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Oct 20, 2021
1 parent ed03d32 commit 75914f2
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/auth/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as webpack from '../webpack';
import {
Base64,
Browser,
Cmd,
Conn,
Constants,
Features,
Expand All @@ -37,19 +38,23 @@ export class Auth extends Emittery<AuthEventTypes> {
}

async initialize() {
Conn.on('change:ref', async () => {
const authCode = await this.getAuthCode().catch(() => null);
if (authCode) {
this.emit('change', authCode);
}
});
// Conn.on('change:ref', async () => {
// const authCode = await this.getAuthCode().catch(() => null);
// if (authCode) {
// this.emit('change', authCode);
// }
// });

State.on('change:state', async () => {
const isIdle = this.isIdle();
if (isIdle) {
this.emit('idle');
}
});

State.on('all', console.log);

Cmd.on('logout', () => this.emit('logout'));
}

/**
Expand Down Expand Up @@ -117,4 +122,14 @@ export class Auth extends Emittery<AuthEventTypes> {
public poke(): void {
State.poke();
}

public async logout(): Promise<any> {
State.logout();

await new Promise((resolve) => {
Cmd.once('logout', resolve);
});

return true;
}
}
1 change: 1 addition & 0 deletions src/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export type AuthCode = AuthCodeSingleDevice | AuthCodeMultiDevice;
export interface AuthEventTypes {
change: AuthCode;
idle: undefined;
logout: undefined;
}
38 changes: 38 additions & 0 deletions src/whatsapp/enums/LogoutReason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*!
* 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 { exportModule } from '../exportModule';

/** @whatsapp 2.2140.7:45634 */
export declare enum LogoutReason {
USER_INITIATED = 'user_initiated',
SYNCD_FAILURE = 'syncd_failure',
CRITICAL_SYNC_TIMEOUT = 'critical_sync_timeout',
UNKNOWN_COMPANION = 'unknown_companion',
CLIENT_VERSION_OUTDATED = 'client_version_outdated',
SYNCD_ERROR_DURING_BOOTSTRAP = 'syncd_error_during_bootstrap',
ACCOUNT_SYNC_ERROR = 'account_sync_error',
CLIENT_FATAL_ERROR = 'client_fatal_error',
UNKNOWN = 'unknown',
}

exportModule(
exports,
{
LogoutReason: 'LogoutReason',
},
(m) => m.LogoutReason
);
1 change: 1 addition & 0 deletions src/whatsapp/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
* limitations under the License.
*/

export * from './LogoutReason';
export * from './SendMsgResult';
export * from './TextFontStyle';
166 changes: 166 additions & 0 deletions src/whatsapp/misc/Cmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*!
* 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 { exportModule } from '../exportModule';
import { EventEmitter } from '.';

/**
* @whatsapp 2.2140.7:69162
*/
export declare class CmdClass extends EventEmitter {
public isMainLoaded: boolean;
public uiBusy: number;

mainLoaded(): void;
incrementProgress(): void;
initialLoadReady(): void;
logSocketSummary(): void;
muteAll(e?: any, t?: any): void;
muteAllCall(e?: any, t?: any): void;
muteChat(e?: any, t?: any): void;
muteChats(e?: any, t?: any, r?: any): void;
deleteOrExitChat(e?: any, t?: any): void;
clearChat(e?: any, t?: any): void;
archiveChat(e?: any, t?: any): void;
pinChat(e?: any, t?: any): void;
pinChats(e?: any, t?: any): void;
markChatUnread(e?: any, t?: any): void;
msgInfoDrawer(e?: any): void;
chatSearch(e?: any): void;
chatInfoDrawer(e?: any, t?: any, r?: any): void;
attachMediaDrawer(e?: any): void;
attachProduct(e?: any): void;
verificationDrawer(e?: any): void;
liveLocationDrawer(e?: any, t?: any): void;
expiredLocationDrawer(e?: any): void;
mediaViewerModal(e?: any): void;
openMediaViewerForAlbumMedia(e?: any): void;
productImageViewerModal(e?: any, t?: any): void;
ephemeralDrawer(e?: any, t?: any): void;
sendStarMsgs(e?: any, t?: any, r?: any, n?: any): void;
sendUnstarMsgs(e?: any, t?: any, r?: any, n?: any): void;
sendDeleteMsgs(e?: any, t?: any, r?: any, n?: any, i?: any): void;
sendRevokeMsgs(e?: any, t?: any, r?: any, n?: any, i?: any): void;
_openChat(e?: any, t?: any): void;
openChatAt(e?: any, t?: any): void;
openChatFromUnread(e?: any): void;
openChatBottom(e?: any): void;
scrollToPtt(e?: any): void;
_scrollToFocusedMsg(e?: any): void;
_scrollChatToBottom(): void;
flashFocusedMsg(e?: any): void;
updateChatlistSelection(e?: any): void;
closeChat(e?: any): void;
focusChatTextInput(e?: any): void;
setChatCtwaContextLinkData(e?: any, t?: any): void;
focusShowMsg(e?: any): void;
enterChatTextInput(e?: any): void;
focusNextChat(e?: any): void;
focusPrevChat(e?: any): void;
focusChatList(): void;
focusChatSearch(): void;
pasteChatTextInput(e?: any, t?: any): void;
startGroupCall(e?: any, t?: any): void;
closeStatusViewer(): void;
sendPasteToCompose(e?: any, t?: any): void;
openComposeBoxPanel(e?: any): void;
login(e?: any): void;
logout(): void;
openContextMenu(e?: any, t?: any): void;
closeContextMenu(e?: any): void;
openTooltip(e?: any, t?: any): void;
closeTooltip(e?: any): void;
openToast(e?: any): void;
closeToast(e?: any): void;
alertNewMsg(e?: any): void;
newMediaMsg(e?: any): void;
alertCall(e?: any, t?: any, r?: any, n?: any, i?: any): void;
cancelCall(e?: any): void;
windowError(): void;
cannotReachPhone(): void;
sentPing(): void;
openModal(e?: any, t?: any): void;
closeModal(e?: any): void;
openModalMedia(e?: any, t?: any): void;
closeModalMedia(): void;
setDrawerContext(e?: any, t?: any): void;
openDrawerRight(e?: any, t?: any, r?: any, n?: any, i?: any): void;
openDrawerMid(e?: any, t?: any, r?: any, n?: any, i?: any, a?: any): void;
openDrawerLeft(e?: any, t?: any, r?: any, n?: any, i?: any): void;
existsDrawerRight(e?: any): void;
existsDrawerMid(e?: any): void;
existsDrawerLeft(e?: any): void;
closeDrawerRight(): void;
closeDrawerMid(): void;
closeDrawerLeft(): void;
onPanesWillChange(e?: any): void;
onPanesDidChange(e?: any): void;
openGroupInviteModal(e?: any): void;
openGroupV4InviteRequestFlow(e?: any, t?: any, r?: any, n?: any): void;
mediaPlaying(e?: any): void;
pttRecording(): void;
pttPlaying(e?: any): void;
uiResize(): void;
setUiBusy(e?: any): void;
windowMouseDown(e?: any): void;
windowClick(e?: any): void;
midnight(): void;
textsizeChange(e?: any): void;
scrollMessages(): void;
getConversationHeaderOffset(e?: any): void;
floaterEscapeOverlap(e?: any, t?: any): void;
autoplayPTT(e?: any): void;
refreshMessages(): void;
serverPropsUpdated(): void;
storageInitializationError(): void;
restartBackend(): void;
downgradeWebclient(): void;
upgradeToMDInternal(): void;
upgradeToMDProd(): void;
refreshQR(): void;
setSocketState(e?: any): void;
socketStreamDisconnected(): void;
openSocketStream(): void;
readyForMainStreamMode(): void;
onInitialChatHistorySynced(): void;
onRecentChatHistorySynced(): void;
onFullChatHistorySynced(): void;
onHistorySyncChunkProcessed(e?: any): void;
handleOfflineProgressUpdate(): void;
criticalSyncDone(): void;
onTemporaryBan(e?: any): void;
onStartingLogout(): void;
handleFatalError(): void;
merchantDetailsDrawer(e?: any): void;
showMerchantDetailsEntityTypePopup(e?: any, t?: any): void;
showCountrySelector(e?: any, t?: any, r?: any): void;
toggleStickerMaker(): void;
}

/** @whatsapp 2.2140.7:69162 */
export declare const Cmd: CmdClass;

export declare const APP_STATE_SYNC_COMPLETED: string;

exportModule(
exports,
{
CmdClass: 'Cmd',
Cmd: 'default',
APP_STATE_SYNC_COMPLETED: 'APP_STATE_SYNC_COMPLETED',
},
(m) => m.Cmd && m.default instanceof m.Cmd
);
1 change: 1 addition & 0 deletions src/whatsapp/misc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export * from './Base64';
export * from './Browser';
export * from './ClockSkew';
export * from './Cmd';
export * from './Conn';
export * from './Constants';
export * from './EventEmitter';
Expand Down
3 changes: 2 additions & 1 deletion src/whatsapp/models/StateModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { LogoutReason } from '../enums';
import { exportModule } from '../exportModule';
import {
Model,
Expand Down Expand Up @@ -85,7 +86,7 @@ export declare class StateModel extends Model {
exitLoop(e?: any): any;
clearCredentialsAndStoredData(): any;
destroyStorage(): any;
logout(e?: any): any;
logout(sendLogout?: boolean, reason?: LogoutReason): void;
summary(): any;
registerHandler(e?: any): any;
onStateChange(): any;
Expand Down

0 comments on commit 75914f2

Please sign in to comment.