feat: add "multi file auth state" implementation

1. add multi file auth state since it's far more efficient than single state
2. deprecate single file auth state (please don't use it anymore)
This commit is contained in:
Adhiraj Singh
2022-05-22 21:21:35 +05:30
parent a8e209705a
commit 06437e182d
7 changed files with 177 additions and 88 deletions

View File

@@ -140,12 +140,12 @@ You obviously don't want to keep scanning the QR code every time you want to con
So, you can load the credentials to log back in:
``` ts
import makeWASocket, { BufferJSON, useSingleFileAuthState } from '@adiwajshing/baileys'
import makeWASocket, { BufferJSON, useMultiFileAuthState } from '@adiwajshing/baileys'
import * as fs from 'fs'
// utility function to help save the auth state in a single file
// it's utility ends at demos -- as re-writing a large file over and over again is very inefficient
const { state, saveState } = useSingleFileAuthState('./auth_info_multi.json')
// utility function to help save the auth state in a single folder
// this function serves as a good guide to help write auth & key states for SQL/no-SQL databases, which I would recommend in any production grade system
const { state, saveState } = useMultiFileAuthState('auth_info_baileys')
// will use the given state to connect
// so if valid credentials are available -- it'll connect without QR
const conn = makeWASocket({ auth: state })
@@ -153,7 +153,7 @@ const conn = makeWASocket({ auth: state })
conn.ev.on ('creds.update', saveState)
```
**Note**: When a message is received/sent, due to signal sessions needing updating, the auth keys (`authState.keys`) will update. Whenever that happens, you must save the updated keys. Not doing so will prevent your messages from reaching the recipient & other unexpected consequences. The `useSingleFileAuthState` function automatically takes care of that, but for any other serious implementation -- you will need to be very careful with the key state management.
**Note**: When a message is received/sent, due to signal sessions needing updating, the auth keys (`authState.keys`) will update. Whenever that happens, you must save the updated keys (`authState.keys.set()` is called). Not doing so will prevent your messages from reaching the recipient & other unexpected consequences. The `useMultiFileAuthState` function automatically takes care of that, but for any other serious implementation -- you will need to be very careful with the key state management.
## Listening to Connection Updates