@thetypefounders/continue-with-google 1.0.2 → 1.1.0

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/README.md CHANGED
@@ -11,9 +11,9 @@ npm install @thetypefounders/continue-with-google --save
11
11
  ## Usage
12
12
 
13
13
  ```javascript
14
+ import { authenticate } from '@thetypefounders/continue-with-google';
14
15
  import Puppeteer from 'puppeteer-extra';
15
16
  import StealthPlugin from 'puppeteer-extra-plugin-stealth';
16
- import { authenticate } from '@thetypefounders/continue-with-google';
17
17
 
18
18
  Puppeteer.use(StealthPlugin());
19
19
 
package/dist/index.d.ts CHANGED
@@ -2,4 +2,10 @@ import { type ElementHandle, Page } from 'puppeteer';
2
2
  export interface Logger {
3
3
  info(message: string): void;
4
4
  }
5
- export declare function authenticate(page: Page, email: string, password: string, secret: string, target: string, logger?: Logger, attemptCount?: number, attemptSeconds?: number): Promise<ElementHandle | null>;
5
+ export type Options = {
6
+ challengeCount?: number;
7
+ challengeTimeoutSeconds?: number;
8
+ trialCount?: number;
9
+ trialTimeoutSeconds?: number;
10
+ };
11
+ 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
@@ -1,6 +1,12 @@
1
1
  import { generateToken } from 'authenticator';
2
2
  import { setTimeout } from 'node:timers/promises';
3
- export async function authenticate(page, email, password, secret, target, logger = console, attemptCount = 3, attemptSeconds = 30) {
3
+ const DEFAULTS = {
4
+ challengeCount: 3,
5
+ challengeTimeoutSeconds: 30,
6
+ trialCount: 10,
7
+ trialTimeoutSeconds: 2,
8
+ };
9
+ export async function authenticate(page, email, password, secret, selector, options = DEFAULTS, logger = console) {
4
10
  logger.info('Waiting to enter the email...');
5
11
  await page.waitForSelector('input[type=email]', { visible: true });
6
12
  logger.info('Entering the email...');
@@ -11,11 +17,13 @@ export async function authenticate(page, email, password, secret, target, logger
11
17
  logger.info('Entering the password...');
12
18
  await page.type('input[type=password]', password);
13
19
  await page.keyboard.press('Enter');
14
- for (let attempt = 0, found = false; attempt < attemptCount && !found; attempt++) {
20
+ for (let attempt = 0, found = false; attempt < (options.challengeCount || DEFAULTS.challengeCount) && !found; attempt++) {
15
21
  if (attempt > 0) {
16
22
  logger.info(`Challenged on attempt ${attempt}. Entering the code...`);
17
23
  if (attempt > 1) {
18
- await setTimeout(1000 * attemptSeconds);
24
+ await setTimeout(1000 *
25
+ (options.challengeTimeoutSeconds ||
26
+ DEFAULTS.challengeTimeoutSeconds));
19
27
  }
20
28
  const code = generateToken(secret);
21
29
  await page.evaluate(() => {
@@ -24,24 +32,24 @@ export async function authenticate(page, email, password, secret, target, logger
24
32
  });
25
33
  await page.type('input[type=tel]', code);
26
34
  await page.keyboard.press('Enter');
27
- await waitForPeace(page, logger);
35
+ await waitForTrial(page, options.trialCount || DEFAULTS.trialCount, options.trialTimeoutSeconds || DEFAULTS.trialTimeoutSeconds, logger);
28
36
  }
29
37
  found = await Promise.any([
30
- page.waitForSelector(target).then(() => true),
38
+ page.waitForSelector(selector).then(() => true),
31
39
  page
32
40
  .waitForSelector('input[type=tel]', { visible: true })
33
41
  .then(() => false),
34
42
  ]);
35
43
  }
36
- return await page.$(target);
44
+ return await page.$(selector);
37
45
  }
38
- async function waitForPeace(page, logger, attemptCount = 10, attemptSeconds = 2) {
46
+ async function waitForTrial(page, attemptCount, attemptTimeoutSeconds, logger) {
39
47
  for (let attempt = -1, previous = undefined, current = undefined; attempt < attemptCount && (current === undefined || previous !== current); attempt++) {
40
48
  if (attempt > 0) {
41
- logger.info(`Changed on attempt ${attempt}. Taking a screenshot...`);
49
+ logger.info(`Tried on attempt ${attempt}. Taking a screenshot...`);
42
50
  }
43
51
  if (attempt > -1) {
44
- await setTimeout(1000 * attemptSeconds);
52
+ await setTimeout(1000 * attemptTimeoutSeconds);
45
53
  }
46
54
  const future = await screenshot(page);
47
55
  if (future) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thetypefounders/continue-with-google",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
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
@@ -6,15 +6,28 @@ export interface Logger {
6
6
  info(message: string): void;
7
7
  }
8
8
 
9
+ export type Options = {
10
+ challengeCount?: number;
11
+ challengeTimeoutSeconds?: number;
12
+ trialCount?: number;
13
+ trialTimeoutSeconds?: number;
14
+ };
15
+
16
+ const DEFAULTS: Options = {
17
+ challengeCount: 3,
18
+ challengeTimeoutSeconds: 30,
19
+ trialCount: 10,
20
+ trialTimeoutSeconds: 2,
21
+ };
22
+
9
23
  export async function authenticate(
10
24
  page: Page,
11
25
  email: string,
12
26
  password: string,
13
27
  secret: string,
14
- target: string,
15
- logger: Logger = console,
16
- attemptCount: number = 3,
17
- attemptSeconds: number = 30
28
+ selector: string,
29
+ options: Options = DEFAULTS,
30
+ logger: Logger = console
18
31
  ): Promise<ElementHandle | null> {
19
32
  logger.info('Waiting to enter the email...');
20
33
  await page.waitForSelector('input[type=email]', { visible: true });
@@ -30,13 +43,17 @@ export async function authenticate(
30
43
 
31
44
  for (
32
45
  let attempt = 0, found = false;
33
- attempt < attemptCount && !found;
46
+ attempt < (options.challengeCount || DEFAULTS.challengeCount!) && !found;
34
47
  attempt++
35
48
  ) {
36
49
  if (attempt > 0) {
37
50
  logger.info(`Challenged on attempt ${attempt}. Entering the code...`);
38
51
  if (attempt > 1) {
39
- await setTimeout(1000 * attemptSeconds);
52
+ await setTimeout(
53
+ 1000 *
54
+ (options.challengeTimeoutSeconds ||
55
+ DEFAULTS.challengeTimeoutSeconds!)
56
+ );
40
57
  }
41
58
  const code = generateToken(secret);
42
59
  await page.evaluate(() => {
@@ -45,24 +62,29 @@ export async function authenticate(
45
62
  });
46
63
  await page.type('input[type=tel]', code);
47
64
  await page.keyboard.press('Enter');
48
- await waitForPeace(page, logger);
65
+ await waitForTrial(
66
+ page,
67
+ options.trialCount || DEFAULTS.trialCount!,
68
+ options.trialTimeoutSeconds || DEFAULTS.trialTimeoutSeconds!,
69
+ logger
70
+ );
49
71
  }
50
72
  found = await Promise.any([
51
- page.waitForSelector(target).then(() => true),
73
+ page.waitForSelector(selector).then(() => true),
52
74
  page
53
75
  .waitForSelector('input[type=tel]', { visible: true })
54
76
  .then(() => false),
55
77
  ]);
56
78
  }
57
79
 
58
- return await page.$(target);
80
+ return await page.$(selector);
59
81
  }
60
82
 
61
- async function waitForPeace(
83
+ async function waitForTrial(
62
84
  page: Page,
63
- logger: Logger,
64
- attemptCount: number = 10,
65
- attemptSeconds: number = 2
85
+ attemptCount: number,
86
+ attemptTimeoutSeconds: number,
87
+ logger: Logger
66
88
  ): Promise<void> {
67
89
  for (
68
90
  let attempt = -1, previous = undefined, current = undefined;
@@ -70,10 +92,10 @@ async function waitForPeace(
70
92
  attempt++
71
93
  ) {
72
94
  if (attempt > 0) {
73
- logger.info(`Changed on attempt ${attempt}. Taking a screenshot...`);
95
+ logger.info(`Tried on attempt ${attempt}. Taking a screenshot...`);
74
96
  }
75
97
  if (attempt > -1) {
76
- await setTimeout(1000 * attemptSeconds);
98
+ await setTimeout(1000 * attemptTimeoutSeconds);
77
99
  }
78
100
  const future = await screenshot(page);
79
101
  if (future) {