React Native

Calljmp is a backend-as-a-service (BaaS) designed specifically for mobile developers. The Calljmp React Native SDK allows seamless integration with backend services, including:

  • User authentication using email/password.
  • Full SQLite database access for executing raw SQL queries.
  • Dynamic user permissions for managing roles and access control.
  • Security features such as App Attestation (iOS) and Play Integrity (Android).

With Calljmp, mobile app developers can focus on building features while offloading backend operations. To use the Calljmp React Native SDK in your project, follow these installation steps:

Installation

Run the following command to install the Calljmp SDK:

npm install @calljmp/react-native

Link the SDK

If you're using a version of React Native lower than 0.60, you'll need to link the SDK manually. For newer versions (0.60 and above), auto-linking should work out of the box.

react-native link @calljmp/react-native

Install dependencies

For iOS, navigate to the ios directory of your React Native project and run:

cd ios && pod install && cd ..

Initialize Calljmp

To begin using the Calljmp SDK, you need to initialize it.

import { Calljmp, UserAuthenticationPolicy } from '@calljmp/react-native'

// Initialize the Calljmp instance
const calljmp = new Calljmp({
  android: {
    cloudProjectNumber: GOOGLE_CLOUD_PROJECT_NUMBER, // replace with your project number
  },
  // only if developing a service locally (requires development.enabled)
  service: {
    baseUrl: 'http://192.168.0.0:8787',
  },
  // only needed for development on simulator
  development: {
    enabled: process.env.NODE_ENV === 'development',
    apiToken: CALLJMP_DEF_API_TOKEN, // replace with your Calljmp dev API token
  },
})

User authentication

Calljmp simplifies user authentication. You can authenticate users using their email and password, either by signing in or creating a new user if one doesn't exist.

  • Name
    email
    Type
    string
    Description

    The email address of the user.

  • Name
    password
    Type
    string
    Description

    The password for the user.

  • Name
    policy
    Type
    UserAuthenticationPolicy
    Description

    Authentication policy for the user. - SignInOrCreate: Try to sign in, but create a new account if one doesn't exist. - SignInExistingOnly: Only sign in an existing user. - CreateNewOnly: Only create a new user if one doesn't exist.

  • Name
    tags
    Type
    string[]
    Description

    Tags for the user, which can be used for role-based access control. You can assign custom tags or roles as you see fit.

const auth = await calljmp.users.authWithEmail({
  email: 'test@email.com',
  password: 'password',
  policy: UserAuthenticationPolicy.SignInOrCreate, // Allows both sign-in and sign-up
  tags: ['role:member'], // Tags for user roles, permissions, and more
})

if (auth.error) {
  console.error(auth.error)
  return
}

const user = auth.data.user
console.log(`Authenticated user: ${user}`)

Managing roles and permissions

Calljmp allows dynamic role-based access control by tagging users with specific roles and permissions.

You can assign roles and custom tags directly during user authentication, or update roles via the admin dashboard. Tags allow flexible access control across your database and resources.

const auth = await calljmp.users.authWithEmail({
  email: 'admin@example.com',
  password: 'securepassword',
  policy: UserAuthenticationPolicy.SignInOrCreate,
  tags: [
    // Role-based tags
    'role:admin',
    'role:member',

    // Subscription-based tags
    'plan:premium',

    // Feature access tags
    'feature:analytics',

    // User status tags
    'status:verified',

    // Organization tags
    'org:marketing',

    // Region/locale tags
    'region:us',
  ],
})

Query database

Once authenticated, you can query the SQLite database directly, just like you would in a native application using SQLite.

  • Name
    sql
    Type
    string
    Description

    The SQLite query to execute.

  • Name
    params
    Type
    (string | number)[]
    Description

    The parameters to bind to the SQL query.

const result = await calljmp.database.query({
  sql: 'SELECT id, email, auth_provider, provider_user_id, tags, created_at FROM users',
  params: [],
})

if (result.error) {
  console.error(result.error)
  return
}

console.log(result.data)

Using SQLite

You can also integrate custom ORMs or wrapper libraries to enhance your database experience. For example, you can use Kysely or Drizzle to create a type-safe database layer on top of the Calljmp SQLite database. You find more options in the Cloudflare D1 community projects.

Accessing service

Calljmp provides a simple way to access your backend services. You can make HTTP requests to your service endpoints using the calljmp.service.

  • Name
    route
    Type
    string
    Description

    The path to the service endpoint.

  • Name
    method
    Type
    string
    Description

    The HTTP method to use (GET, POST, etc.).

  • Name
    body
    Type
    any
    Description

    The request body to send with the request.

  • Name
    headers
    Type
    Record<string, string>
    Description

    Additional headers to include in the request.

  • Name
    params
    Type
    Record<string, string>
    Description

    Query parameters to include in the request.

const result = await calljmp.service
  .request('/')
  .get()
  .json<{ message: string }>()

if (result.error) {
  console.error(result.error)
  return
}

console.log(result.data)

When using calljmp.service.request(), you can specify the HTTP method (GET, POST, etc.) and the request body. The SDK will handle adding the necessary headers and authentication tokens for you.

If you like to use a different HTTP client, you can resolve service url dynamically by calling calljmp.service.url(). This will return the base URL of your service, which you can use with any HTTP client of your choice. If you decide to go this route, you will need to handle authentication and headers manually:

  • Name
    X-Platform
    Type
    string
    Description

    A header that indicates the platform of the request. Currently supported platforms: ios and android.

  • Name
    Authorization
    Type
    string
    Description

    The access token for the authenticated user. This token is used to authorize requests to the backend service. Currently only Bearer token is supported. You can resolve access token by calling calljmp.users.accessToken().

Security

Calljmp provides robust security features to ensure that your app and its data are protected.

App attestation (iOS)

Calljmp utilizes Apple’s App Attestation for verifying the integrity of your iOS app. This ensures that only legitimate apps are communicating with your backend.

For more information, refer to Apple's App Attestation documentation.

Play Integrity (Android)

For Android apps, Google Play Integrity helps ensure that your app is genuine and hasn’t been tampered with.

For more details, check out Google Play Integrity documentation.

These features enhance the security of your app, ensuring that your backend communications are from trusted sources.