Skip to main content
The @mentra/react library simplifies building React-based webviews that integrate seamlessly with MentraOS authentication. When users open your webview through the MentraOS Mobile App, they are automatically authenticated without requiring a separate login process.

What Are React Webviews?

React webviews are web applications built with React that run inside the MentraOS Mobile App. They provide rich user interfaces for:
  • Settings and Configuration: Let users customize your app’s behavior
  • Data Visualization: Display charts, graphs, and analytics
  • Content Management: Create, edit, and organize user content
  • Dashboard Interfaces: Show personalized information and controls
The @mentra/react library handles all the authentication complexity, automatically extracting and verifying user tokens from the MentraOS system.

Complete Example

There’s a complete example of a React webview in the MentraOS-React-Example-App repository. Simply follow along with the README to get off the ground quickly.

Installation

Install the React authentication library in your webview project:

Prerequisites

  • React 16.8+: The library uses React Hooks
  • MentraOS App Server: Your backend must be deployed, either on the same domain as your frontend or on a different domain that allows CORS requests from your frontend.
  • Developer Console Setup: Set the webview url in the developer console to your frontend server

Basic Setup

1. Wrap Your App with the Authentication Provider

The MentraAuthProvider component manages authentication state for your entire React application:

2. Access Authentication State

Use the UseMentraAuth hook to access user information and authentication status:

Complete Example Application

Here’s a comprehensive example that demonstrates authentication, API calls, and error handling:

Making Authenticated API Calls

The frontendToken from UseMentraAuth is a JWT token that you should include in the Authorization header when making requests to your App backend:

Authentication Hook API

The UseMentraAuth hook returns an object with the following properties:

How Authentication Works

  1. Token Extraction: When the MentraOS Mobile App opens your webview, it appends an aos_signed_user_token parameter to the URL
  2. Token Verification: The library verifies the token’s signature against the MentraOS Cloud public key
  3. User Identification: If valid, it extracts the userId and frontendToken from the token payload
  4. Persistence: The authentication data is stored in localStorage for the session
  5. Context Sharing: All authentication state is made available through React Context

CORS Configuration

If your webview frontend is hosted separately from your app backend, configure CORS properly:

Troubleshooting

Common Issues

“MentraOS signed user token not found”
  • Ensure your webview is opened through the MentraOS Mobile App
  • Check that the manager app is correctly appending the token to your URL
  • Verify your webview URL is configured correctly in the Developer Console
“Token validation failed”
  • Verify the device clock is synchronized (token expiration is time-sensitive)
  • Check that you’re using the latest version of @mentra/react
  • Ensure the MentraOS Mobile App is up to date
Backend authentication fails
  • Check that you’re sending the Authorization: Bearer ${frontendToken} header
  • Verify your app server is configured to accept the frontend token
  • Ensure CORS is configured properly if frontend and backend are on different domains
Changes not reflecting
  • Clear browser cache and localStorage
  • Restart the MentraOS Mobile App
  • Check browser developer tools for JavaScript errors

Debugging Tips

Enable debug logging to troubleshoot authentication issues:

Next Steps