@captchacat/svelte 0.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/LICENSE +21 -0
- package/README.md +102 -0
- package/dist/Captchacat.svelte +57 -0
- package/dist/Captchacat.svelte.d.ts +23 -0
- package/dist/global.d.ts +10 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/server.d.ts +13 -0
- package/dist/server.js +30 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Captchacat
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# @captchacat/svelte
|
|
2
|
+
|
|
3
|
+
Svelte/SvelteKit integration for Captchacat.
|
|
4
|
+
|
|
5
|
+
GitHub: https://github.com/Captchacat-Integrations/Svelte
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @captchacat/svelte
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Basic Form (Recommended)
|
|
16
|
+
|
|
17
|
+
The captcha token is automatically added as a hidden `captchacat-token` field when the user completes verification.
|
|
18
|
+
|
|
19
|
+
```svelte
|
|
20
|
+
<script>
|
|
21
|
+
import { Captchacat } from '@captchacat/svelte';
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<form action="/api/login" method="POST">
|
|
25
|
+
<input name="email" type="email" />
|
|
26
|
+
<input name="password" type="password" />
|
|
27
|
+
<Captchacat siteKey="your-site-key" />
|
|
28
|
+
<button type="submit">Login</button>
|
|
29
|
+
</form>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### With Callback (Optional)
|
|
33
|
+
|
|
34
|
+
Use `on:verify` if you need to know when verification completes (e.g., enable submit button).
|
|
35
|
+
|
|
36
|
+
```svelte
|
|
37
|
+
<script lang="ts">
|
|
38
|
+
import { Captchacat } from '@captchacat/svelte';
|
|
39
|
+
|
|
40
|
+
let isVerified = false;
|
|
41
|
+
|
|
42
|
+
function handleVerify(event: CustomEvent<string>) {
|
|
43
|
+
console.log('Token:', event.detail);
|
|
44
|
+
isVerified = true;
|
|
45
|
+
}
|
|
46
|
+
</script>
|
|
47
|
+
|
|
48
|
+
<Captchacat siteKey="your-site-key" on:verify={handleVerify} />
|
|
49
|
+
<button disabled={!isVerified}>Submit</button>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Server-Side Validation
|
|
53
|
+
|
|
54
|
+
Validate the `captchacat-token` from the form submission:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
// src/routes/api/login/+server.ts
|
|
58
|
+
import { validateCaptchacatToken } from "@captchacat/svelte/server";
|
|
59
|
+
import { json, error } from "@sveltejs/kit";
|
|
60
|
+
|
|
61
|
+
export const POST = async ({ request }) => {
|
|
62
|
+
const formData = await request.formData();
|
|
63
|
+
const token = formData.get("captchacat-token") as string;
|
|
64
|
+
|
|
65
|
+
const result = await validateCaptchacatToken({
|
|
66
|
+
apiKey: process.env.CAPTCHACAT_API_KEY!,
|
|
67
|
+
token,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (!result.valid) {
|
|
71
|
+
throw error(403, "Captcha failed");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Captcha valid - proceed with login
|
|
75
|
+
const email = formData.get("email");
|
|
76
|
+
const password = formData.get("password");
|
|
77
|
+
// ...
|
|
78
|
+
|
|
79
|
+
return json({ success: true });
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
### `<Captchacat />`
|
|
86
|
+
|
|
87
|
+
| Prop | Type | Required | Description |
|
|
88
|
+
| --------- | -------- | -------- | -------------------------------- |
|
|
89
|
+
| `siteKey` | `string` | Yes | Your site key from the dashboard |
|
|
90
|
+
|
|
91
|
+
| Event | Payload | Description |
|
|
92
|
+
| -------- | -------- | ------------------------------- |
|
|
93
|
+
| `verify` | `string` | Emitted on verification success |
|
|
94
|
+
|
|
95
|
+
### `validateCaptchacatToken(options)`
|
|
96
|
+
|
|
97
|
+
| Option | Type | Required |
|
|
98
|
+
| -------- | -------- | -------- |
|
|
99
|
+
| `apiKey` | `string` | Yes |
|
|
100
|
+
| `token` | `string` | Yes |
|
|
101
|
+
|
|
102
|
+
Returns: `Promise<{ valid: boolean, message?: string, rawResponse?: unknown }>`
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<script lang="ts">import { onMount, onDestroy } from "svelte";
|
|
2
|
+
import { createEventDispatcher } from "svelte";
|
|
3
|
+
import "./global.d.ts";
|
|
4
|
+
const BASE_URL = "https://challenge.captchacat.com";
|
|
5
|
+
export let siteKey;
|
|
6
|
+
const dispatch = createEventDispatcher();
|
|
7
|
+
let containerRef;
|
|
8
|
+
let callbackName = "";
|
|
9
|
+
onMount(() => {
|
|
10
|
+
callbackName = `captchacat_cb_${Math.random().toString(36).substring(7)}`;
|
|
11
|
+
window[callbackName] = (token) => {
|
|
12
|
+
dispatch("verify", token);
|
|
13
|
+
};
|
|
14
|
+
const scriptUrl = `${BASE_URL}/ray/widget.js`;
|
|
15
|
+
const handleInit = () => {
|
|
16
|
+
if (window.Captchacat?.render && containerRef) {
|
|
17
|
+
window.Captchacat.render(containerRef);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
let script = document.querySelector(
|
|
21
|
+
`script[src="${scriptUrl}"]`
|
|
22
|
+
);
|
|
23
|
+
if (!script) {
|
|
24
|
+
script = document.createElement("script");
|
|
25
|
+
script.src = scriptUrl;
|
|
26
|
+
script.async = true;
|
|
27
|
+
script.onload = handleInit;
|
|
28
|
+
document.body.appendChild(script);
|
|
29
|
+
} else {
|
|
30
|
+
if (window.Captchacat) {
|
|
31
|
+
handleInit();
|
|
32
|
+
} else {
|
|
33
|
+
script.addEventListener("load", handleInit);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
onDestroy(() => {
|
|
38
|
+
if (callbackName && typeof window !== "undefined") {
|
|
39
|
+
delete window[callbackName];
|
|
40
|
+
}
|
|
41
|
+
if (containerRef) {
|
|
42
|
+
containerRef.innerHTML = "";
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
$: if (siteKey && typeof window !== "undefined" && window.Captchacat?.render && containerRef) {
|
|
46
|
+
containerRef.innerHTML = "";
|
|
47
|
+
window.Captchacat.render(containerRef);
|
|
48
|
+
}
|
|
49
|
+
</script>
|
|
50
|
+
|
|
51
|
+
<div
|
|
52
|
+
bind:this={containerRef}
|
|
53
|
+
class="captcha-widget"
|
|
54
|
+
data-sitekey={siteKey}
|
|
55
|
+
data-token-callback={callbackName}
|
|
56
|
+
style="min-height: 48px;"
|
|
57
|
+
></div>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import './global.d.ts';
|
|
2
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
3
|
+
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
|
4
|
+
$$bindings?: Bindings;
|
|
5
|
+
} & Exports;
|
|
6
|
+
(internal: unknown, props: Props & {
|
|
7
|
+
$$events?: Events;
|
|
8
|
+
$$slots?: Slots;
|
|
9
|
+
}): Exports & {
|
|
10
|
+
$set?: any;
|
|
11
|
+
$on?: any;
|
|
12
|
+
};
|
|
13
|
+
z_$$bindings?: Bindings;
|
|
14
|
+
}
|
|
15
|
+
declare const Captchacat: $$__sveltets_2_IsomorphicComponent<{
|
|
16
|
+
siteKey: string;
|
|
17
|
+
}, {
|
|
18
|
+
verify: CustomEvent<string>;
|
|
19
|
+
} & {
|
|
20
|
+
[evt: string]: CustomEvent<any>;
|
|
21
|
+
}, {}, {}, string>;
|
|
22
|
+
type Captchacat = InstanceType<typeof Captchacat>;
|
|
23
|
+
export default Captchacat;
|
package/dist/global.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Captchacat } from './Captchacat.svelte';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Captchacat } from './Captchacat.svelte';
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface ValidationResponse {
|
|
2
|
+
valid: boolean;
|
|
3
|
+
message?: string;
|
|
4
|
+
rawResponse?: unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface ValidationOptions {
|
|
7
|
+
apiKey: string;
|
|
8
|
+
token: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Validates the Captchacat token server-side.
|
|
12
|
+
*/
|
|
13
|
+
export declare function validateCaptchacatToken(options: ValidationOptions): Promise<ValidationResponse>;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates the Captchacat token server-side.
|
|
3
|
+
*/
|
|
4
|
+
export async function validateCaptchacatToken(options) {
|
|
5
|
+
const { apiKey, token } = options;
|
|
6
|
+
try {
|
|
7
|
+
const response = await fetch('https://challenge.captchacat.com/validate_token', {
|
|
8
|
+
method: 'POST',
|
|
9
|
+
headers: {
|
|
10
|
+
'Content-Type': 'application/json',
|
|
11
|
+
},
|
|
12
|
+
body: JSON.stringify({
|
|
13
|
+
api_key: apiKey,
|
|
14
|
+
token: token,
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
return { valid: false, message: `Server error: ${response.status}` };
|
|
19
|
+
}
|
|
20
|
+
const text = await response.text();
|
|
21
|
+
const data = text ? JSON.parse(text) : null;
|
|
22
|
+
return { valid: true, rawResponse: data };
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
return {
|
|
26
|
+
valid: false,
|
|
27
|
+
message: error instanceof Error ? error.message : 'Unknown validation error',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@captchacat/svelte",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Svelte/SvelteKit integration for Captchacat",
|
|
5
|
+
"author": "Captchacat",
|
|
6
|
+
"homepage": "https://captchacat.com",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"svelte": "./dist/index.js",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"svelte": "./dist/index.js",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./server": {
|
|
19
|
+
"types": "./dist/server.d.ts",
|
|
20
|
+
"import": "./dist/server.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"dev": "vite dev",
|
|
28
|
+
"build": "vite build && npm run package",
|
|
29
|
+
"package": "svelte-kit sync && svelte-package -o dist",
|
|
30
|
+
"prepublishOnly": "npm run package",
|
|
31
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"svelte": "^4.0.0 || ^5.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@sveltejs/adapter-auto": "^3.0.0",
|
|
38
|
+
"@sveltejs/kit": "^2.0.0",
|
|
39
|
+
"@sveltejs/package": "^2.0.0",
|
|
40
|
+
"@sveltejs/vite-plugin-svelte": "^3.0.0",
|
|
41
|
+
"svelte": "^5.0.0",
|
|
42
|
+
"svelte-check": "^4.0.0",
|
|
43
|
+
"typescript": "^5.3.0",
|
|
44
|
+
"vite": "^5.0.0"
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"captcha",
|
|
48
|
+
"captchacat",
|
|
49
|
+
"svelte",
|
|
50
|
+
"sveltekit",
|
|
51
|
+
"bot-detection"
|
|
52
|
+
],
|
|
53
|
+
"license": "MIT",
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "https://github.com/Captchacat-Integrations/Svelte"
|
|
57
|
+
}
|
|
58
|
+
}
|