@seereal/better-auth 0.0.0-stage → 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/README.md +43 -2
- package/dist/index.d.ts +56 -0
- package/dist/index.js +77 -0
- package/package.json +53 -6
package/README.md
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @seereal/better-auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Better Auth plugin that stops throwaway emails at signup, using [SeeReal](https://seereal.email).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { betterAuth } from "better-auth";
|
|
7
|
+
import { seereal } from "@seereal/better-auth";
|
|
8
|
+
|
|
9
|
+
export const auth = betterAuth({
|
|
10
|
+
// ...your config
|
|
11
|
+
plugins: [seereal()],
|
|
12
|
+
});
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Put your key in `SEEREAL_API_KEY` and that's it. If an address gets rejected, Better Auth answers with a 400, `code: "SEEREAL_EMAIL_REJECTED"` and a message you can show to the user directly.
|
|
16
|
+
|
|
17
|
+
By default it checks email sign-up, magic link, email OTP and change email.
|
|
18
|
+
|
|
19
|
+
## Options
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
seereal({
|
|
23
|
+
apiKey: "sr_beta_...", // default: process.env.SEEREAL_API_KEY
|
|
24
|
+
timeoutMs: 3000, // after this the signup just goes through
|
|
25
|
+
blockDisposable: true,
|
|
26
|
+
blockUndeliverable: true, // domains that can't receive mail
|
|
27
|
+
blockTypos: true, // "Did you mean ...?"
|
|
28
|
+
blockRelays: false, // Hide My Email etc. are real people
|
|
29
|
+
paths: ["/sign-up/email"], // only guard these endpoints
|
|
30
|
+
decide: (result) => null, // your own rule: return a message to block, null to allow
|
|
31
|
+
onError: (err, email) => console.warn(err),
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`checkEmail(email, options)` is exported as well if you want to check addresses somewhere else.
|
|
36
|
+
|
|
37
|
+
## Tests
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
npm install
|
|
41
|
+
npm test
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The tests start a real Better Auth instance with an in-memory database and a fake SeeReal API.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** What the SeeReal API returns for one address (POST /api/v1/check). */
|
|
2
|
+
export interface SeeRealResult {
|
|
3
|
+
email: string;
|
|
4
|
+
domain: string;
|
|
5
|
+
action: "allow" | "review" | "block";
|
|
6
|
+
verdict: string;
|
|
7
|
+
is_disposable: boolean;
|
|
8
|
+
risk_score: number;
|
|
9
|
+
reasons: string[];
|
|
10
|
+
suggestion: string | null;
|
|
11
|
+
relay_provider: string | null;
|
|
12
|
+
latency_ms: number;
|
|
13
|
+
}
|
|
14
|
+
export interface SeeRealOptions {
|
|
15
|
+
/** Your SeeReal key (sr_beta_...). Defaults to process.env.SEEREAL_API_KEY. */
|
|
16
|
+
apiKey?: string;
|
|
17
|
+
/** API endpoint. Defaults to https://app.seereal.email/api/v1/check. */
|
|
18
|
+
endpoint?: string;
|
|
19
|
+
/** Give up after this many milliseconds and let the signup through. Default 3000. */
|
|
20
|
+
timeoutMs?: number;
|
|
21
|
+
/** Block throwaway inboxes. Default true. */
|
|
22
|
+
blockDisposable?: boolean;
|
|
23
|
+
/** Block domains that cannot receive mail. Default true. */
|
|
24
|
+
blockUndeliverable?: boolean;
|
|
25
|
+
/** Reject typos like gmial.com with a "Did you mean ...?" message. Default true. */
|
|
26
|
+
blockTypos?: boolean;
|
|
27
|
+
/** Also block privacy relays (Hide My Email, Firefox Relay). Default false: they forward to real people. */
|
|
28
|
+
blockRelays?: boolean;
|
|
29
|
+
/** Which Better Auth endpoints to guard. Default: email sign-up, magic link, email OTP and change email. */
|
|
30
|
+
paths?: string[];
|
|
31
|
+
/** Custom decision: return an error message to block, or null to allow. Runs instead of the built-in rules. */
|
|
32
|
+
decide?: (result: SeeRealResult) => string | null | Promise<string | null>;
|
|
33
|
+
/** Called when the check could not run (no key, timeout, quota, network). The signup always goes through. */
|
|
34
|
+
onError?: (error: unknown, email: string) => void;
|
|
35
|
+
/** For tests: a fetch implementation. Defaults to the global fetch. */
|
|
36
|
+
fetch?: typeof fetch;
|
|
37
|
+
}
|
|
38
|
+
/** Checks one address. Returns null when the check could not run, so callers can fail open. */
|
|
39
|
+
export declare function checkEmail(email: string, options?: SeeRealOptions): Promise<SeeRealResult | null>;
|
|
40
|
+
/** The built-in rules. Returns the message to show, or null to allow. */
|
|
41
|
+
export declare function defaultDecision(result: SeeRealResult, options?: SeeRealOptions): string | null;
|
|
42
|
+
/**
|
|
43
|
+
* Better Auth plugin: checks the email on sign-up (and magic link, email OTP, change email) with SeeReal
|
|
44
|
+
* and rejects throwaway, undeliverable and mistyped addresses with a 400 and a clear message.
|
|
45
|
+
* Fails open: if SeeReal cannot be reached, the request goes through.
|
|
46
|
+
*/
|
|
47
|
+
export declare const seereal: (options?: SeeRealOptions) => {
|
|
48
|
+
id: "seereal";
|
|
49
|
+
hooks: {
|
|
50
|
+
before: {
|
|
51
|
+
matcher: (context: import("better-auth").HookEndpointContext) => boolean;
|
|
52
|
+
handler: import("better-auth").Middleware<import("better-auth").MiddlewareOptions, (inputContext: import("better-auth").MiddlewareInputContext<import("better-auth").MiddlewareOptions>) => Promise<void>>;
|
|
53
|
+
}[];
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
export default seereal;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { APIError, createAuthMiddleware } from "better-auth/api";
|
|
2
|
+
const DEFAULT_PATHS = ["/sign-up/email", "/sign-in/magic-link", "/email-otp/send-verification-otp", "/change-email"];
|
|
3
|
+
/** Checks one address. Returns null when the check could not run, so callers can fail open. */
|
|
4
|
+
export async function checkEmail(email, options = {}) {
|
|
5
|
+
const apiKey = options.apiKey ?? (typeof process !== "undefined" ? process.env.SEEREAL_API_KEY : undefined);
|
|
6
|
+
const doFetch = options.fetch ?? fetch;
|
|
7
|
+
if (!apiKey) {
|
|
8
|
+
options.onError?.(new Error("SeeReal: no API key (set SEEREAL_API_KEY or pass apiKey)"), email);
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
const res = await doFetch(options.endpoint ?? "https://app.seereal.email/api/v1/check", {
|
|
13
|
+
method: "POST",
|
|
14
|
+
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
|
|
15
|
+
body: JSON.stringify({ email }),
|
|
16
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? 3000),
|
|
17
|
+
});
|
|
18
|
+
const body = (await res.json().catch(() => null));
|
|
19
|
+
if (!res.ok || !body || typeof body.action !== "string") {
|
|
20
|
+
options.onError?.(new Error(`SeeReal: ${body?.error?.code ?? `HTTP ${res.status}`}`), email);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return body;
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
options.onError?.(err, email);
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** The built-in rules. Returns the message to show, or null to allow. */
|
|
31
|
+
export function defaultDecision(result, options = {}) {
|
|
32
|
+
if (result.is_disposable && options.blockDisposable !== false) {
|
|
33
|
+
return "Please use a permanent email address. Temporary inboxes are not accepted.";
|
|
34
|
+
}
|
|
35
|
+
if (result.relay_provider && options.blockRelays === true) {
|
|
36
|
+
return "Please use your regular email address instead of a forwarding alias.";
|
|
37
|
+
}
|
|
38
|
+
if (result.action === "block" && options.blockUndeliverable !== false) {
|
|
39
|
+
return "This email domain cannot receive mail. Please check the address.";
|
|
40
|
+
}
|
|
41
|
+
if (result.suggestion && options.blockTypos !== false) {
|
|
42
|
+
return `Did you mean ${result.suggestion}? Please check the address.`;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Better Auth plugin: checks the email on sign-up (and magic link, email OTP, change email) with SeeReal
|
|
48
|
+
* and rejects throwaway, undeliverable and mistyped addresses with a 400 and a clear message.
|
|
49
|
+
* Fails open: if SeeReal cannot be reached, the request goes through.
|
|
50
|
+
*/
|
|
51
|
+
export const seereal = (options = {}) => {
|
|
52
|
+
const paths = new Set(options.paths ?? DEFAULT_PATHS);
|
|
53
|
+
return {
|
|
54
|
+
id: "seereal",
|
|
55
|
+
hooks: {
|
|
56
|
+
before: [
|
|
57
|
+
{
|
|
58
|
+
matcher: (context) => paths.has(context.path ?? ""),
|
|
59
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
60
|
+
const body = (ctx.body ?? {});
|
|
61
|
+
const email = typeof body.newEmail === "string" ? body.newEmail : body.email;
|
|
62
|
+
if (typeof email !== "string" || !email.includes("@"))
|
|
63
|
+
return;
|
|
64
|
+
const result = await checkEmail(email.trim().toLowerCase(), options);
|
|
65
|
+
if (!result)
|
|
66
|
+
return;
|
|
67
|
+
const message = options.decide ? await options.decide(result) : defaultDecision(result, options);
|
|
68
|
+
if (message) {
|
|
69
|
+
throw new APIError("BAD_REQUEST", { message, code: "SEEREAL_EMAIL_REJECTED" });
|
|
70
|
+
}
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
export default seereal;
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@seereal/better-auth",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@seereal/better-auth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Better Auth plugin that blocks throwaway (disposable) emails at sign-up with SeeReal, including new temp-mail domains that blocklists miss.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"better-auth",
|
|
21
|
+
"disposable-email",
|
|
22
|
+
"temporary-email",
|
|
23
|
+
"signup",
|
|
24
|
+
"fraud",
|
|
25
|
+
"seereal"
|
|
26
|
+
],
|
|
27
|
+
"homepage": "https://seereal.email",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc -p tsconfig.json",
|
|
30
|
+
"test": "npm run build && node --test test/plugin.test.mjs",
|
|
31
|
+
"prepublishOnly": "npm test"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"better-auth": ">=1.2.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"better-auth": "^1.3.0",
|
|
38
|
+
"typescript": "^5.6.0",
|
|
39
|
+
"@types/node": "^22.0.0"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/SeeReal-email/integrations.git",
|
|
44
|
+
"directory": "better-auth"
|
|
45
|
+
},
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/SeeReal-email/integrations/issues"
|
|
48
|
+
},
|
|
49
|
+
"author": "SeeReal <silas@seereal.email>",
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
}
|
|
53
|
+
}
|