Refactor billing imports, reorganize package scripts and improve action structure

Deleted unnecessary schema files and reorganized their imports into more logical order. Modified the package script structure to align more accurately with standard conventions. Also refactored the team-billing.service file to improve action structure, making it easier to understand and edit. Furthermore, upgraded various dependencies, reflecting their new versions in the lockfile.
This commit is contained in:
giancarlo
2024-04-07 12:47:29 +08:00
parent 0a9c1f35c6
commit 0002ac6255
22 changed files with 244 additions and 82 deletions

View File

@@ -14,7 +14,8 @@
"./password-reset": "./src/password-reset.ts",
"./shared": "./src/shared.ts",
"./mfa": "./src/mfa.ts",
"./captcha": "./src/components/captcha/index.ts"
"./captcha/client": "./src/captcha/client/index.ts",
"./captcha/server": "./src/captcha/server/index.ts"
},
"devDependencies": {
"@hookform/resolvers": "^3.3.4",

View File

@@ -0,0 +1 @@
export * from './verify-captcha';

View File

@@ -0,0 +1,30 @@
import 'server-only';
const verifyEndpoint =
'https://challenges.cloudflare.com/turnstile/v0/siteverify';
const secret = process.env.CAPTCHA_SECRET_TOKEN;
/**
* Verify the CAPTCHA token with the CAPTCHA service
* @param token
*/
export async function verifyCaptchaToken(token: string) {
if (!secret) {
throw new Error('CAPTCHA_SECRET_TOKEN is not set');
}
const res = await fetch(verifyEndpoint, {
method: 'POST',
body: `secret=${encodeURIComponent(secret)}&response=${encodeURIComponent(token)}`,
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
});
const data = await res.json();
if (!data.success) {
throw new Error('Invalid CAPTCHA token');
}
}

View File

@@ -8,7 +8,7 @@ import { If } from '@kit/ui/if';
import { Separator } from '@kit/ui/separator';
import { Trans } from '@kit/ui/trans';
import { useCaptchaToken } from './captcha';
import { useCaptchaToken } from '../captcha/client';
import { MagicLinkAuthContainer } from './magic-link-auth-container';
import { OauthProviders } from './oauth-providers';
import { EmailPasswordSignUpContainer } from './password-sign-up-container';

View File

@@ -0,0 +1,41 @@
{
"name": "@kit/next",
"private": true,
"version": "0.1.0",
"scripts": {
"clean": "git clean -xdf .turbo node_modules",
"format": "prettier --check \"**/*.{ts,tsx}\"",
"lint": "eslint .",
"typecheck": "tsc --noEmit"
},
"prettier": "@kit/prettier-config",
"exports": {
"./actions": "./src/actions/index.ts"
},
"peerDependencies": {
"@kit/auth": "workspace:*",
"@kit/supabase": "workspace:*"
},
"devDependencies": {
"@kit/auth": "*",
"@kit/eslint-config": "workspace:*",
"@kit/prettier-config": "workspace:*",
"@kit/supabase": "*",
"@kit/tailwind-config": "workspace:*",
"@kit/tsconfig": "workspace:*"
},
"eslintConfig": {
"root": true,
"extends": [
"@kit/eslint-config/base",
"@kit/eslint-config/react"
]
},
"typesVersions": {
"*": {
"*": [
"src/*"
]
}
}
}

View File

@@ -0,0 +1,67 @@
import { redirect } from 'next/navigation';
import type { User } from '@supabase/supabase-js';
import { z } from 'zod';
import { verifyCaptchaToken } from '@kit/auth/captcha/server';
import { requireUser } from '@kit/supabase/require-user';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
const parseFactory =
<T extends z.ZodTypeAny>(schema: T) =>
(data: unknown): z.infer<T> => {
try {
return schema.parse(data);
} catch (err) {
console.error(err);
// handle error
throw new Error(`Invalid data: ${err}`);
}
};
/**
*
* @name enhanceAction
* @description Enhance an action with captcha, schema and auth checks
*/
export function enhanceAction<
Args,
Schema extends z.ZodType<Omit<Args, 'captchaToken'>, z.ZodTypeDef>,
Response,
>(
fn: (params: z.infer<Schema>, user: User) => Response,
config: {
captcha?: boolean;
schema: Schema;
},
) {
return async (
params: z.infer<Schema> & {
captchaToken?: string;
},
) => {
// verify the user is authenticated if required
const auth = await requireUser(getSupabaseServerActionClient());
// If the user is not authenticated, redirect to the specified URL.
if (!auth.data) {
redirect(auth.redirectTo);
}
// verify the captcha token if required
if (config.captcha) {
const token = z.string().min(1).parse(params.captchaToken);
await verifyCaptchaToken(token);
}
// validate the schema
const parsed = parseFactory(config.schema);
const data = parsed(params);
// pass the data to the action
return fn(data, auth.data);
};
}

View File

@@ -0,0 +1,8 @@
{
"extends": "@kit/tsconfig/base.json",
"compilerOptions": {
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["*.ts", "src"],
"exclude": ["node_modules"]
}