Skip to main content

Example Implementation

This guide explains how to integrate an external user system with Replyke. For demonstration purposes, we’ll use Clerk as the authentication provider. However, the logic can be adapted for other systems, with slight variations based on their specific requirements.

Client-Side Integration

Overview

In this example, we check for a logged-in user from the external authentication system (Clerk). If a user is logged in, a request is made to the backend to retrieve a signed token, which is then consumed by the Replyke AuthProvider.

Implementation

Here’s the React client-side implementation:

Key Points:

  1. Token Management: The Clerk session token is retrieved using getToken.
  2. API Call: A GET request is sent to the server with the token included in the Authorization header. For other systems, ensure their tokens are passed as required.
  3. State Management: The signed token received from the server is stored in the component state and consumed by the Replyke AuthProvider.

Server-Side Integration

On the server, we:
  1. Use Clerk middleware to handle authentication.
  2. Implement an endpoint to issue a signed token for Replyke.

Middleware Setup

To enable Clerk’s middleware:
This ensures requests are authenticated before reaching protected routes.

Endpoint Definition

We protect the route using Clerk’s requireAuth middleware and delegate the logic to a controller:

Controller Logic

The controller generates a signed JWT token:

Key Points:

  1. Middleware: Clerk’s middleware attaches the user’s ID to the request object.
  2. Private Key: The private key is converted from a base64 string to a PEM format.
  3. JWT Payload:
    • sub: User ID.
    • iss: Project ID.
    • aud: Always set to replyke.com.
    • userData: Optional user details. In this example we pass the email only, but we can pass all properties that are avaialable on the User object (e.g. name, username, location, metadata etc.)
  4. Token Signing: The JWT is signed using RS256 with a short expiration time (5 minutes).

Customizing for Other Systems

To adapt this integration for other authentication systems:
  1. Replace Clerk-specific logic (e.g., getToken, requireAuth, and user details extraction) with equivalent methods from your chosen system.
  2. Ensure your authentication middleware populates the request with user data.
  3. Follow Replyke’s JWT payload structure for compatibility.

Conclusion

This example demonstrates a secure and modular approach to integrating an external user system with Replyke. While we used Clerk for demonstration, the same principles apply to other systems with minor adjustments. This flexibility ensures seamless integration with your existing authentication setup.