Skip to content

Conversation

mogita
Copy link
Contributor

@mogita mogita commented Aug 7, 2025

Closes #821

Changes:

  • Add CAP_ALLOWED_SIGNUP_DOMAINS environment variable for domain-restricted signup.
  • Implement signup restriction in NextAuth signIn callback with existing user bypass (existing users can always signin)

Test build: ghcr.io/mogita/cap-web:restricted-signup.1

Summary by CodeRabbit

  • New Features
    • Added support for restricting new user signups to specific email domains, configurable via an environment variable.
  • Bug Fixes
    • Improved validation of email domains during signup to ensure only allowed domains can register.
  • Chores
    • Updated example environment configuration and dependencies to support domain restriction functionality.

Copy link
Contributor

coderabbitai bot commented Aug 7, 2025

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

A new environment variable, CAP_ALLOWED_SIGNUP_DOMAINS, was introduced to restrict user signups to specific email domains. Supporting logic was added to the authentication flow to enforce this restriction for new users, using a new utility function for domain validation. Documentation and environment schema were updated accordingly, and a dependency on zod was added.

Changes

Cohort / File(s) Change Summary
Environment Variable Declaration & Documentation
.env.example, packages/env/server.ts
Added CAP_ALLOWED_SIGNUP_DOMAINS as an optional environment variable, with schema validation and documentation.
Authentication Logic Update
packages/database/auth/auth-options.tsx
Added a sign-in callback to restrict new user signups to allowed email domains, using the new utility function.
Domain Validation Utility
packages/database/auth/domain-utils.ts
Introduced isEmailAllowedForSignup to validate email domains for signup, using zod for parsing and checks.
Dependency Update
packages/database/package.json
Added zod as a new runtime dependency.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AuthSystem
    participant Database
    participant Env

    User->>AuthSystem: Attempt sign-in
    AuthSystem->>Database: Check if user exists
    Database-->>AuthSystem: User found? (yes/no)
    alt User is new
        AuthSystem->>Env: Get CAP_ALLOWED_SIGNUP_DOMAINS
        AuthSystem->>AuthSystem: isEmailAllowedForSignup(user.email, allowedDomains)
        alt Domain allowed
            AuthSystem-->>User: Allow sign-in
        else Domain not allowed
            AuthSystem-->>User: Block sign-in
        end
    else User exists
        AuthSystem-->>User: Allow sign-in
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

In fields of code, a fence now stands,
Only certain emails pass these lands.
"Allowed domains," the env file reads,
While zod ensures the signup proceeds.
Bunnies hop with glee, secure and bright—
Only friendly emails join the site!
🐇✨

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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: 1

🧹 Nitpick comments (1)
packages/database/auth/auth-options.tsx (1)

173-192: Well-implemented domain restriction logic.

The signIn callback correctly:

  • Exempts existing users from domain restrictions
  • Only applies checks to new signups
  • Uses proper database queries and utility functions
  • Includes appropriate logging

Consider enhancing the logging to include the rejected domain for better debugging:

-              console.log(`Signup blocked for email domain: ${user.email}`);
+              console.log(`Signup blocked for email domain: ${user.email?.split('@')[1]} (email: ${user.email})`);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 00a7a0b and 1390778.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (5)
  • .env.example (1 hunks)
  • packages/database/auth/auth-options.tsx (2 hunks)
  • packages/database/auth/domain-utils.ts (1 hunks)
  • packages/database/package.json (1 hunks)
  • packages/env/server.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build Desktop (aarch64-apple-darwin, macos-latest)
  • GitHub Check: Clippy
  • GitHub Check: Build Desktop (x86_64-pc-windows-msvc, windows-latest)
🔇 Additional comments (6)
packages/env/server.ts (1)

48-48: LGTM - Environment variable correctly configured.

The addition of CAP_ALLOWED_SIGNUP_DOMAINS as an optional string follows the existing pattern and is appropriately positioned in the schema.

.env.example (1)

68-71: Excellent documentation for the new environment variable.

Clear explanation with a practical example. Commenting out by default is the right approach for optional functionality.

packages/database/auth/domain-utils.ts (4)

3-19: Solid main function with proper default behavior.

The function correctly handles the case where no domain restrictions are configured (returns true) and uses appropriate helper functions for validation.


21-30: Good email domain extraction logic.

Using lastIndexOf("@") is correct for handling edge cases in email addresses, and validating with zod before extraction ensures robustness.


32-37: Well-implemented domain parsing with normalization.

The function properly splits, trims, normalizes case, and filters invalid domains. The approach is thorough and handles malformed input gracefully.


39-41: Appropriate domain validation using zod.

Using z.hostname() for domain validation is the right approach and provides reliable validation.

@Brendonovich
Copy link
Member

We'll probably expose this in a UI at some point but this works for now!

@Brendonovich Brendonovich merged commit b265e53 into CapSoftware:main Aug 13, 2025
11 checks passed
@mogita mogita deleted the restricted-signup branch August 13, 2025 03:28
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.

Improvement on registration control
2 participants