This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/web/src/api/Users.ts
Jens Langhammer 2c54be85be web: prioritise ?locale parameter over saved locale
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
2022-02-26 16:24:29 +01:00

39 lines
1.3 KiB
TypeScript

import { CoreApi, SessionUser } from "@goauthentik/api";
import { activateLocale } from "../interfaces/locale";
import { DEFAULT_CONFIG } from "./Config";
let globalMePromise: Promise<SessionUser>;
export function me(): Promise<SessionUser> {
if (!globalMePromise) {
globalMePromise = new CoreApi(DEFAULT_CONFIG).coreUsersMeRetrieve().then((user) => {
if (!user.user.settings || !("locale" in user.user.settings)) {
return user;
}
const locale = user.user.settings.locale;
if (locale && locale !== "") {
console.debug(`authentik/locale: Activating user's configured locale '${locale}'`);
activateLocale(locale);
}
return user;
}).catch((ex) => {
const defaultUser: SessionUser = {
user: {
pk: -1,
isSuperuser: false,
isActive: true,
groups: [],
avatar: "",
uid: "",
username: "",
name: ""
}
};
if (ex.status === 401 || ex.status === 403) {
window.location.assign("/");
}
return defaultUser;
});
}
return globalMePromise;
}