Webview Authentication
MentraOS provides a simple and secure way for apps to identify users within webviews. When a user opens your web application through the MentraOS Mobile App, you can automatically identify them without requiring a separate login process.What Is Webview Authentication?
Webview authentication lets your web application identify MentraOS users without requiring them to log in separatelyWhen a user opens your webview through the MentraOS Mobile App:
- The manager app automatically appends a temporary authentication token to your URL
- The MentraOS SDK middleware automatically exchanges this token for the user’s ID
- Your webview can then provide a personalized experience based on the user’s identity
When a user opens your website in a regular browser (not the MentraOS app):
Here’s what happens under the hood:- Your app detects the user isn’t authenticated (no
authUserIdon the request). - You redirect them to
/mentra-auth, which the SDK handles automatically. - The user is sent to
account.mentra.glass/auth?packagename=your.package.name— a consent screen where they sign in with their Mentra account (email, Google, or Apple) and authorize your app. - After the user clicks “Allow”, they are redirected back to your app’s webview URL with a signed token (
aos_signed_user_token) in the query parameters. - The SDK middleware automatically validates this token and sets
req.authUserId.
How It Works
1. Include a Webview in Your App
First, specify a webview URL in your app’s configuration through the MentraOS Developer Console:- Log in to the MentraOS Developer Console
- Navigate to your app’s settings
- Add your
Webview URL - Save your changes
2. Set Up Authentication with the SDK
The MentraOS SDK provides built-in middleware that handles the token exchange process automatically:3. Access User ID in Your Routes
In your Express route handlers, access the authenticated user ID:4. Handle unauthenticated users with the OAuth flow
If the user opens your webview in a regular browser (not through the MentraOS app), they won’t have a token automatically. The SDK provides a built-in/mentra-auth route that redirects users to the Mentra OAuth consent screen.
Add a “Sign in with Mentra” button to your unauthenticated view:
You can use the “Sign in with Mentra” image as the login button: https://account.mentra.glass/sign-in-mentra.png
How the OAuth consent screen works
When a user clicks “Sign in with Mentra”, they are redirected to:- Sign in: If the user isn’t already signed in to their Mentra account, they are prompted to sign in (email, Google, or Apple).
- Consent: The user sees your app’s name, icon, and description, and is asked to authorize the app to access their basic profile.
- Redirect: After the user clicks “Allow”, they are redirected back to your app’s Webview URL (configured in the Developer Console) with a signed token appended:
- Token validation: The SDK middleware automatically validates the token and sets
req.authUserIdon all subsequent requests.
The token issued during the OAuth flow expires after 10 minutes. After that, the user will need to re-authenticate. The SDK handles this automatically when the user returns through the MentraOS app.
Common Use Cases
Webview authentication enables:- Personalized dashboards and content
- User-specific settings and preferences
- Integration with your existing user database
- Seamless experiences without additional login steps

