Jump to content
McKay Development
lll

Best practice to logIn

Recommended Posts

 

 

I have a Bot class and an async method for logIn, are there any better way for writing this method?

 

completeLogin(botDetails, useLoginKey) {
    this.user.logOn(this.details(botDetails, useLoginKey));

    return new Promise((done, reject) => {
        const cxt = this;

        async function success(_, cookies) {
            try {
                await cxt.setCookies(cookies);
                cxt.user.off('error', failed);
                done();
            } catch(err) {
                reject(err);
            }
        }

        function failed(err) {
            cxt.user.off('webSession', success);
            reject(err);
        }

        cxt.user.once('webSession', success);
        cxt.user.once('error', failed);
    });
}

 

Share this post


Link to post
Share on other sites

 

This logic will be written in the constructor. Something like this :

constructor(accountName) {
    this.setCookies = util.promisify(this.manager.setCookies).bind(this.manager);

    this.user.on('error', this.logOut);
    this.user.on('loginKey', loginKey => db.findOne('bot', { accountName }, { loginKey }));
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...