This commit is contained in:
pulipakaa24
2026-03-20 00:28:47 -05:00
2 changed files with 27 additions and 0 deletions

View File

@@ -58,5 +58,8 @@ export const auth = betterAuth({
trustedOrigins: [ trustedOrigins: [
'http://localhost:5173', 'http://localhost:5173',
'https://labwise.wahwa.com', 'https://labwise.wahwa.com',
// iOS native app callback — allows Better Auth to honour the
// https://labwise.wahwa.com/api/ios-callback callbackURL
'labwise://',
], ],
}); });

View File

@@ -4,6 +4,7 @@ import cors from 'cors';
import { toNodeHandler } from 'better-auth/node'; import { toNodeHandler } from 'better-auth/node';
import { auth } from './auth/auth'; import { auth } from './auth/auth';
import { authRateLimiter, apiRateLimiter } from './auth/rateLimiter'; import { authRateLimiter, apiRateLimiter } from './auth/rateLimiter';
import { requireAuth } from './auth/middleware';
import chemicalsRouter from './routes/chemicals'; import chemicalsRouter from './routes/chemicals';
import protocolsRouter from './routes/protocols'; import protocolsRouter from './routes/protocols';
import profileRouter from './routes/profile'; import profileRouter from './routes/profile';
@@ -27,6 +28,29 @@ app.use(cors({
// Serve uploaded files // Serve uploaded files
app.use('/uploads', express.static(UPLOADS_DIR)); app.use('/uploads', express.static(UPLOADS_DIR));
// iOS OAuth callback — must be registered before the Better Auth wildcard
// so Express matches this specific path first.
// Better Auth completes the Google flow, sets the session cookie, then
// redirects to this endpoint (passed as callbackURL from the native app).
// We read the raw session token out of the cookie and forward it in the
// custom URL scheme so the iOS app can inject it into URLSession's cookie jar.
app.get('/api/ios-callback', requireAuth, (req, res) => {
const cookieHeader = req.headers.cookie ?? '';
const token = cookieHeader
.split(';')
.map(c => c.trim())
.find(c => c.startsWith('better-auth.session_token='))
?.split('=')
.slice(1)
.join('='); // re-join in case the value itself contains '='
if (!token) {
return res.redirect('labwise://auth?error=no_session');
}
res.redirect(`labwise://auth?token=${encodeURIComponent(token)}`);
});
// Better Auth — must come before express.json() so it can read its own body // Better Auth — must come before express.json() so it can read its own body
app.use('/api/auth/*', authRateLimiter); app.use('/api/auth/*', authRateLimiter);
app.all('/api/auth/*', toNodeHandler(auth)); app.all('/api/auth/*', toNodeHandler(auth));