Skip to content

feat(CLI): implement better auth detection#189

Open
anirudhprmar wants to merge 1 commit into
getpaykit:mainfrom
anirudhprmar:feat(CLI)/better-auth-detection
Open

feat(CLI): implement better auth detection#189
anirudhprmar wants to merge 1 commit into
getpaykit:mainfrom
anirudhprmar:feat(CLI)/better-auth-detection

Conversation

@anirudhprmar
Copy link
Copy Markdown
Contributor

@anirudhprmar anirudhprmar commented May 23, 2026

Issue : #114

Summary

  • New Feature
    • added better auth detection for CLI which helps with identify block and imports auth from detected existing config file location for retrieving session information.

Summary by CodeRabbit

  • New Features
    • CLI initialization now automatically detects and integrates Better Auth identity configuration when present in your project, simplifying authentication setup.

Review Change Stack

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 23, 2026

@anirudhprmar is attempting to deploy a commit to the maxktz Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 23, 2026

📝 Walkthrough

Walkthrough

The PayKit CLI initialization command now detects the presence of Better Auth in the target project and conditionally generates an identity-resolving handler in the PayKit config file. A new detection utility identifies Better Auth; a shared helper function centralizes identity block generation; and the initialization flow integrates both to emit config with optional Better Auth session integration.

Changes

Better Auth Identity Integration

Layer / File(s) Summary
Better Auth detection utility
packages/paykit/src/cli/utils/detect.ts
New detectBetterAuth(cwd) function reads the target project's package.json and returns true if better-auth is present in dependencies or devDependencies.
Identity block generation and config templates
packages/paykit/src/cli/commands/init.ts
New buildIdentifyBlock helper centralizes creation of the identify function block (either Better Auth–backed using auth.api.getSession or a stub returning null). Both generateConfigFile and generateConfigFileFromProductsModule are updated to accept useBetterAuthIdentify and use the shared helper, conditionally including the auth import when available.
Init action Better Auth wiring
packages/paykit/src/cli/commands/init.ts
In initAction, Better Auth presence is detected and the config file location is resolved, then the resolved import path and usage flag are passed to config generators so the emitted PayKit config reflects Better Auth integration.

Sequence Diagram

sequenceDiagram
  participant initAction as initAction
  participant detectBA as detectBetterAuth
  participant locateCfg as locate Better Auth config
  participant genCfg as generateConfigFile
  participant writeFile as write config file
  
  initAction->>detectBA: check for better-auth
  detectBA-->>initAction: usesBetterAuth (boolean)
  initAction->>locateCfg: find Better Auth config path
  locateCfg-->>initAction: auth import path or undefined
  initAction->>genCfg: pass usesBetterAuth & auth path
  genCfg-->>initAction: PayKit config string
  initAction->>writeFile: write config with conditional auth integration
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 A rabbit hops through Better Auth fields,
Weaving identity into PayKit's yields—
Conditional blocks and detection so keen,
Config generation unseen, yet serene!
Init flows forward with purpose clear,
Better Auth wiring appears here. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: implementing Better Auth detection in the CLI. It is concise, specific, and directly related to the primary objective of the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/paykit/src/cli/commands/init.ts`:
- Around line 566-568: The ternary expression using usesBetterAuth &&
existingBetterAuthConfig ? p.log.step(`Detected Better Auth:
${picocolors.bold(existingBetterAuthConfig)}`) : null is being used for a
side-effect and triggers the no-unused-expressions ESLint rule; replace it with
a straightforward if statement: check if usesBetterAuth &&
existingBetterAuthConfig then call p.log.step(...) so the side-effect is
explicit (refer to usesBetterAuth, existingBetterAuthConfig and p.log.step in
init.ts).
- Line 211: The generated import line includes a stray semicolon inside the
import path string (the template expression using includeIdentify and
useBetterAuthIdentify produces `import { auth } from
"${useBetterAuthIdentify};"`), causing a syntax error; update the template so
the semicolon is placed after the closing quote (or removed from inside the
string) so it emits `import { auth } from "${useBetterAuthIdentify}";` (or
without a semicolon if your style prefers), adjusting the template expression
that builds the import line where includeIdentify and useBetterAuthIdentify are
used.
- Line 151: The import template places the semicolon inside the string,
producing an invalid path; update the conditional template that builds the
import line (using the includeIdentify and useBetterAuthIdentify variables) so
the semicolon is outside the quoted path — e.g., change the generated fragment
from `import { auth } from "${useBetterAuthIdentify};"` to `import { auth } from
"${useBetterAuthIdentify}";` so the module specifier is correct and the import
statement ends with a semicolon.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9029ddb9-9e98-4bd5-9255-48e0081b74f2

📥 Commits

Reviewing files that changed from the base of the PR and between 98acaa3 and 633d75d.

📒 Files selected for processing (2)
  • packages/paykit/src/cli/commands/init.ts
  • packages/paykit/src/cli/utils/detect.ts


return `${providerImport(provider)}
import { createPayKit } from "paykitjs";${importLine}
${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify};"` : ""}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Syntax error: semicolon inside the import path string.

The semicolon is placed inside the quotes, generating an invalid import like import { auth } from "path/to/auth;" instead of import { auth } from "path/to/auth";. This will cause a module resolution failure.

🐛 Proposed fix
-${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify};"` : ""}
+${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify}";` : ""}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify};"` : ""}
${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify}";` : ""}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/paykit/src/cli/commands/init.ts` at line 151, The import template
places the semicolon inside the string, producing an invalid path; update the
conditional template that builds the import line (using the includeIdentify and
useBetterAuthIdentify variables) so the semicolon is outside the quoted path —
e.g., change the generated fragment from `import { auth } from
"${useBetterAuthIdentify};"` to `import { auth } from
"${useBetterAuthIdentify}";` so the module specifier is correct and the import
statement ends with a semicolon.


return `${providerImport(provider)}
import { createPayKit } from "paykitjs";${importLine}
${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify};"` : ""}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Same syntax error: semicolon inside the import path string.

Same issue as line 151 — the semicolon is inside the quotes.

🐛 Proposed fix
-${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify};"` : ""}
+${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify}";` : ""}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify};"` : ""}
${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify}";` : ""}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/paykit/src/cli/commands/init.ts` at line 211, The generated import
line includes a stray semicolon inside the import path string (the template
expression using includeIdentify and useBetterAuthIdentify produces `import {
auth } from "${useBetterAuthIdentify};"`), causing a syntax error; update the
template so the semicolon is placed after the closing quote (or removed from
inside the string) so it emits `import { auth } from
"${useBetterAuthIdentify}";` (or without a semicolon if your style prefers),
adjusting the template expression that builds the import line where
includeIdentify and useBetterAuthIdentify are used.

Comment on lines +566 to +568
usesBetterAuth && existingBetterAuthConfig
? p.log.step(`Detected Better Auth: ${picocolors.bold(existingBetterAuthConfig)}`)
: null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Use if statement instead of ternary for side effects.

ESLint reports no-unused-expressions. The ternary result is discarded — use an if statement for clarity and to satisfy the linter.

🔧 Proposed fix
-  usesBetterAuth && existingBetterAuthConfig
-    ? p.log.step(`Detected Better Auth: ${picocolors.bold(existingBetterAuthConfig)}`)
-    : null;
+  if (usesBetterAuth && existingBetterAuthConfig) {
+    p.log.step(`Detected Better Auth: ${picocolors.bold(existingBetterAuthConfig)}`);
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
usesBetterAuth && existingBetterAuthConfig
? p.log.step(`Detected Better Auth: ${picocolors.bold(existingBetterAuthConfig)}`)
: null;
if (usesBetterAuth && existingBetterAuthConfig) {
p.log.step(`Detected Better Auth: ${picocolors.bold(existingBetterAuthConfig)}`);
}
🧰 Tools
🪛 GitHub Check: lint

[warning] 566-568: eslint(no-unused-expressions)
Expected expression to be used

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/paykit/src/cli/commands/init.ts` around lines 566 - 568, The ternary
expression using usesBetterAuth && existingBetterAuthConfig ?
p.log.step(`Detected Better Auth: ${picocolors.bold(existingBetterAuthConfig)}`)
: null is being used for a side-effect and triggers the no-unused-expressions
ESLint rule; replace it with a straightforward if statement: check if
usesBetterAuth && existingBetterAuthConfig then call p.log.step(...) so the
side-effect is explicit (refer to usesBetterAuth, existingBetterAuthConfig and
p.log.step in init.ts).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant