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. Auth middleware is applied automatically byMiniAppServer. You don’t need to set it up - just call getMentraAuth(c) on any route.
What Is Webview Authentication?
Webview authentication lets your web application identify MentraOS users without requiring them to log in separately.When a user opens your webview through the MentraOS Mobile App:
- The MentraOS app automatically appends a temporary authentication token to your URL
- The SDK’s built-in auth middleware automatically exchanges this token for the user’s ID and sets a signed cookie
- 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
userIdfromgetMentraAuth(c)) - You redirect them to
/mentra-auth, which the SDK handles automatically - The user is sent to
account.mentra.glass/auth?packagename=your.package.namewhere 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 in the query parameters
- The auth middleware automatically validates this token and sets a signed cookie for future requests
How It Works
1. Include a Webview in Your App
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. Authentication Is Automatic
MiniAppServer applies auth middleware to all routes automatically. You don’t need to call createAuthMiddleware or configure a cookieSecret - the SDK uses your apiKey to sign session cookies by default.
3. Access User Identity in Your Routes
CallgetMentraAuth(c) on any route to get the user’s identity. The auth context is available everywhere - no middleware application needed per-route.
auth.userId is null. It’s your route handler’s job to check and return a 401 or redirect if needed.
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:
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 auth middleware automatically validates the token and sets a signed cookie for future 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.
Complete Example
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
Migrating from v2
In v2, authentication used Express middleware withreq.authUserId:
getExpressApp()is removed. Routes are added directly on theMiniAppServerinstance.AuthenticatedRequestandreq.authUserIdare replaced bygetMentraAuth(c).userId.- No
createAuthMiddleware()call needed. Auth middleware is applied automatically byMiniAppServer. - No
cookieSecretneeded. The SDK uses yourapiKeyto sign cookies by default.
React Webviews
If you want to build your frontend webview using React, see the React Webviews guide. The@mentra/react package handles authentication on the frontend - it works the same regardless of whether your backend uses v2 or v3.
