Files
AdventureLog/frontend/src/hooks.server.ts
Sean Morley 59a103ea8c Cloud (#1170)
* Add new default light and dark themes to match AdventureLog styles

* Allow different theme for site and maps
Fixes #1130

* Update settings page to use translation keys for currency and map style descriptions

* Add global flag to CollectionItineraryItemSerializer and implement map view persistence in frontend

* Update appVersion to v0.12.0-main-052126

* Enhance permissions for shared collections and restrict publicity changes to owners only

* Update appVersion to v0.12.1-beta-052126

* Add new localization strings and descriptions for various languages

- Updated Spanish (es.json) with new map style and preferred currency descriptions.
- Enhanced French (fr.json) with additional details for Strava activities and currency descriptions.
- Added new strings in Hungarian (hu.json) for map style and preferred currency.
- Included additional details in Italian (it.json) for Strava activities and currency descriptions.
- Updated Japanese (ja.json) with new strings for map style and preferred currency.
- Enhanced Korean (ko.json) with additional details for Strava activities and currency descriptions.
- Added new strings in Dutch (nl.json) for map style and preferred currency.
- Updated Norwegian (no.json) with new strings for map style and preferred currency.
- Enhanced Polish (pl.json) with additional details for Strava activities and currency descriptions.
- Updated Brazilian Portuguese (pt-br.json) with new map style and preferred currency descriptions.
- Added new strings in Romanian (ro.json) for additional details and currency descriptions.
- Enhanced Russian (ru.json) with new strings for map style and preferred currency.
- Updated Slovak (sk.json) with additional details for map style and preferred currency.
- Enhanced Swedish (sv.json) with new strings for map style and preferred currency.
- Added new strings in Turkish (tr.json) for map style and preferred currency.
- Updated Ukrainian (uk.json) with new strings for map style and preferred currency.
- Enhanced Chinese (zh.json) with additional details for map style and preferred currency.

* feat: Implement cloud billing system with subscription management

- Added billing app with models, serializers, views, and admin configuration for Subscription.
- Integrated Stripe for payment processing and subscription management.
- Created middleware for cloud access control based on user subscriptions.
- Developed frontend components for subscription status and billing management.
- Updated settings to include cloud mode and Stripe configuration.
- Added routes for billing and subscription management in the API.
- Implemented user subscription creation and management on user registration and checkout.

* feat: Implement media storage limit and usage tracking

- Added media storage limit configuration in settings.
- Introduced utility functions for managing media storage limits and usage.
- Updated models to delete media files using storage backend.
- Enhanced serializers to build media URLs dynamically.
- Implemented media usage tracking in user views and added endpoint for fetching media usage.
- Updated frontend to display media storage usage and limits.
- Added translations for media storage related strings in multiple languages.

* feat: Implement conditional throttling for various external services and update settings for rate limits
2026-05-30 11:02:44 -04:00

113 lines
3.4 KiB
TypeScript

import type { Handle } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
export const authHook: Handle = async ({ event, resolve }) => {
event.cookies.delete('csrftoken', { path: '/' });
event.locals.subscription = null;
event.locals.hasAccess = true;
event.locals.cloudMode = false;
try {
// Image proxy requests can be very high-volume and do not need locals.user.
if (event.url.pathname.startsWith('/immich/')) {
return await resolve(event);
}
let sessionid = event.cookies.get('sessionid');
if (!sessionid) {
event.locals.user = null;
return await resolve(event);
}
const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
const cookie = event.request.headers.get('cookie') || '';
let userFetch = await event.fetch(`${serverEndpoint}/auth/current-user/`, {
headers: {
cookie
}
});
if (!userFetch.ok) {
// Preserve the session on transient backend failures (e.g. 429 throttling)
// to avoid forcing users into a logout loop.
if (userFetch.status === 429 || userFetch.status >= 500) {
event.locals.user = null;
return await resolve(event);
}
event.locals.user = null;
event.cookies.delete('sessionid', { path: '/', secure: event.url.protocol === 'https:' });
return await resolve(event);
}
if (userFetch.ok) {
const payload = await userFetch.json();
event.locals.user = payload.user || null;
event.locals.subscription = payload.subscription || null;
event.locals.hasAccess = payload.has_access ?? true;
event.locals.cloudMode = payload.cloud_mode ?? false;
const setCookieHeader = userFetch.headers.get('Set-Cookie');
if (setCookieHeader) {
// Regular expression to match sessionid cookie and its expiry
const sessionIdRegex = /sessionid=([^;]+).*?expires=([^;]+)/;
const match = setCookieHeader.match(sessionIdRegex);
if (match) {
const sessionId = match[1];
const expiryString = match[2];
const expiryDate = new Date(expiryString);
// Set the sessionid cookie
event.cookies.set('sessionid', sessionId, {
path: '/',
httpOnly: true,
sameSite: 'lax',
secure: event.url.protocol === 'https:',
expires: expiryDate
});
}
}
} else {
event.locals.user = null;
event.cookies.delete('sessionid', { path: '/', secure: event.url.protocol === 'https:' });
}
} catch (error) {
console.error('Error in authHook:', error);
event.locals.user = null;
event.locals.subscription = null;
event.locals.hasAccess = true;
event.locals.cloudMode = false;
event.cookies.delete('sessionid', { path: '/', secure: event.url.protocol === 'https:' });
}
return await resolve(event);
};
export const themeHook: Handle = async ({ event, resolve }) => {
let theme = event.url.searchParams.get('theme') || event.cookies.get('colortheme');
if (theme) {
return await resolve(event, {
transformPageChunk: ({ html }) => html.replace('data-theme=""', `data-theme="${theme}"`)
});
}
return await resolve(event);
};
// hook to get the langauge cookie and set the locale
export const i18nHook: Handle = async ({ event, resolve }) => {
let locale = event.cookies.get('locale');
if (!locale) {
return await resolve(event);
}
event.locals.locale = locale; // Store the locale in locals
return await resolve(event);
};
export const handle = sequence(authHook, themeHook, i18nHook);