@thetypefounders/continue-with-google 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -6,10 +6,9 @@ export interface Logger {
6
6
  export type Options = {
7
7
  challengeCount?: number;
8
8
  challengeTimeoutSeconds?: number;
9
- challengeScreenshot?: boolean;
10
9
  trialCount?: number;
11
10
  trialTimeoutSeconds?: number;
12
- trialScreenshot?: boolean;
11
+ screenshot?: boolean | string;
13
12
  waitForSelector?: WaitForSelectorOptions;
14
13
  };
15
14
  export declare function authenticate(page: Page, email: string, password: string, secret: string, selector: string, options?: Options, logger?: Logger): Promise<ElementHandle | null>;
package/dist/index.js CHANGED
@@ -3,31 +3,31 @@ import { setTimeout } from 'node:timers/promises';
3
3
  const DEFAULTS = {
4
4
  challengeCount: 3,
5
5
  challengeTimeoutSeconds: 30,
6
- challengeScreenshot: false,
7
6
  trialCount: 10,
8
7
  trialTimeoutSeconds: 2,
9
- trialScreenshot: false,
10
8
  };
11
9
  export async function authenticate(page, email, password, secret, selector, options = DEFAULTS, logger = console) {
10
+ const mergedOptions = { ...DEFAULTS, ...options };
12
11
  logger.info('Waiting to enter the email...');
12
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
13
13
  await page.waitForSelector('input[type=email]', { visible: true });
14
14
  logger.info('Entering the email...');
15
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
15
16
  await page.type('input[type=email]', email);
16
17
  await page.keyboard.press('Enter');
17
18
  logger.info('Waiting to enter the password...');
19
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
18
20
  await page.waitForSelector('input[type=password]', { visible: true });
19
21
  logger.info('Entering the password...');
22
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
20
23
  await page.type('input[type=password]', password);
21
24
  await page.keyboard.press('Enter');
22
- for (let attempt = 0, found = false; attempt < (options.challengeCount || DEFAULTS.challengeCount) && !found; attempt++) {
25
+ for (let attempt = 0, found = false; attempt < mergedOptions.challengeCount && !found; attempt++) {
23
26
  if (attempt > 0) {
24
27
  logger.warn(`Challenged on attempt ${attempt}. Entering the code...`);
25
- if (options.challengeScreenshot)
26
- await takeScreenshotToDisplay(page);
28
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
27
29
  if (attempt > 1) {
28
- await setTimeout(1000 *
29
- (options.challengeTimeoutSeconds ||
30
- DEFAULTS.challengeTimeoutSeconds));
30
+ await setTimeout(1000 * mergedOptions.challengeTimeoutSeconds);
31
31
  }
32
32
  const code = generateToken(secret);
33
33
  await page.evaluate(() => {
@@ -36,10 +36,12 @@ export async function authenticate(page, email, password, secret, selector, opti
36
36
  });
37
37
  await page.type('input[type=tel]', code);
38
38
  await page.keyboard.press('Enter');
39
- await waitForTrial(page, options.trialCount || DEFAULTS.trialCount, options.trialTimeoutSeconds || DEFAULTS.trialTimeoutSeconds, options.trialScreenshot || DEFAULTS.trialScreenshot, logger);
39
+ await waitForTrial(page, mergedOptions.trialCount, mergedOptions.trialTimeoutSeconds, mergedOptions.screenshot, logger);
40
40
  }
41
41
  found = await Promise.any([
42
- page.waitForSelector(selector, options.waitForSelector).then(() => true),
42
+ page
43
+ .waitForSelector(selector, mergedOptions.waitForSelector)
44
+ .then(() => true),
43
45
  page
44
46
  .waitForSelector('input[type=tel]', { visible: true })
45
47
  .then(() => false),
@@ -47,12 +49,11 @@ export async function authenticate(page, email, password, secret, selector, opti
47
49
  }
48
50
  return await page.$(selector);
49
51
  }
50
- async function waitForTrial(page, attemptCount, attemptTimeoutSeconds, attemptScreenshot, logger) {
52
+ async function waitForTrial(page, attemptCount, attemptTimeoutSeconds, screenshot, logger) {
51
53
  for (let attempt = -1, previous = undefined, current = undefined; attempt < attemptCount && (current === undefined || previous !== current); attempt++) {
52
54
  if (attempt > 0) {
53
55
  logger.warn(`Tried on attempt ${attempt}. Waiting to finish...`);
54
- if (attemptScreenshot)
55
- await takeScreenshotToDisplay(page);
56
+ await takeScreenshotToDisplay(page, screenshot, logger);
56
57
  }
57
58
  if (attempt > -1) {
58
59
  await setTimeout(1000 * attemptTimeoutSeconds);
@@ -74,8 +75,15 @@ async function takeScreenshotToCompare(page) {
74
75
  return undefined;
75
76
  }
76
77
  }
77
- async function takeScreenshotToDisplay(page) {
78
- await page.screenshot({
79
- path: `continue-with-google-${new Date(Date.now()).toISOString()}.png`,
80
- });
78
+ async function takeScreenshotToDisplay(page, mode, logger) {
79
+ const timestamp = new Date(Date.now()).toISOString();
80
+ if (mode === 'log') {
81
+ const content = await page.evaluate(() => document.body.innerText);
82
+ logger.info(`${timestamp}:\n${content}\n`);
83
+ }
84
+ else if (mode) {
85
+ const path = `continue-with-google-${timestamp.replace(':', '-')}.png`;
86
+ // @ts-ignore
87
+ await page.screenshot({ path });
88
+ }
81
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thetypefounders/continue-with-google",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "license": "Apache-2.0",
5
5
  "author": "Ivan Ukhov <ivan.ukhov@gmail.com>",
6
6
  "description": "Two-factor authentication with Google via Puppeteer",
package/src/index.ts CHANGED
@@ -10,20 +10,17 @@ export interface Logger {
10
10
  export type Options = {
11
11
  challengeCount?: number;
12
12
  challengeTimeoutSeconds?: number;
13
- challengeScreenshot?: boolean;
14
13
  trialCount?: number;
15
14
  trialTimeoutSeconds?: number;
16
- trialScreenshot?: boolean;
15
+ screenshot?: boolean | string;
17
16
  waitForSelector?: WaitForSelectorOptions;
18
17
  };
19
18
 
20
19
  const DEFAULTS: Options = {
21
20
  challengeCount: 3,
22
21
  challengeTimeoutSeconds: 30,
23
- challengeScreenshot: false,
24
22
  trialCount: 10,
25
23
  trialTimeoutSeconds: 2,
26
- trialScreenshot: false,
27
24
  };
28
25
 
29
26
  export async function authenticate(
@@ -35,32 +32,36 @@ export async function authenticate(
35
32
  options: Options = DEFAULTS,
36
33
  logger: Logger = console
37
34
  ): Promise<ElementHandle | null> {
35
+ const mergedOptions = { ...DEFAULTS, ...options };
36
+
38
37
  logger.info('Waiting to enter the email...');
38
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
39
39
  await page.waitForSelector('input[type=email]', { visible: true });
40
+
40
41
  logger.info('Entering the email...');
42
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
41
43
  await page.type('input[type=email]', email);
42
44
  await page.keyboard.press('Enter');
43
45
 
44
46
  logger.info('Waiting to enter the password...');
47
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
45
48
  await page.waitForSelector('input[type=password]', { visible: true });
49
+
46
50
  logger.info('Entering the password...');
51
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
47
52
  await page.type('input[type=password]', password);
48
53
  await page.keyboard.press('Enter');
49
54
 
50
55
  for (
51
56
  let attempt = 0, found = false;
52
- attempt < (options.challengeCount || DEFAULTS.challengeCount!) && !found;
57
+ attempt < mergedOptions.challengeCount! && !found;
53
58
  attempt++
54
59
  ) {
55
60
  if (attempt > 0) {
56
61
  logger.warn(`Challenged on attempt ${attempt}. Entering the code...`);
57
- if (options.challengeScreenshot) await takeScreenshotToDisplay(page);
62
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
58
63
  if (attempt > 1) {
59
- await setTimeout(
60
- 1000 *
61
- (options.challengeTimeoutSeconds ||
62
- DEFAULTS.challengeTimeoutSeconds!)
63
- );
64
+ await setTimeout(1000 * mergedOptions.challengeTimeoutSeconds!);
64
65
  }
65
66
  const code = generateToken(secret);
66
67
  await page.evaluate(() => {
@@ -71,14 +72,16 @@ export async function authenticate(
71
72
  await page.keyboard.press('Enter');
72
73
  await waitForTrial(
73
74
  page,
74
- options.trialCount || DEFAULTS.trialCount!,
75
- options.trialTimeoutSeconds || DEFAULTS.trialTimeoutSeconds!,
76
- options.trialScreenshot || DEFAULTS.trialScreenshot!,
75
+ mergedOptions.trialCount!,
76
+ mergedOptions.trialTimeoutSeconds!,
77
+ mergedOptions.screenshot,
77
78
  logger
78
79
  );
79
80
  }
80
81
  found = await Promise.any([
81
- page.waitForSelector(selector, options.waitForSelector).then(() => true),
82
+ page
83
+ .waitForSelector(selector, mergedOptions.waitForSelector)
84
+ .then(() => true),
82
85
  page
83
86
  .waitForSelector('input[type=tel]', { visible: true })
84
87
  .then(() => false),
@@ -92,7 +95,7 @@ async function waitForTrial(
92
95
  page: Page,
93
96
  attemptCount: number,
94
97
  attemptTimeoutSeconds: number,
95
- attemptScreenshot: boolean,
98
+ screenshot: boolean | string | undefined,
96
99
  logger: Logger
97
100
  ): Promise<void> {
98
101
  for (
@@ -102,7 +105,7 @@ async function waitForTrial(
102
105
  ) {
103
106
  if (attempt > 0) {
104
107
  logger.warn(`Tried on attempt ${attempt}. Waiting to finish...`);
105
- if (attemptScreenshot) await takeScreenshotToDisplay(page);
108
+ await takeScreenshotToDisplay(page, screenshot, logger);
106
109
  }
107
110
  if (attempt > -1) {
108
111
  await setTimeout(1000 * attemptTimeoutSeconds);
@@ -127,8 +130,18 @@ async function takeScreenshotToCompare(
127
130
  }
128
131
  }
129
132
 
130
- async function takeScreenshotToDisplay(page: Page): Promise<void> {
131
- await page.screenshot({
132
- path: `continue-with-google-${new Date(Date.now()).toISOString()}.png`,
133
- });
133
+ async function takeScreenshotToDisplay(
134
+ page: Page,
135
+ mode: boolean | string | undefined,
136
+ logger: Logger
137
+ ): Promise<void> {
138
+ const timestamp = new Date(Date.now()).toISOString();
139
+ if (mode === 'log') {
140
+ const content = await page.evaluate(() => document.body.innerText);
141
+ logger.info(`${timestamp}:\n${content}\n`);
142
+ } else if (mode) {
143
+ const path = `continue-with-google-${timestamp.replace(':', '-')}.png`;
144
+ // @ts-ignore
145
+ await page.screenshot({ path });
146
+ }
134
147
  }