Skip to content

Commit

Permalink
Merge pull request #119 from chvarkov/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
chvarkov authored Apr 15, 2023
2 parents bc8b43b + 77ad00d commit 9798f3c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 61 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v3.3.1
- Reworked readme docs.
- Added changelog file.

## v3.3.0
- Reworked to use axios instead of @nestjs/axios.
- Removed peer dependencies:
Expand Down
117 changes: 58 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,58 +290,9 @@ export class AppModule {

## Usage

### Validate in service

```typescript
@Injectable()
export class SomeService {
constructor(private readonly recaptchaValidator: GoogleRecaptchaValidator) {
}

async someAction(recaptchaToken: string): Promise<void> {
const result = await this.recaptchaValidator.validate({
response: recaptchaToken,
score: 0.8,
action: 'SomeAction',
});

if (!result.success) {
throw new GoogleRecaptchaException(result.errors);
}
// TODO: Your implemetation
}
}
```

### Validate in service (Enterprise)

```typescript
@Injectable()
export class SomeService {
constructor(private readonly recaptchaEnterpriseValidator: GoogleRecaptchaEnterpriseValidator) {
}

async someAction(recaptchaToken: string): Promise<void> {
const result = await this.recaptchaEnterpriseValidator.validate({
response: recaptchaToken,
score: 0.8,
action: 'SomeAction',
});

if (!result.success) {
throw new GoogleRecaptchaException(result.errors);
}

const riskAnalytics = result.getEnterpriseRiskAnalytics();

// TODO: Your implemetation
}
}
```

### Usage in REST application

Use `@Recaptcha` decorator to protect your endpoints.
To protect your REST endpoints, you can use the `@Recaptcha` decorator.<br/>Example:

```typescript

Expand All @@ -356,7 +307,7 @@ export class FeedbackController {

```

You can override default property that contain recaptcha for specific endpoint.
You can also override the default property that contains reCAPTCHA for a specific endpoint.<br/>

```typescript

Expand All @@ -371,7 +322,7 @@ export class FeedbackController {

```

Also you can override recaptcha v3 options.
Additionally, you can override reCAPTCHA v3 options.<br/>

```typescript

Expand All @@ -386,7 +337,7 @@ export class FeedbackController {

```

Get verification result
To get the verification result, you can use the @RecaptchaResult() decorator.<br/>

```typescript

Expand All @@ -402,8 +353,7 @@ export class FeedbackController {

```

If you want use google recaptcha guard in combination with another guards then you can use `@UseGuards` decorator.

If you want to use the Google reCAPTCHA guard in combination with other guards, you can use the `@UseGuards` decorator.<br/>
```typescript

@Controller('feedback')
Expand All @@ -418,11 +368,11 @@ export class FeedbackController {

```

Usage example you can find [here](https://github.com/chvarkov/google-recaptcha-example).
You can find a usage example in the following [link](https://github.com/chvarkov/google-recaptcha-example).

### Usage in Graphql application

Use `@Recaptcha` decorator to protect your resolver.
To protect your resolver, use the `@Recaptcha` decorator.

```typescript
@Recaptcha()
Expand All @@ -435,7 +385,7 @@ export class RecipesResolver {
}
```

Get verification result
Obtain verification result:

```typescript
@Recaptcha()
Expand All @@ -450,7 +400,7 @@ export class RecipesResolver {
}
```

You can override default property that contain recaptcha for specific query, mutation or subscription.
You can override the default recaptcha property for a specific endpoint.

```typescript
@Recaptcha()
Expand All @@ -470,6 +420,55 @@ export class RecipesResolver {
}
```

### Validate in service

```typescript
@Injectable()
export class SomeService {
constructor(private readonly recaptchaValidator: GoogleRecaptchaValidator) {
}

async someAction(recaptchaToken: string): Promise<void> {
const result = await this.recaptchaValidator.validate({
response: recaptchaToken,
score: 0.8,
action: 'SomeAction',
});

if (!result.success) {
throw new GoogleRecaptchaException(result.errors);
}
// TODO: Your implemetation
}
}
```

### Validate in service (Enterprise)

```typescript
@Injectable()
export class SomeService {
constructor(private readonly recaptchaEnterpriseValidator: GoogleRecaptchaEnterpriseValidator) {
}

async someAction(recaptchaToken: string): Promise<void> {
const result = await this.recaptchaEnterpriseValidator.validate({
response: recaptchaToken,
score: 0.8,
action: 'SomeAction',
});

if (!result.success) {
throw new GoogleRecaptchaException(result.errors);
}

const riskAnalytics = result.getEnterpriseRiskAnalytics();

// TODO: Your implemetation
}
}
```

### Error handling

**GoogleRecaptchaException**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestlab/google-recaptcha",
"version": "3.3.0",
"version": "3.3.1",
"description": "Google recaptcha module for NestJS.",
"keywords": [
"nestjs",
Expand All @@ -11,7 +11,7 @@
"private": false,
"main": "index.js",
"scripts": {
"build": "rimraf dist && tsc && cp package.json dist && cp README.md dist && cp LICENSE dist && cp CONTRIBUTING.md dist",
"build": "rimraf dist && tsc && cp package.json dist && cp README.md dist && cp LICENSE dist && cp CONTRIBUTING.md dist && cp CHANGELOG.md dist",
"format": "prettier \"**/*.ts\" \"**/*.json\" --ignore-path ./.prettierignore --write",
"lint:fix": "eslint . --fix",
"lint:check": "eslint . --max-warnings=0",
Expand Down

0 comments on commit 9798f3c

Please sign in to comment.