Skip to main content

React Native and Hybrid Apps

Use this guide when an existing hybrid mobile app needs to embed Loyal Guru loyalty modules.

In React Native, the recommended integration is WebView-based. The app loads a small HTML host inside react-native-webview; that HTML host loads the Web SDK from the CDN and initializes the Loyal Guru module.

Architecture​

This approach reuses the same Web SDK across browser and hybrid app integrations, while the host app keeps control of native navigation, login, token refresh, analytics, and error handling.

Requirements​

RequirementDescription
WebView dependencyInstall and configure react-native-webview.
Module originLoyal Guru module URL for the Company and environment.
Customer JWTToken issued by the host backend.
Token refresh endpointHost backend endpoint that can issue a fresh JWT.
Message handlingHost app must process WebView messages from the embedded module.

Example​

import React, { useMemo, useRef } from 'react';
import { WebView } from 'react-native-webview';

export function LoyaltyWebViewScreen() {
const webViewRef = useRef<WebView>(null);
const iframeOrigin = 'https://your-loyalty-module.loyal.guru';
const token = 'CUSTOMER_JWT';
const locale = 'en';
const module = 'rewards';

const html = useMemo(() => `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="loyalty-container"></div>

<script src="https://cdn.loyal.guru/sdk/v1.1.3/embed-sdk.umd.js"></script>
<script>
window.EmbedLoyaltyApp.init({
containerId: 'loyalty-container',
iframeOrigin: '${iframeOrigin}',
module: '${module}',
token: '${token}',
locale: '${locale}',
autoResize: true,
onLoad: function () {
window.ReactNativeWebView.postMessage(JSON.stringify({
type: 'SDK_LOADED'
}));
},
onSize: function (size) {
window.ReactNativeWebView.postMessage(JSON.stringify({
type: 'SDK_SIZE',
payload: size
}));
},
onServerError: function (error) {
window.ReactNativeWebView.postMessage(JSON.stringify({
type: 'SERVER_ERROR',
payload: error
}));
},
onData: function (data) {
window.ReactNativeWebView.postMessage(JSON.stringify({
type: 'SDK_DATA',
payload: data
}));
}
});

window.EmbedLoyaltyApp.onTokenRefreshRequested(function () {
window.ReactNativeWebView.postMessage(JSON.stringify({
type: 'REQUEST_NEW_TOKEN'
}));
});
</script>
</body>
</html>
`, [iframeOrigin, token, locale, module]);

return (
<WebView
ref={webViewRef}
originWhitelist={['*']}
source={{ html }}
javaScriptEnabled
domStorageEnabled
startInLoadingState
onMessage={async (event) => {
const message = JSON.parse(event.nativeEvent.data);

if (message.type === 'REQUEST_NEW_TOKEN') {
const refreshedToken = await fetchFreshTokenFromYourBackend();
webViewRef.current?.injectJavaScript(`
window.EmbedLoyaltyApp.refreshToken('${refreshedToken}');
true;
`);
}

if (message.type === 'SERVER_ERROR') {
console.warn('Loyalty server error', message.payload);
}
}}
/>
);
}

Token Refresh​

When the embedded module requests a fresh token:

  1. The SDK emits REQUEST_NEW_TOKEN.
  2. The HTML host forwards it through window.ReactNativeWebView.postMessage.
  3. The React Native app calls its own backend to get a fresh Customer JWT.
  4. The app injects EmbedLoyaltyApp.refreshToken(newToken) into the WebView.
  5. The Loyal Guru module resumes without interrupting the Customer session.

Do not reuse expired tokens. Issue short-lived tokens and refresh them on demand.

Native App Responsibilities​

ResponsibilityDetail
NavigationDecide where the loyalty WebView appears and how the Customer enters/leaves it.
LoginAuthenticate the Customer before opening the loyalty module, unless using LG-managed authentication.
Token refreshProvide a backend refresh endpoint and handle REQUEST_NEW_TOKEN.
Error handlingSurface SERVER_ERROR and SDK errors using the host app's UX patterns.
External linksOpen external URLs outside the WebView where required.
AnalyticsForward SDK events to the host analytics layer if needed.

Production Notes​

AreaRecommendation
Origin validationRestrict allowed origins in production.
JWT storageDo not hardcode production JWTs. Store and refresh tokens according to the host app's security standards.
WebView permissionsEnable only what the embedded module needs.
LayoutAvoid fixed parent heights that clip auto-resized content.
VersioningThe host app release cadence remains under the retailer's control; Loyal Guru can update embedded module content without a native release when no native-layer change is required.