immich/web/src/lib/utils/auth.ts
renovate[bot] df970da59e
chore(deps): update dependency eslint-plugin-unicorn to v70 - abandoned (#29684)
* chore(deps): update dependency eslint-plugin-unicorn to v70

* fix: linting

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Daniel Dietzler <mail@ddietzler.dev>
2026-07-20 23:47:14 -04:00

49 lines
1.2 KiB
TypeScript

import { getStorage } from '@immich/sdk';
import { redirect } from '@sveltejs/kit';
import { DateTime } from 'luxon';
import { authManager } from '$lib/managers/auth-manager.svelte';
import { Route } from '$lib/route';
import { userInteraction } from '$lib/stores/user.svelte';
export interface AuthOptions {
admin?: true;
public?: boolean;
}
export const authenticate = async (url: URL, options?: AuthOptions) => {
const { public: publicRoute, admin: adminRoute } = options || {};
await authManager.load();
if (publicRoute) {
return;
}
if (!authManager.authenticated) {
redirect(307, Route.login({ continue: url.pathname + url.search }));
}
if (adminRoute && !authManager.user.isAdmin) {
redirect(307, Route.photos());
}
};
export const requestServerInfo = async () => {
if (!authManager.authenticated) {
return;
}
const data = await getStorage();
userInteraction.serverInfo = data;
};
export const getAccountAge = (): number => {
if (!authManager.authenticated) {
return 0;
}
const createdDate = DateTime.fromISO(authManager.user.createdAt);
const now = DateTime.now();
const accountAge = now.diff(createdDate, 'days').days.toFixed(0);
return Number(accountAge);
};