New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@nestlab/google-recaptcha

Package Overview
Dependencies
Maintainers
1
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestlab/google-recaptcha

Google recaptcha module for NestJS.

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12K
increased by1.09%
Maintainers
1
Weekly downloads
 
Created
Source

Google recaptcha module

The NestJS module to protect your endpoints via google recaptcha.

Usage example here

Installation

$ npm i @nestlab/google-recaptcha

Configuration

@Module({
    imports: [
        GoogleRecaptchaModule.forRoot({
            secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY,
            response: req => req.headers.recaptcha,
            skipIf: () => process.env.NODE_ENV !== 'production',
        })
    ],
})
export class AppModule {
}

Usage

Use @Recaptcha decorator to protect your endpoints.


@Controller('feedback')
export class FeedbackController {
    @Recaptcha()
    @Post('send')
    async send(): Promise<any> {
        // TODO: Your implementation.
    }
}

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


@Controller('feedback')
export class FeedbackController {
    @UseGuards(Guard1, GoogleRecaptchaGuard, Guard2)
    @Post('send')
    async send(): Promise<any> {
        // TODO: Your implementation.
    }
}

Error handling

Google recaptcha guard will throw GoogleRecaptchaException on error.

GoogleRecaptchaException has data with google recaptcha error codes.

GoogleRecaptchaExceptionBadRequestExceptionHttpExceptionError.

You can handle it via ExceptionFilter.

Example exception filter implementation.


@Catch(GoogleRecaptchaException)
export class GoogleRecaptchaFilter implements ExceptionFilter {
    catch(exception: GoogleRecaptchaException, host: ArgumentsHost): any {
        const res: Response = host.switchToHttp().getResponse();

        const firstErrorCode = exception.errorCodes.shift();

        const transformedError = this.transformError(firstErrorCode);

        res.status(transformedError.status).send({
            type: 'GoogleRecaptchaError',
            message: transformedError.message,
        });
    }

    transformError(errorCode: ErrorCode): {status: number, message: string} {
        switch (errorCode) {
            case ErrorCode.InvalidInputResponse:
                return {
                    status: HttpStatus.BAD_REQUEST,
                    message: 'The response parameter is invalid or malformed.',
                };

            case ErrorCode.MissingInputResponse:
                return {
                    status: HttpStatus.BAD_REQUEST,
                    message: 'The response parameter is missing.',
                };

            case ErrorCode.TimeoutOrDuplicate:
                return {
                    status: HttpStatus.BAD_REQUEST,
                    message: 'The response is no longer valid: either is too old or has been used previously.',
                };

            case ErrorCode.InvalidInputSecret:
            case ErrorCode.MissingInputSecret:
                return {
                    status: HttpStatus.INTERNAL_SERVER_ERROR,
                    message: 'Invalid module configuration. Please check public-secret keys.',
                };

            case ErrorCode.UnknownError:
            case ErrorCode.BadRequest:
            default:
                return {
                    status: HttpStatus.INTERNAL_SERVER_ERROR,
                    message: 'Unexpected error. Please submit issue to @nestlab/google-recaptcha.',
                };
        }
    }
}

And add your filter to application


async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.useGlobalFilters(new ErrorFilter(), new GoogleRecaptchaFilter());
    await app.listen(3000);
}
bootstrap();


Enjoy!

Keywords

FAQs

Package last updated on 23 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc