Overview
ThegetUser function retrieves the currently authenticated user from Supabase Auth. This is a fundamental function used throughout the application to verify authentication and access user data.
Function Signature
Parameters
This function does not accept any parameters. It automatically retrieves the user from the current Supabase session.Return Value
Indicates whether the operation was successful
Error message if the operation failed. Possible values:
- “User not found” - No authenticated session exists
- “Failed to get user” - Generic error during retrieval
Implementation Details
The function:- Creates a Supabase server client
- Calls
supabase.auth.getUser()to retrieve the authenticated user - Returns the user data if found
- Catches and handles any errors that occur
Error Handling
- Returns
{ success: false, error: "User not found" }if no user is authenticated - Returns
{ success: false, error: "Failed to get user" }if an exception occurs - All errors are caught and returned as structured responses
Usage Example
Middleware Protection Example
Client Component Example
Related Actions
createUser- Creates a user record in the databasegetUserSubscription- Gets user subscription detailsdeleteAccount- Deletes the user account
Source Code
Location:actions/get-user.ts:7