'Gram.js - on node.js web interface

I am working on signing a user via the Telegram API with a web interface (node.js).

The flow is the following: User access web interface -> Click on Send Phone Code -> call Post Web Service, that send the Phone Code.

router.post('/sendPhoneCode', async function(req, res, next){
            const _stringSession = new telegramStringSession.StringSession("");
            const _apiId = parseInt(req.body.apiID);
            const _apiHash = req.body.apiHash;
            const _phoneNumber =  req.body.phoneNumber;
            const client = new telegramClient.TelegramClient(_stringSession, _apiId, _apiHash,{
              connectionRetries: 3
            });

            await client.connect(); 
            const {phoneCodeHash,isCodeViaApp }=  await client.sendCode({apiId: _apiId, apiHash: _apiHash}, _phoneNumber);
            console.log("Phone Code Hash : " + phoneCodeHash + " Session : " + client.session.save());
            

Then on the same page, the user will input the phone code to logged in. But I am having Invalid Phone Code. Seems like the phone code changes everytime I call client.start();

router.post('/saveStringSession', async function(req, res, next){
    
            const _stringSession = new telegramStringSession.StringSession("");
            const _apiId = parseInt(req.body.apiID);
            const _apiHash = req.body.apiHash;
            const _phoneNumber =  req.body.phoneNumber;
            const client = new telegramClient.TelegramClient(_stringSession, _apiId, _apiHash,{
              connectionRetries: 3
            });
            
            await client.start({
                phoneNumber: () => _phoneNumber,
                phoneCode: async () => req.body.phoneCode,
                onError: (err) => console.log(err),
            });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source