update hooks server
This commit is contained in:
parent
56bed7a643
commit
207d347d6c
@ -1,78 +1,46 @@
|
|||||||
import PocketBase from "pocketbase";
|
import PocketBase from "pocketbase";
|
||||||
|
|
||||||
import { serializeNonPOJOs } from '$lib/utils';
|
import { serializeNonPOJOs } from '$lib/utils';
|
||||||
//import { v4 as uuidv4 } from 'uuid';
|
|
||||||
|
|
||||||
|
|
||||||
const usRegion = ['cle1','iad1','pdx1','sfo1'];
|
|
||||||
|
|
||||||
|
|
||||||
|
const usRegion = new Set(['cle1', 'iad1', 'pdx1', 'sfo1']);
|
||||||
|
|
||||||
export const handle = async ({ event, resolve }) => {
|
export const handle = async ({ event, resolve }) => {
|
||||||
|
const regionHeader = event?.request?.headers?.get('x-vercel-id') ?? 'fra1::fra1::8t4xg-1700258428633-157d82fdfcc7';
|
||||||
|
const userRegion = regionHeader.split('::')[0];
|
||||||
|
|
||||||
|
const isUsRegion = usRegion.has(userRegion);
|
||||||
|
const pbUrl = import.meta.env[isUsRegion ? 'VITE_USEAST_POCKETBASE_URL' : 'VITE_EU_POCKETBASE_URL'];
|
||||||
|
const apiURL = import.meta.env[isUsRegion ? 'VITE_USEAST_API_URL' : 'VITE_EU_API_URL'];
|
||||||
|
const fastifyURL = import.meta.env[isUsRegion ? 'VITE_USEAST_FASTIFY_URL' : 'VITE_EU_FASTIFY_URL'];
|
||||||
|
|
||||||
|
event.locals = {
|
||||||
|
region: decodeURIComponent(regionHeader),
|
||||||
|
pb: new PocketBase(pbUrl),
|
||||||
|
apiURL,
|
||||||
|
fastifyURL,
|
||||||
|
apiKey: import.meta.env.VITE_STOCKNEAR_API_KEY
|
||||||
|
};
|
||||||
|
|
||||||
|
const authCookie = event?.request?.headers?.get('cookie') || '';
|
||||||
|
event.locals.pb.authStore.loadFromCookie(authCookie);
|
||||||
|
|
||||||
//let distinctUserId= uuidv4()
|
if (event.locals.pb.authStore.isValid) {
|
||||||
|
|
||||||
|
|
||||||
//event.cookies.set('mixpanelUserId', distinctUserId, { path: '/', httpOnly: true, sameSite: 'strict', maxAge: 60 * 60 * 24 * 365});
|
|
||||||
|
|
||||||
event.locals.region = decodeURIComponent(
|
|
||||||
event?.request?.headers?.get('x-vercel-id') ?? 'fra1::fra1::8t4xg-1700258428633-157d82fdfcc7',
|
|
||||||
);
|
|
||||||
|
|
||||||
const userRegion = event?.locals?.region?.split("::")?.at(0)?.split("::")?.at(0) || '';
|
|
||||||
|
|
||||||
// Set a default API URL
|
|
||||||
let pbUrl;
|
|
||||||
let apiURL;
|
|
||||||
let fastifyURL;
|
|
||||||
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
|
|
||||||
|
|
||||||
|
|
||||||
if (usRegion?.includes(userRegion)) {
|
|
||||||
pbUrl = import.meta.env.VITE_USEAST_POCKETBASE_URL;
|
|
||||||
apiURL = import.meta.env.VITE_USEAST_API_URL;
|
|
||||||
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
|
|
||||||
} else {
|
|
||||||
pbUrl = import.meta.env.VITE_EU_POCKETBASE_URL;
|
|
||||||
apiURL = import.meta.env.VITE_EU_API_URL;
|
|
||||||
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
event.locals.pb = new PocketBase(pbUrl);
|
|
||||||
event.locals.pb.authStore.loadFromCookie(event?.request?.headers?.get('cookie') || '');
|
|
||||||
|
|
||||||
event.locals.apiURL = apiURL;
|
|
||||||
event.locals.fastifyURL = fastifyURL;
|
|
||||||
event.locals.apiKey = apiKey;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (event?.locals?.pb?.authStore?.isValid) {
|
await event.locals.pb.collection('users').authRefresh();
|
||||||
await event?.locals?.pb?.collection('users')?.authRefresh();
|
event.locals.user = serializeNonPOJOs(event.locals.pb.authStore.model);
|
||||||
event.locals.user = serializeNonPOJOs(event?.locals?.pb?.authStore?.model);
|
} catch (_) {
|
||||||
|
event.locals.pb.authStore.clear();
|
||||||
}
|
|
||||||
} catch(_) {
|
|
||||||
event?.locals?.pb?.authStore?.clear();
|
|
||||||
event.locals.user = undefined;
|
event.locals.user = undefined;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const response = await resolve(event);
|
const response = await resolve(event);
|
||||||
|
response.headers.append('set-cookie', event.locals.pb.authStore.exportToCookie({
|
||||||
|
httpOnly: true,
|
||||||
|
path: '/',
|
||||||
response.headers.append('set-cookie', event?.locals?.pb?.authStore?.exportToCookie({ httpOnly: true, path: '/', sameSite: 'lax', secure: true, maxAge: 60 * 60 * 24 * 365}));
|
sameSite: 'lax',
|
||||||
|
secure: true,
|
||||||
|
maxAge: 60 * 60 * 24 * 365
|
||||||
|
}));
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user