2024-02-13 02:47:26 +01:00
|
|
|
import { browser } from '$app/environment';
|
2024-07-18 10:56:27 -05:00
|
|
|
import { licenseStore } from '$lib/stores/license.store';
|
2024-02-13 17:07:37 -05:00
|
|
|
import { serverInfo } from '$lib/stores/server-info.store';
|
2024-05-27 22:16:53 -04:00
|
|
|
import { preferences as preferences$, user as user$ } from '$lib/stores/user.store';
|
2024-07-18 10:56:27 -05:00
|
|
|
import { getAboutInfo, getMyPreferences, getMyUser, getStorage } from '@immich/sdk';
|
2024-02-13 17:07:37 -05:00
|
|
|
import { redirect } from '@sveltejs/kit';
|
2024-07-18 10:56:27 -05:00
|
|
|
import { DateTime } from 'luxon';
|
2024-02-13 17:07:37 -05:00
|
|
|
import { get } from 'svelte/store';
|
|
|
|
import { AppRoute } from '../constants';
|
2023-11-17 23:13:36 -05:00
|
|
|
|
|
|
|
export interface AuthOptions {
|
|
|
|
admin?: true;
|
2024-02-13 02:47:26 +01:00
|
|
|
public?: true;
|
2023-11-17 23:13:36 -05:00
|
|
|
}
|
|
|
|
|
2024-02-13 02:47:26 +01:00
|
|
|
export const loadUser = async () => {
|
2023-11-17 23:13:36 -05:00
|
|
|
try {
|
2024-05-27 22:16:53 -04:00
|
|
|
let user = get(user$);
|
|
|
|
let preferences = get(preferences$);
|
2024-07-18 10:56:27 -05:00
|
|
|
let serverInfo;
|
|
|
|
|
2024-05-27 22:16:53 -04:00
|
|
|
if ((!user || !preferences) && hasAuthCookie()) {
|
2024-07-18 10:56:27 -05:00
|
|
|
[user, preferences, serverInfo] = await Promise.all([getMyUser(), getMyPreferences(), getAboutInfo()]);
|
2024-05-27 22:16:53 -04:00
|
|
|
user$.set(user);
|
|
|
|
preferences$.set(preferences);
|
2024-07-18 10:56:27 -05:00
|
|
|
|
|
|
|
// Check for license status
|
|
|
|
if (serverInfo.licensed || user.license?.activatedAt) {
|
|
|
|
licenseStore.setLicenseStatus(true);
|
|
|
|
}
|
2024-02-13 02:47:26 +01:00
|
|
|
}
|
2024-05-27 22:16:53 -04:00
|
|
|
return user;
|
2023-11-17 23:13:36 -05:00
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-13 02:47:26 +01:00
|
|
|
const hasAuthCookie = (): boolean => {
|
|
|
|
if (!browser) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const cookie of document.cookie.split('; ')) {
|
|
|
|
const [name] = cookie.split('=');
|
|
|
|
if (name === 'immich_is_authenticated') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2023-11-17 23:13:36 -05:00
|
|
|
export const authenticate = async (options?: AuthOptions) => {
|
2024-02-13 02:47:26 +01:00
|
|
|
const { public: publicRoute, admin: adminRoute } = options || {};
|
|
|
|
const user = await loadUser();
|
2023-11-17 23:13:36 -05:00
|
|
|
|
2024-02-13 02:47:26 +01:00
|
|
|
if (publicRoute) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-12 03:35:57 +01:00
|
|
|
|
2023-11-17 23:13:36 -05:00
|
|
|
if (!user) {
|
2024-01-20 13:47:41 -05:00
|
|
|
redirect(302, AppRoute.AUTH_LOGIN);
|
2023-11-17 23:13:36 -05:00
|
|
|
}
|
|
|
|
|
2024-02-13 02:47:26 +01:00
|
|
|
if (adminRoute && !user.isAdmin) {
|
2024-01-20 13:47:41 -05:00
|
|
|
redirect(302, AppRoute.PHOTOS);
|
2023-11-17 23:13:36 -05:00
|
|
|
}
|
2023-12-12 17:35:28 +01:00
|
|
|
};
|
2023-12-12 03:35:57 +01:00
|
|
|
|
2024-01-30 18:21:45 +01:00
|
|
|
export const requestServerInfo = async () => {
|
2024-05-27 22:16:53 -04:00
|
|
|
if (get(user$)) {
|
2024-05-22 10:25:55 +01:00
|
|
|
const data = await getStorage();
|
2024-01-30 18:21:45 +01:00
|
|
|
serverInfo.set(data);
|
|
|
|
}
|
|
|
|
};
|
2024-07-18 10:56:27 -05:00
|
|
|
|
|
|
|
export const getAccountAge = (): number => {
|
|
|
|
const user = get(user$);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const createdDate = DateTime.fromISO(user.createdAt);
|
|
|
|
const now = DateTime.now();
|
|
|
|
const accountAge = now.diff(createdDate, 'days').days.toFixed(0);
|
|
|
|
|
|
|
|
return Number(accountAge);
|
|
|
|
};
|