Backend Integration

Getting started is easy. The first step is installing the Frontegg SDK on your backend application.

npm i @frontegg/client
pip install frontegg
composer install frontegg/php-sdk

And copy the following code to your application's backend main entry point:

import { frontegg, FronteggPermissions } from '@frontegg/client'
...
...
...
app.use('/frontegg', frontegg({
  clientId: 'YOUR-CLIENT-ID',
  apiKey: 'YOUR_API_KEY',
  contextResolver: async (req) => {
    const userId = 'the-logged-in-user-id';
    const tenantId = 'the-logged-in-tenant-id';
    return {
      userId,
      tenantId,
      permissions: FronteggPermissions.All,
    }
  }
}))
from flask import Flask
from frontegg import FronteggContext
from frontegg.flask import Frontegg

app = Flask('example')
app.config['FRONTEGG_CLIENT_ID'] = 'your client id'
app.config['FRONTEGG_API_KEY'] = 'your api key'
app.config['FRONTEGG_CONTEXT_RESOLVER'] = lambda request: FronteggContext('the-logged-in-user-id', 'the-logged-in-tenant-id')

frontegg = Frontegg(app)
<?php
require_once './vendor/autoload.php';
require_once './src/Frontegg/autoload.php';
...

$clientId = 'YOUR_CLIENT_ID';
$apikey = 'YOUR_API_KEY';
$config = [
    'clientId' => $clientId,
    'clientSecret' => $apikey,
    'apiBaseUrl' => 'https://api.frontegg.com/',
    'contextResolver' => function(RequestInterface $request) {
        return [
            'tenantId' => 'MY-TENANT-ID', // Should be extracted from the request context
            'userId' => 'MY-USER-ID',     // Should be extracted from the request context
            'permissions' => [],
        ];
    },
    'disableCors' => false,
];
$frontegg = new Frontegg($config);
    
/**
 * Setup routing rule for "/frontegg" URIs.
 * Can be a part of middleware f.e. in Laravel.
 */
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/frontegg') === 0) {
    $response = handleFronteggUri($request);
}
...
function handleFronteggUri(RequestInterface $request)
{
    $response = $frontegg->forward($request);
    return $response->getBody(); 
}

πŸ“˜

The Frontegg middleware shown above is provided by Frontegg and is a mandatory install. This code allows your product to maintain its current security and authentication methods, while integrating with Frontegg, without compromising on the identity of your tenants and users.


Did this page help you?