Skip to content

AN-Tech-Crew/auth-mysql

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Authentication with MySQL for Baileys

Usage

  1. Create table in MySQL
CREATE TABLE `auth` (
	`session` varchar(50) NOT NULL,
	`id` varchar(100) NOT NULL,
	`value` json DEFAULT NULL,
	UNIQUE KEY `idxunique` (`session`,`id`),
	KEY `idxsession` (`session`),
	KEY `idxid` (`id`)
) ENGINE=MyISAM
  1. Install auth-mysql
npm i auth-mysql
  1. Import code
const { useMySQLAuthState } = require('auth-mysql')
  1. Implement code
const { state, saveCreds, removeCreds } = await useMySQLAuthState({
	session: sessionName || '1',
	host: 'localhost',
	user: 'root',
	password: 'Password123#',
	database: 'auth'
})

Complete code for use

const {
	makeWASocket,
	makeCacheableSignalKeyStore,
	isJidBroadcast,
	fetchLatestBaileysVersion,
	DisconnectReason
} = require('@whiskeysockets/Baileys')
const { useMySQLAuthState } = require('auth-mysql')
const pino = require('pino')

const logger = pino({
	level: 40
})

async function startSock(sessionName){
	const { version, error, isLatest } = await fetchLatestBaileysVersion()

	//This is important, so as not to try to create a MySQL connection with unstable internet or without internet, otherwise it gives the error: "Many connections"
	if (error || !isLatest){
		connectToWhatsApp(sessionName)
		return
	}

	const { state, saveCreds, removeCreds } = await useMySQLAuthState({
		session: sessionName || '1',
		host: 'localhost',
		user: 'root',
		password: 'Password123#',
		database: 'auth'
	})

	const sock = makeWASocket({
		auth: {
			creds: state.creds,
			keys: makeCacheableSignalKeyStore(state.keys, logger),
		},
		browser: ['Windows', 'DESKTOP', '2.2344.5.0'],
		version: version,
		qrTimeout: 45_000,
		defaultQueryTimeoutMs: undefined, // important
		markOnlineOnConnect: true,
		logger: logger,
		shouldIgnoreJid: (jid) => {
			return !jid || isJidBroadcast(jid)
		}
	})

	sock.ev.on('creds.update', saveCreds)

	sock.ev.on('connection.update', async({ connection, lastDisconnect }) => {
		const status = lastDisconnect?.error?.output?.statusCode

		if (connection === 'close'){
			const reason = Object.entries(DisconnectReason).find(i => i[1] === status)?.[0] || 'unknown'

			console.log(`Closed connection, status: ${reason} (${status})`)

			if (status === 403 || status === 401){
				sock = null
				removeCreds()
			} else {
				startSock(session)
			}
		} else if (connection === 'open'){
			console.log(`WhatsApp on-line!`)
		}
	})

	sock.ev.on('messages.upsert', async({ messages, type }) => {
		// your code here
	})
}

startSock('session1')

If you want to start other sessions in the same code, use this:

startSock('session1')
startSock('session2')
startSock('session3')
startSock('session4')

About

Authentication with MySQL for Baileys

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%