Refactor account server actions using the enhanced action helper

The enhanced action helper has been utilized to refactor account-related server actions across the codebase. This change aims to streamline the server-side handling of user accounts, team accounts, and related functionality. As a result, various account-related server actions have now been wrapped with the helper, providing uniformity and consistency in action handling.
This commit is contained in:
giancarlo
2024-04-27 18:31:11 +07:00
parent ec59d02fb0
commit 0616d3b288
14 changed files with 388 additions and 409 deletions

View File

@@ -4,7 +4,7 @@ import { redirect } from 'next/navigation';
import type { User } from '@supabase/supabase-js';
import { z } from 'zod';
import { ZodType, z } from 'zod';
import { verifyCaptchaToken } from '@kit/auth/captcha/server';
import { requireUser } from '@kit/supabase/require-user';
@@ -12,6 +12,12 @@ import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-clie
import { captureException, zodParseFactory } from '../utils';
/**
* @name IS_CAPTCHA_SETUP
* @description Check if the CAPTCHA is setup
*/
const IS_CAPTCHA_SETUP = !!process.env.CAPTCHA_SECRET_TOKEN;
/**
*
* @name enhanceAction
@@ -24,19 +30,21 @@ export function enhanceAction<
auth?: boolean;
captcha?: boolean;
captureException?: boolean;
schema: z.ZodType<
schema?: z.ZodType<
Config['captcha'] extends true ? Args & { captchaToken: string } : Args,
z.ZodTypeDef
>;
},
>(
fn: (
params: z.infer<Config['schema']>,
params: Config['schema'] extends ZodType ? z.infer<Config['schema']> : Args,
user: Config['auth'] extends false ? undefined : User,
) => Response | Promise<Response>,
config: Config,
) {
return async (params: z.infer<Config['schema']>) => {
return async (
params: Config['schema'] extends ZodType ? z.infer<Config['schema']> : Args,
) => {
type UserParam = Config['auth'] extends false ? undefined : User;
const requireAuth = config.auth ?? true;
@@ -56,11 +64,15 @@ export function enhanceAction<
}
// validate the schema
const parsed = zodParseFactory(config.schema);
const data = parsed(params);
const data = config.schema
? zodParseFactory(config.schema)(params)
: params;
// verify captcha unless it's explicitly disabled
// verify the captcha token if required
if (config.captcha) {
const verifyCaptcha = config.captcha ?? IS_CAPTCHA_SETUP;
if (verifyCaptcha) {
const token = (data as Args & { captchaToken: string }).captchaToken;
// Verify the CAPTCHA token. It will throw an error if the token is invalid.

View File

@@ -19,6 +19,12 @@ interface HandlerParams<Body> {
body: Body;
}
/**
* @name IS_CAPTCHA_SETUP
* @description Check if the CAPTCHA is setup
*/
const IS_CAPTCHA_SETUP = !!process.env.CAPTCHA_SECRET_TOKEN;
/**
* Enhanced route handler function.
*
@@ -63,8 +69,10 @@ export const enhanceRouteHandler = <
* This function takes a request object as an argument and returns a response object.
*/
return async function routeHandler(request: NextRequest) {
// Verify the captcha token if required
if (params?.captcha) {
const shouldVerifyCaptcha = params?.captcha ?? IS_CAPTCHA_SETUP;
// Verify the captcha token if required and setup
if (shouldVerifyCaptcha) {
const token = captchaTokenGetter(request);
// If the captcha token is not provided, return a 400 response.