mirror of
https://github.com/immich-app/immich.git
synced 2025-12-18 01:11:07 +03:00
* refactor: inline warning * fix: do not use onmount * chore: remove outdated comment * wording --------- Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
54 lines
1.1 KiB
TypeScript
54 lines
1.1 KiB
TypeScript
import { api } from '@api';
|
|
import { redirect } from '@sveltejs/kit';
|
|
import { AppRoute } from '../constants';
|
|
import { getSavedUser, setUser } from '$lib/stores/user.store';
|
|
import { serverInfo } from '$lib/stores/server-info.store';
|
|
|
|
export interface AuthOptions {
|
|
admin?: true;
|
|
}
|
|
|
|
export const getAuthUser = async () => {
|
|
try {
|
|
const { data: user } = await api.userApi.getMyUserInfo();
|
|
return user;
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
export const authenticate = async (options?: AuthOptions) => {
|
|
options = options || {};
|
|
|
|
const savedUser = getSavedUser();
|
|
const user = savedUser || (await getAuthUser());
|
|
|
|
if (!user) {
|
|
redirect(302, AppRoute.AUTH_LOGIN);
|
|
}
|
|
|
|
if (options.admin && !user.isAdmin) {
|
|
redirect(302, AppRoute.PHOTOS);
|
|
}
|
|
|
|
if (!savedUser) {
|
|
setUser(user);
|
|
}
|
|
};
|
|
|
|
export const requestServerInfo = async () => {
|
|
if (getSavedUser()) {
|
|
const { data } = await api.serverInfoApi.getServerInfo();
|
|
serverInfo.set(data);
|
|
}
|
|
};
|
|
|
|
export const isLoggedIn = async () => {
|
|
const savedUser = getSavedUser();
|
|
const user = savedUser || (await getAuthUser());
|
|
if (!savedUser) {
|
|
setUser(user);
|
|
}
|
|
return user;
|
|
};
|