* Add OTP and security guidelines documentation and additional checks on client-provided values - Introduced additional checks on client-provided values such as cookies - Introduced a new OTP API documentation outlining the creation and verification of OTP tokens for sensitive operations. - Added comprehensive security guidelines for writing secure code in Next.js, covering client and server components, environment variables, authentication, and error handling. These additions enhance the project's security posture and provide clear instructions for developers on implementing secure practices. * Add OTP API documentation and enhance security guidelines - Introduced comprehensive documentation for the OTP API, detailing the creation and verification of OTP tokens for sensitive operations. - Enhanced security guidelines for Next.js, emphasizing the importance of input validation, environment variable management, and error handling. - Implemented additional checks for client-provided values to improve overall security posture. These updates provide clear instructions for developers and strengthen the project's security framework.
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { cookies } from 'next/headers';
|
|
|
|
import { z } from 'zod';
|
|
|
|
/**
|
|
* @name Theme
|
|
* @description The theme mode enum.
|
|
*/
|
|
const Theme = z.enum(['light', 'dark', 'system'], {
|
|
description: 'The theme mode',
|
|
});
|
|
|
|
/**
|
|
* @name appDefaultThemeMode
|
|
* @description The default theme mode set by the application.
|
|
*/
|
|
const appDefaultThemeMode = Theme.safeParse(
|
|
process.env.NEXT_PUBLIC_DEFAULT_THEME_MODE,
|
|
);
|
|
|
|
/**
|
|
* @name fallbackThemeMode
|
|
* @description The fallback theme mode if none of the other options are available.
|
|
*/
|
|
const fallbackThemeMode = `light`;
|
|
|
|
/**
|
|
* @name getRootTheme
|
|
* @description Get the root theme from the cookies or default theme.
|
|
* @returns The root theme.
|
|
*/
|
|
export async function getRootTheme() {
|
|
const cookiesStore = await cookies();
|
|
const themeCookieValue = cookiesStore.get('theme')?.value;
|
|
const theme = Theme.safeParse(themeCookieValue);
|
|
|
|
// pass the theme from the cookie if it exists
|
|
if (theme.success) {
|
|
return theme.data;
|
|
}
|
|
|
|
// pass the default theme from the environment variable if it exists
|
|
if (appDefaultThemeMode.success) {
|
|
return appDefaultThemeMode.data;
|
|
}
|
|
|
|
// in all other cases, fallback to the default theme
|
|
return fallbackThemeMode;
|
|
}
|