Skip to content

Commit 4028551

Browse files
committed
[DOCS] documents some files in the 'auth' folder
1 parent f1a9ebb commit 4028551

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/auth/index.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
const whatsapp = require('whatsapp-web.js');
1+
const { Client } = require('whatsapp-web.js');
22
const qrcode = require('qrcode-terminal');
33
const fs = require('fs');
44
const path = require('path');
55

66
const FILE_NAME = 'session.json';
77

8-
class Session extends whatsapp.Client {
8+
/**
9+
* Starting point for interacting with the WhatsApp Web API.
10+
* @param {string} SESSION_FILE_PATH - Path to `session.json` file.
11+
* @see https://docs.wwebjs.dev/Client.html
12+
* @extends {Client}
13+
*/
14+
class Session extends Client {
915
constructor() {
1016
super({
1117
puppeteer: {
@@ -16,22 +22,36 @@ class Session extends whatsapp.Client {
1622
this.SESSION_FILE_PATH = path.join(__dirname, FILE_NAME);
1723
}
1824

25+
/**
26+
* Checks if the `session.json` file already exists.
27+
* @returns {boolean} - `True` if exists, `False` if not.
28+
*/
1929
get exists() {
2030
return fs.existsSync(this.SESSION_FILE_PATH);
2131
}
2232

33+
/**
34+
* Throws the QR-Code to authenticate the session. When the QR-Code is read, the session file is written.
35+
*/
2336
create() {
2437
this.on('qr', (qr) => {
2538
qrcode.generate(qr, { small: true });
2639
});
2740
this.on('authenticated', this.save);
2841
}
2942

43+
/**
44+
* Writes the session in a .json file (`this.SESSION_FILE_PATH`)
45+
* @param {object} session - The session file returned in the authentication.
46+
*/
3047
save(session) {
3148
fs.writeFileSync(this.SESSION_FILE_PATH, JSON.stringify(session));
3249
console.log('⚠ The current session has been saved ⚠');
3350
}
3451

52+
/**
53+
* Loads the saved session file.
54+
*/
3555
load() {
3656
if (!this.exists) {
3757
throw Error(`session data does not exist in ${this.SESSION_FILE_PATH}`);
@@ -44,6 +64,9 @@ class Session extends whatsapp.Client {
4464
console.log('⚠ The previous session was loaded ⚠');
4565
}
4666

67+
/**
68+
* Starts the session.
69+
*/
4770
start() {
4871
this.on('ready', () => {
4972
console.log('Client is ready!');

0 commit comments

Comments
 (0)