> For the complete documentation index, see [llms.txt](https://zakariaes-organization.gitbook.io/saascore-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zakariaes-organization.gitbook.io/saascore-docs/authentication-flow/for-clients/oauth.md).

# 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:

   ```jsx
   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`:

   ```jsx
   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.
* ```jsx
  if (!existingPreClientPlan && !userDb && !isAdmin) {
      return false;
  }
  ```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://zakariaes-organization.gitbook.io/saascore-docs/authentication-flow/for-clients/oauth.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
