Overview
ThecreateUser function creates a new user record in the database after Supabase authentication. It retrieves the authenticated user, checks if they already exist in the database, creates a Dodo Payments customer, and then inserts a new user record.
Function Signature
Parameters
This function does not accept any parameters. It automatically retrieves the authenticated user from the Supabase session.Return Value
Indicates whether the operation was successful
Success message: either “User created” or “User already exists”
Error message if the operation failed. Possible values:
- “User not found” - No authenticated user session
- “Failed to create customer” - Dodo Payments customer creation failed
Implementation Details
The function performs the following steps:- Retrieves the authenticated user via
getUser() - Checks if a user record already exists with the Supabase user ID
- If the user exists, returns success without creating a duplicate
- Creates a Dodo Payments customer using the user’s email and name
- Inserts a new user record with:
supabaseUserId: The Supabase authentication user IDdodoCustomerId: The Dodo Payments customer IDcurrentSubscriptionId: Empty string (no subscription yet)createdAt: Current timestampupdatedAt: Current timestamp
Error Handling
The function returns structured error responses:- Returns
{ success: false, error: "User not found" }if authentication fails - Returns
{ success: false, error: "Failed to create customer" }if Dodo customer creation fails - Returns
{ success: true, data: "User already exists" }if user record already exists (idempotent)
Usage Example
Client Component Example
Related Actions
getUser- Retrieves the authenticated Supabase usercreateDodoCustomer- Creates a customer in Dodo Payments
Source Code
Location:actions/create-user.ts:10