@instantdb/core
Version:
Instant's core local abstraction
52 lines • 1.85 kB
JavaScript
function createUserSyncResponse(config, user) {
if (user && user.refresh_token) {
return new Response(JSON.stringify({ ok: true }), {
headers: {
'Content-Type': 'application/json',
// 7 day expiry
'Set-Cookie': `instant_user_${config.appId}=${encodeURIComponent(JSON.stringify(user))}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=604800`,
},
});
}
else {
return new Response(JSON.stringify({ ok: true }), {
headers: {
'Content-Type': 'application/json',
// remove the cookie (some browsers)
'Set-Cookie': `instant_user_${config.appId}=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=-1`,
},
});
}
}
function errorResponse(status, message) {
return new Response(JSON.stringify({ ok: false, error: message }), {
status,
headers: { 'Content-Type': 'application/json' },
});
}
export const createInstantRouteHandler = (config) => {
return {
POST: async (req) => {
let body;
try {
body = await req.json();
}
catch {
return errorResponse(400, 'Invalid JSON body');
}
if (!body.type) {
return errorResponse(400, 'Missing "type" field');
}
if (body.appId !== config.appId) {
return errorResponse(403, 'App ID mismatch');
}
switch (body.type) {
case 'sync-user':
return createUserSyncResponse(config, body.user ?? null);
default:
return errorResponse(400, `Unknown type: ${body.type}`);
}
},
};
};
//# sourceMappingURL=createRouteHandler.js.map