ios support stuff
This commit is contained in:
@@ -4,6 +4,7 @@ import cors from 'cors';
|
||||
import { toNodeHandler } from 'better-auth/node';
|
||||
import { auth } from './auth/auth';
|
||||
import { authRateLimiter, apiRateLimiter } from './auth/rateLimiter';
|
||||
import { requireAuth } from './auth/middleware';
|
||||
import chemicalsRouter from './routes/chemicals';
|
||||
import protocolsRouter from './routes/protocols';
|
||||
import profileRouter from './routes/profile';
|
||||
@@ -27,6 +28,29 @@ app.use(cors({
|
||||
// Serve uploaded files
|
||||
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
|
||||
app.use('/api/auth/*', authRateLimiter);
|
||||
app.all('/api/auth/*', toNodeHandler(auth));
|
||||
|
||||
Reference in New Issue
Block a user