Overview
ThegetUserSubscription function retrieves the currently authenticated user’s subscription information along with their user record. It returns both the subscription data (if one exists) and the user details.
Function Signature
Parameters
This function does not accept any parameters. It automatically retrieves the authenticated user from the session.Return Value
Indicates whether the operation was successful
An object containing both user and subscription data:
The user’s database record:
The Supabase authentication user ID
The Dodo Payments customer ID
The ID of the current active subscription
ISO timestamp when the user was created
ISO timestamp when the user was last updated
ISO timestamp when the user was deleted (soft delete)
The user’s current subscription, or null if no subscription exists:And other subscription fields…
Unique subscription identifier
Reference to the user’s Supabase ID
The subscribed product ID
Subscription status (e.g., “active”, “cancelled”, “past_due”)
Recurring amount before tax
Currency code (e.g., “USD”, “EUR”)
Number of units subscribed
ISO timestamp of the next billing date
ISO timestamp of the previous billing date
Whether the subscription will cancel at the next billing date
ISO timestamp when subscription was cancelled
Error message if the operation failed:
- “User not found” - No authenticated session
- “User details not found” - User exists in Supabase but not in database
Implementation Details
The function:- Retrieves the authenticated Supabase user via
getUser() - Queries the database for the user’s record using their Supabase ID
- If no subscription ID exists, returns user with
subscription: null - If subscription ID exists, queries for the subscription details
- Returns both user and subscription data together
Error Handling
- Returns
{ success: false, error: "User not found" }if authentication fails - Returns
{ success: false, error: "User details not found" }if user record doesn’t exist in database - Returns subscription as
nullif user has no active subscription (not an error)
Usage Example
Checking Subscription Status
Client Component Example
Related Actions
getUser- Retrieves the authenticated usergetInvoices- Retrieves user’s payment historychangePlan- Changes the subscription plancancelSubscription- Cancels the subscriptionrestoreSubscription- Restores a cancelled subscription
Source Code
Location:actions/get-user-subscription.ts:19