From ca5e90883c4da5f01d4e7e110190aa70dff04864 Mon Sep 17 00:00:00 2001 From: Bereket Engida <86073083+Bekacru@users.noreply.github.com> Date: Mon, 25 Nov 2024 23:44:30 +0300 Subject: [PATCH] feat(cli): add secret generator command for the cli (#659) --- docs/content/docs/concepts/cli.mdx | 11 ++++++++++- docs/content/docs/concepts/typescript.mdx | 22 ++++++++++++++++++++++ packages/cli/src/commands/secert.ts | 12 ++++++++++++ packages/cli/src/index.ts | 2 ++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/commands/secert.ts diff --git a/docs/content/docs/concepts/cli.mdx b/docs/content/docs/concepts/cli.mdx index dca245e975..42434df5c7 100644 --- a/docs/content/docs/concepts/cli.mdx +++ b/docs/content/docs/concepts/cli.mdx @@ -40,4 +40,13 @@ npx @better-auth/cli@latest migrate If you see this error, it means the CLI can’t resolve imported modules in your Better Auth config file. We're working on a fix for many of these issues, but in the meantime, you can try the following: -- Remove any import aliases in your config file and use relative paths instead. After running the CLI, you can revert to using aliases. \ No newline at end of file +- Remove any import aliases in your config file and use relative paths instead. After running the CLI, you can revert to using aliases. + + +## Secret + +The CLI also provides a way to generate a secret key for your Better Auth instance. + +```bash title="Terminal" +npx @better-auth/cli@latest secret +``` \ No newline at end of file diff --git a/docs/content/docs/concepts/typescript.mdx b/docs/content/docs/concepts/typescript.mdx index ffc2e9fee1..ec08dc6a1c 100644 --- a/docs/content/docs/concepts/typescript.mdx +++ b/docs/content/docs/concepts/typescript.mdx @@ -22,6 +22,28 @@ To avoid instantiation and reference errors, configure your TypeScript config fi If you're using a monorepo and want to share the `auth` package across multiple projects, either export the TypeScript files as-is without compilation or use build tools like `tsup` to transpile the package. +### Strict Mode + +Better Auth is designed to work with TypeScript's strict mode. We recommend enabling strict mode in your TypeScript config file: + +```json title="tsconfig.json" +{ + "compilerOptions": { + "strict": true + } +} +``` + +if you can't set `strict` to `true`, you can enable `strictNullChecks`: + +```json title="tsconfig.json" +{ + "compilerOptions": { + "strictNullChecks": true, + } +} +``` + ## Inferring Types Both the client SDK and the server offer types that can be inferred using the `$Infer` property. Plugins can extend base types like `User` and `Session`, and you can use `$Infer` to infer these types. Additionally, plugins can provide extra types that can also be inferred through `$Infer`. diff --git a/packages/cli/src/commands/secert.ts b/packages/cli/src/commands/secert.ts new file mode 100644 index 0000000000..7e18e82205 --- /dev/null +++ b/packages/cli/src/commands/secert.ts @@ -0,0 +1,12 @@ +import { logger } from "better-auth"; +import chalk from "chalk"; +import { Command } from "commander"; +import Crypto from "crypto"; + +export const generateSecret = new Command("secret").action(() => { + const secret = Crypto.randomBytes(32).toString("hex"); + logger.info(`\nAdd the following to your .env file: +${ + chalk.gray("# Auth Secret") + chalk.green(`\nBETTER_AUTH_SECRET=${secret}`) +}`); +}); diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index b8f52cb173..a0d94acfe4 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -4,12 +4,14 @@ import { Command } from "commander"; import { migrate } from "./commands/migrate"; import { generate } from "./commands/generate"; import "dotenv/config"; +import { generateSecret } from "./commands/secert"; async function main() { const program = new Command("better-auth"); program .addCommand(migrate) .addCommand(generate) + .addCommand(generateSecret) .version("0.0.1") .description("Better Auth CLI"); program.parse();