New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

auth-mysql

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

auth-mysql

Implementation of MySQL in Baileys.

unpublished
latest
Source
npmnpm
Version
1.0.3
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Authentication with MySQL for Baileys

Usage

  • 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
  • Install auth-mysql
npm i auth-mysql
  • Import code
const { useMySQLAuthState } = require('auth-mysql')
  • 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')

FAQs

Package last updated on 01 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts