Skip to content

Commit

Permalink
feat: add basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
exzos28 committed May 9, 2024
1 parent 50c05ac commit beae39b
Show file tree
Hide file tree
Showing 18 changed files with 18,949 additions and 39 deletions.
41 changes: 31 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,45 @@ Google reCAPTCHA provider for react native projects
## Installation

```sh
npm install react-native-google-recaptcha-provider
yarn add react-native-google-recaptcha-provider
```

## Usage
## Overview

```js
import { multiply } from 'react-native-google-recaptcha-provider';
_react-native-google-recaptcha-provider_ is a library for integrating Google reCAPTCHA into React Native applications. It provides a convenient way to add and customize reCAPTCHA in your application to prevent spam and abuse.

// ...
## Example

const result = await multiply(3, 7);
```
[You can find a usage example here](./example)

## Customization

The library provides various customization options such as specifying the size, theme, language, and action associated with the reCAPTCHA widget.

## Contributing
## Method and Field Descriptions

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
### __GoogleRecaptchaProps__
| Prop Name | Type | Required | Description |
|--------------------|--------------------------------------------------------|----------|--------------------------------------------------------------------------------------------------|
| siteKey | string | Yes | The site key obtained from Google reCAPTCHA. |
| baseUrl | string | Yes | The base URL of the website where reCAPTCHA is being used. |
| onVerify | (token: string) => void | Yes | Callback function invoked when reCAPTCHA token is successfully verified. |
| onExpire | () => void | No | Callback function invoked when the reCAPTCHA token expires. |
| onError | (error: any) => void | No | Callback function invoked when an error occurs during reCAPTCHA verification. |
| onClose | () => void | No | Callback function invoked when the reCAPTCHA widget is closed without completing. |
| onLoad | () => void | No | Callback function invoked when the WebView finishes loading the HTML content. |
| loadingComponent | ReactNode | No | A custom loading component to display while reCAPTCHA is loading. |
| webViewProps | Omit<WebViewProps, 'source' \| 'style' \| 'onMessage'> | No | Additional props to be passed to the underlying WebView component. |
| lang | string | No | The language code to use for reCAPTCHA. |
| size | 'normal' \| 'compact' \| 'invisible' | No | The size of the reCAPTCHA widget. Possible values: 'normal', 'compact', 'invisible'. Default is 'normal'. |
| theme | 'light' \| 'dark' | No | The theme of the reCAPTCHA widget. Possible values: 'light', 'dark'. Default is 'light'. |
| enterprise | boolean | No | Specifies whether to use the new reCAPTCHA Enterprise API. Default is false. |
| action | string | No | An additional parameter for specifying the action name associated with the protected element. |
| recaptchaDomain | string | No | The domain of the reCAPTCHA service. Defaults to 'www.google.com'. |
| gstaticDomain | string | No | The domain of the Google static content. Defaults to 'www.gstatic.com'. |
| hideBadge | boolean | No | Specifies whether to hide the reCAPTCHA badge. Defaults to false. |
| style | StyleProp\<ViewStyle\> | No | The style object or stylesheet for the root container of the component. |

## License

MIT

Expand Down
13 changes: 13 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Example Usage

The reCAPTCHA component is implemented in such a way that it stretches to the full available width and height by default. Users are expected to implement their own modal window for displaying the reCAPTCHA challenge.

## Implementation

To see an example implementation of how to use `react-native-google-recaptcha-provider`, you can refer to the `ModalExample.tsx`. This file demonstrates how to integrate the reCAPTCHA component within a modal window.


## Screenshots

<img src="../screenshot/1.png" alt="1" width="300"/>
<img src="../screenshot/2.png" alt="1" width="300"/>
1 change: 1 addition & 0 deletions example/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function (api) {
},
},
],
['module:react-native-dotenv'],
],
};
};
11 changes: 7 additions & 4 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
"expo": "~50.0.17",
"expo-status-bar": "~1.11.1",
"react": "18.2.0",
"react-native": "0.73.6",
"react-dom": "18.2.0",
"react-native-web": "~0.19.6"
"react-native": "0.73.6",
"react-native-safe-area-context": "^4.10.1",
"react-native-web": "~0.19.6",
"react-native-webview": "^13.8.7"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"babel-plugin-module-resolver": "^5.0.0",
"@expo/webpack-config": "^18.0.1",
"babel-loader": "^8.1.0"
"babel-loader": "^8.1.0",
"babel-plugin-module-resolver": "^5.0.0",
"react-native-dotenv": "^3.4.11"
},
"private": true
}
47 changes: 29 additions & 18 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import * as React from 'react';
import React, { useState } from 'react';

import { StyleSheet, View, Text } from 'react-native';
import { multiply } from 'react-native-google-recaptcha-provider';
import { StyleSheet, Button, Alert } from 'react-native';
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
import { RecaptchaModal } from './ModalExample';

export default function App() {
const [result, setResult] = React.useState<number | undefined>();

React.useEffect(() => {
multiply(3, 7).then(setResult);
}, []);
export default function Root() {
return (
<SafeAreaProvider style={styles.root}>
<App />
</SafeAreaProvider>
);
}

function App() {
const [modalIsVisible, setModalIsVisible] = useState(false);
return (
<View style={styles.container}>
<Text>Result: {result}</Text>
</View>
<SafeAreaView style={styles.container}>
<Button title="Open modal" onPress={() => setModalIsVisible(true)} />
{modalIsVisible && (
<RecaptchaModal
onClose={() => setModalIsVisible(false)}
onVerify={(_) => {
setModalIsVisible(false);
Alert.alert('Token', _);
}}
/>
)}
</SafeAreaView>
);
}

const styles = StyleSheet.create({
root: {
flex: 1,
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
box: {
width: 60,
height: 60,
marginVertical: 20,
alignItems: 'center',
},
});
47 changes: 47 additions & 0 deletions example/src/ModalExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { RecaptchaProps } from 'react-native-google-recaptcha-provider';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import React from 'react';
import { RecaptchaProvider } from './RecaptchaProvider';

type RecaptchaModalProps = Pick<RecaptchaProps, 'onVerify' | 'onClose'> & {};
export const RecaptchaModal = (props: RecaptchaModalProps) => {
const insets = useSafeAreaInsets();
const recaptchaTop = insets.top + CLOSE_SIZE;
return (
<View style={styles.modal}>
<RecaptchaProvider
style={{ top: recaptchaTop, bottom: insets.bottom }}
{...props}
/>
<TouchableOpacity
hitSlop={10}
onPress={props.onClose}
style={[styles.close, { marginTop: insets.top }]}
>
<Text style={styles.closeText}>&times;</Text>
</TouchableOpacity>
</View>
);
};

const CLOSE_SIZE = 50;
const styles = StyleSheet.create({
modal: {
...StyleSheet.absoluteFillObject,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
close: {
justifyContent: 'center',
alignItems: 'center',
height: CLOSE_SIZE,
width: CLOSE_SIZE,
marginLeft: 'auto',
marginRight: 20,
borderRadius: 25,
backgroundColor: '#ffffff',
},
closeText: {
fontSize: 24,
},
});
14 changes: 14 additions & 0 deletions example/src/RecaptchaProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
Recaptcha,
type RecaptchaProps,
} from 'react-native-google-recaptcha-provider';
import React from 'react';
import { BASE_URL, SITE_KEY } from '@env';

export const RecaptchaProvider = (
props: Omit<RecaptchaProps, 'siteKey' | 'baseUrl'>
) => {
return (
<Recaptcha lang="en" siteKey={SITE_KEY} baseUrl={BASE_URL} {...props} />
);
};
2 changes: 1 addition & 1 deletion example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../tsconfig",
"compilerOptions": {
// Avoid expo-cli auto-generating a tsconfig
"typeRoots": ["./types"]
}
}
4 changes: 4 additions & 0 deletions example/types/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '@env' {
export const SITE_KEY: string;
export const BASE_URL: string;
}
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-google-recaptcha-provider",
"version": "0.1.0",
"description": "Google reCAPTCHA provider for react native projects",
"description": "Google reCAPTCHA provider for react native (android & iOS) projects",
"main": "lib/commonjs/index",
"module": "lib/module/index",
"types": "lib/typescript/src/index.d.ts",
Expand Down Expand Up @@ -35,9 +35,13 @@
"release": "release-it"
},
"keywords": [
"recaptcha",
"google recaptcha",
"recaptcha provider",
"react-native",
"ios",
"android"
"android",
"enterprise"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -69,6 +73,7 @@
"react": "18.2.0",
"react-native": "0.73.6",
"react-native-builder-bob": "^0.23.2",
"react-native-webview": "^13.8.7",
"release-it": "^15.0.0",
"typescript": "^5.2.2"
},
Expand All @@ -77,7 +82,8 @@
},
"peerDependencies": {
"react": "*",
"react-native": "*"
"react-native": "*",
"react-native-webview": "*"
},
"workspaces": [
"example"
Expand Down
Binary file added screenshot/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit beae39b

Please sign in to comment.