src/auth.js
/**
 * Login function
 * Logs into shopgoodwill.com with either a credentials file or prompts the user to authenticate over CLI
 * @param {browser} browser - browser to do login
 * @return {error} - returns an error if authentication fails
 */
Login function
Logs into shopgoodwill.com with either a credentials file or prompts the user to authenticate over CLI
@param {browser} browser - browser to do login
@return {error} - returns an error if authentication fails
Login function Logs into shopgoodwill . com with either a credentials file or prompts the user to authenticate over CLI @param { browser } browser - browser to do login @return { error } - returns an error if authentication fails
async function login(browser = undefined, credentials = undefined) {
    if (!browser) {
        winston.error('Invalid Browser Provided - unable to log in');
        return new Error('Invalid Browser Provided - unable to log in');
    }
    if (credentials && fs.existsSync(credentials)) {
        creds = require(credentials);
    } else {
        let answers = await inquirer.prompt(
            [{
                    type: 'input',
                    name: 'username1',
                    message: 'Enter your shopgoodwill.com username:'
                },
                {
                    type: 'password',
                    message: 'Enter your shopgoodwill.com password:',
                    name: 'password1'
                }
            ]);
        creds = {
            username: answers.username1,
            password: answers.password1
        }
    }
    const page = await browser.newPage();
    await page.goto(SIGNIN_URL);
    await page.type(USERNAME_SELECTOR, creds.username);
    await page.type(PASSWORD_SELECTOR, creds.password);
    await page.click(LOGIN_BUTTON_SELECTOR);
    await page.waitForNavigation();
    let verifyLogin = await page.evaluate((sel) => {
        let element = document.querySelector(sel);
        return element ? element.innerText : null;
    }, LOGIN_FAILED_SELECTOR);
    if (verifyLogin) {
        winston.error('Invalid credentials - unable to log in');
        return new Error('Invalid credentials - unable to log in');
    }
    await page.close();
}
async function login ( browser = undefined , credentials = undefined ) { if ( ! browser ) { winston . error ( 'Invalid Browser Provided - unable to log in' ) ; return new Error ( 'Invalid Browser Provided - unable to log in' ) ; } if ( credentials && fs . existsSync ( credentials ) ) { creds = require ( credentials ) ; } else { let answers = await inquirer . prompt ( [ { type : 'input' , name : 'username1' , message : 'Enter your shopgoodwill.com username:' } , { type : 'password' , message : 'Enter your shopgoodwill.com password:' , name : 'password1' } ] ) ; creds = { username : answers . username1 , password : answers . password1 } } const page = await browser . newPage ( ) ; await page . goto ( SIGNIN_URL ) ; await page . type ( USERNAME_SELECTOR , creds . username ) ; await page . type ( PASSWORD_SELECTOR , creds . password ) ; await page . click ( LOGIN_BUTTON_SELECTOR ) ; await page . waitForNavigation ( ) ; let verifyLogin = await page . evaluate ( ( sel ) => { let element = document . querySelector ( sel ) ; return element ? element . innerText : null ; } , LOGIN_FAILED_SELECTOR ) ; if ( verifyLogin ) { winston . error ( 'Invalid credentials - unable to log in' ) ; return new Error ( 'Invalid credentials - unable to log in' ) ; } await page . close ( ) ; }
Login function
 Logs into shopgoodwill.com with either a credentials file or prompts the user to authenticate over CLI
 @param {browser} browser - browser to do login
 @return {error} - returns an error if authentication fails
Login function Logs into shopgoodwill . com with either a credentials file or prompts the user to authenticate over CLI @param { browser } browser - browser to do login @return { error } - returns an error if authentication fails
// Check the browser // Check if credentials come from a file or if we need to authenticate // Import the credentials file // Prompt the user to authenticate // Create the page // Go to the login page // Put in the username and password // Click the login button // Wait for navigation // Verify login successful // Make sure a login error didn't occur // Check if we actually received any matches // Close the page