Skip to main content

Authentication

Cocobase provides a robust authentication system supporting both administrative API keys and per-user JWT tokens.

Authentication Methods

API Key Authentication

API keys are used for server-side operations and administrative tasks.
  • Header: X-API-Key: your_api_key or Authorization: Bearer your_api_key
  • Use Case: Cloud functions, backend integrations, administrative scripts.

JWT Token Authentication

User tokens are obtained after login and are used for client-side user operations.
  • Header: Authorization: Bearer <jwt_token>
  • Use Case: Frontend applications, mobile apps, user-specific data access.

Sign Up

Create a new user account.

Endpoint

POST /auth-collections/signup

Request Body

Response


Login

Authenticate an existing user.

Endpoint

POST /auth-collections/login

Request Body

Response


Get Current User

Retrieve the profile of the currently authenticated user.

Endpoint

GET /auth-collections/user

Headers

Authorization: Bearer <jwt_token>

Update User

Update the current user’s data. Supports atomic array operations.

Endpoint

PATCH /auth-collections/user

Atomic Array Operations

Use $append and $remove to modify array fields (like followers/following) without fetching-and-saving.

Follow a User ($append)

Unfollow a User ($remove)

Update with File Upload

To update a user with files (e.g., avatar), use multipart/form-data.

Change Password

Update the user’s password.

Endpoint

POST /auth-collections/change-password

Request Body


List Users

List all users in the project. Requires API Key.

Endpoint

GET /auth-collections/users

Query Parameters

  • limit: Number of users to return
  • offset: Skip results for pagination
  • role: Filter by role
  • email_contains: Search by email content

Get User by ID

Retrieve a specific user’s public profile.

Endpoint

GET /auth-collections/users/{id}

OAuth Authentication

Cocobase supports Google and Apple OAuth.

1. Get OAuth Google URL

GET /auth-collections/oauth/google?redirect_uri=https://yourapp.com/callback

2. Verify Google Login

POST /auth-collections/verify-google-login

User Relationships

Users can have relationships like followers and following. These are typically stored as arrays of IDs in the user data object.

Examples


Best Practices

  1. Security: Never expose Live API keys (sk_live_...) in client-side code.
  2. Tokens: Refresh JWT tokens before they expire to maintain session persistence.
  3. Atomic Operations: Always use $append and $remove for array updates to avoid race conditions.
  4. Validation: Validate email formats and password strength on the frontend before submitting to the API.

Rate Limits