Skip to content

Commit

Permalink
Merge pull request #16 from chvarkov/develop
Browse files Browse the repository at this point in the history
Updated skipIf option.
  • Loading branch information
chvarkov authored Nov 1, 2020
2 parents fd91053 + 6279078 commit 822d9cb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ npm i @nestlab/google-recaptcha
GoogleRecaptchaModule.forRoot({
secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY,
response: req => req.headers.recaptcha,
skipIf: async req => process.env.NODE_ENV !== 'production',
skipIf: process.env.NODE_ENV !== 'production',
useRecaptchaNet: false,
agent: null
})
Expand All @@ -36,13 +36,13 @@ export class AppModule {

**Configuration options**

| Property | | Type | Description |
|-------------------|---|---------------------------------------------|-------------|
| `secretKey` | | string | Google recaptcha secret key |
| `response` | | (request) => string | Function that returns response (recaptcha token) by request |
| `skipIf` | | (request) => boolean \| Promise\<boolean\> | Function that returns true if you allow the request to skip the recaptcha verification. Useful for involing other check methods (e.g. custom privileged API key) or for development or testing |
| `useRecaptchaNet` | | boolean | If your server has trouble connecting to https://www.google.com. You can use https://recaptcha.net instead, just set true |
| `agent` | | https.Agent | If you need to use an agent |
| Property | Type | Description |
|-------------------|--------------------------------------------------------|-------------|
| `secretKey` | `string` | **Required.** Google recaptcha secret key |
| `response` | `(request) => string` | **Required.** Function that returns response (recaptcha token) by request |
| `skipIf` | `boolean | (request) => boolean \| Promise\<boolean\>` | Optional. Function that returns true if you allow the request to skip the recaptcha verification. Useful for involing other check methods (e.g. custom privileged API key) or for development or testing |
| `useRecaptchaNet` | `boolean` | Optional. If your server has trouble connecting to https://www.google.com. You can use https://recaptcha.net instead, just set true |
| `agent` | `https.Agent` | Optional. If you need to use an agent |


If you want import configs from your [ConfigService](https://docs.nestjs.com/techniques/configuration#getting-started) via [custom getter function](https://docs.nestjs.com/techniques/configuration#custom-getter-functions) that will return `GoogleRecaptchaModuleOptions` object.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestlab/google-recaptcha",
"version": "1.1.5",
"version": "1.1.6",
"description": "Google recaptcha module for NestJS.",
"keywords": [
"nest",
Expand Down
4 changes: 3 additions & 1 deletion src/guards/google-recaptcha.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export class GoogleRecaptchaGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<true | never> {
const request = context.switchToHttp().getRequest();

const skip = this.options.skipIf ? await this.options.skipIf(request) : false;
const skip = typeof this.options.skipIf === 'function'
? await this.options.skipIf(request)
: !!this.options.skipIf;

if (skip) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/google-recaptcha-guard-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { RecaptchaResponseProvider } from '../types';

export interface GoogleRecaptchaGuardOptions {
response: RecaptchaResponseProvider;
skipIf?: (request: any) => boolean | Promise<boolean>;
skipIf?: boolean | ((request: any) => boolean | Promise<boolean>);
}

0 comments on commit 822d9cb

Please sign in to comment.