OAuth

Scenario 1: Sign up/Sign in with Free Account

This scenario applies if your web app offers a free plan or requires only account registration without any payment.

Responsible File: auth.ts

Callback Function: signIn()

Conditions:

  • A "Free Plan" must exist among your available plans.

Process:

  1. A free account is setup by calling the createUser event in the auth.ts file.

  2. This triggers the crucial setupUserAccount() function, which in this scenario executes:

    await prisma.user.update({
        where: { id: userId },
        data: {
            roles: {
                set: ["CLIENT"],
            },
        },
    });
    
    handleAfterSignupTasks({
        email,
        name,
        userId,
        isAffiliate,
    });

Scenario 2: Sign up as a New Client Who Has Just Subscribed to a Plan

This is the most common scenario in SaaS applications.

Process:

  1. When a new visitor subscribes to a plan for the first time, a PreClientPlan table is created at the Stripe webhook level to temporarily store plan information with a token.

  2. A link containing the token is sent to the new client to create their account.

    Link Format: example.com/auth/sign-up?token=token_code

  3. In the auth.ts file, the createUser event is triggered, invoking the setupUserAccount() function.

  4. The function searches for the PreClientPlan record using the token to retrieve data for creating the actual ClientPlan:

    await prisma.clientPlan.create({
        data: {
            userId,
            subscriptionPlan: existingPreClientPlan.plan,
            status: "ACTIVE",
            subscriptionType: existingPreClientPlan.subscriptionType,
            // ...other fields
        },
    });
  5. The handleAfterSignupTasks() function is then called to complete the process.

Additional Details:

  • No Free Plan Available: If a free plan is not available, sign-up/sign-in is restricted except in the following cases:

    1. Authenticating with an admin email (specified in the config file).

    2. Authenticating as a client who has previously signed up.

    3. Authenticating as a client who has already paid for their subscription plan.

  • if (!existingPreClientPlan && !userDb && !isAdmin) {
        return false;
    }

Last updated