Solution for authentication errors in Rork mobile app
Error Messages:
❌ Session invalid or expired. Please log in again.
❌ Session expired or invalid. Please log in again.
❌ Authentication required. No credentials provided.
Root Cause:
After merchant logs in with email + OTP, the authentication cookie is NOT being sent with subsequent mobileAPI requests.
✅ Add this to EVERY mobileAPI fetch call in Rork:
credentials: 'include'
Why this works:
credentials: 'include' tells fetch to send authentication cookiesbase44.auth.me()Your login flow is correct. The issue is AFTER login.
// ✅ Login flow is CORRECT
const loginResponse = await fetch(
`https://your-app.base44.com/auth/magic-link?token=${magicLinkToken}`,
{
method: 'GET',
credentials: 'include' // ✅ This sets the cookie
}
);// ❌ WRONG - Cookie not sent
async function getMerchantOrders() {
const response = await fetch('https://your-app.base44.com/functions/mobileAPI', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
// ❌ MISSING: credentials: 'include'
body: JSON.stringify({
action: 'merchantOrders'
})
});
const data = await response.json();
// ❌ Result: { success: false, error: "Session invalid or expired" }
return data;
}// ✅ CORRECT - Cookie sent
async function getMerchantOrders() {
const response = await fetch('https://your-app.base44.com/functions/mobileAPI', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include', // ✅ CRITICAL: Send auth cookie
body: JSON.stringify({
action: 'merchantOrders'
})
});
const data = await response.json();
// ✅ Result: { success: true, data: [...orders] }
return data;
}// RORK: Apply this pattern to ALL mobileAPI calls
class MobileAPI {
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
async call(action, data = {}) {
const response = await fetch(`${this.baseUrl}/functions/mobileAPI`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include', // ✅ ALWAYS include this
body: JSON.stringify({
action,
...data
})
});
return await response.json();
}
}
// Usage
const api = new MobileAPI('https://your-app.base44.com');
// All calls now automatically include credentials
await api.call('merchantProfile');
await api.call('merchantOrders', { params: { store_id: 'store_123' } });
await api.call('myOrders');
await api.call('getCart');Check Network Tab
In browser DevTools, verify 'Cookie' header is present in mobileAPI requests
Check Response
API should return { success: true, data: {...} } instead of 401 errors
Check Console Logs
Look for '[mobileAPI] ✅ Token authentication successful' in backend logs
Test Full Flow
Login → Call merchantProfile → Should return merchant data, not error
Test the complete auth flow using the built-in debugger: