2019-09-11 09:59:01 +02:00
|
|
|
import {
|
|
|
|
AUTH_ACCESS_TOKEN,
|
|
|
|
AUTH_REFRESH_TOKEN,
|
|
|
|
AUTH_USER_ACTOR_ID,
|
|
|
|
AUTH_USER_EMAIL,
|
|
|
|
AUTH_USER_ID,
|
|
|
|
AUTH_USER_ROLE,
|
2020-10-28 18:58:43 +01:00
|
|
|
USER_LOCALE,
|
2020-02-18 08:57:00 +01:00
|
|
|
} from "@/constants";
|
|
|
|
import { ILogin, IToken } from "@/types/login.model";
|
|
|
|
import { UPDATE_CURRENT_USER_CLIENT } from "@/graphql/user";
|
2022-07-12 10:55:28 +02:00
|
|
|
import { UPDATE_CURRENT_ACTOR_CLIENT } from "@/graphql/actor";
|
2020-11-27 19:27:44 +01:00
|
|
|
import { ICurrentUserRole } from "@/types/enums";
|
2021-05-25 11:00:46 +02:00
|
|
|
import { LOGOUT } from "@/graphql/auth";
|
2022-07-12 10:55:28 +02:00
|
|
|
import { provideApolloClient, useMutation } from "@vue/apollo-composable";
|
|
|
|
import { apolloClient } from "@/vue-apollo";
|
2019-01-11 13:58:29 +01:00
|
|
|
|
2020-10-28 18:58:43 +01:00
|
|
|
export function saveTokenData(obj: IToken): void {
|
|
|
|
localStorage.setItem(AUTH_ACCESS_TOKEN, obj.accessToken);
|
|
|
|
localStorage.setItem(AUTH_REFRESH_TOKEN, obj.refreshToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function saveUserData(obj: ILogin): void {
|
2019-01-11 13:58:29 +01:00
|
|
|
localStorage.setItem(AUTH_USER_ID, `${obj.user.id}`);
|
2019-01-18 14:47:10 +01:00
|
|
|
localStorage.setItem(AUTH_USER_EMAIL, obj.user.email);
|
2019-09-09 09:31:08 +02:00
|
|
|
localStorage.setItem(AUTH_USER_ROLE, obj.user.role);
|
2019-08-12 16:04:16 +02:00
|
|
|
|
|
|
|
saveTokenData(obj);
|
|
|
|
}
|
|
|
|
|
2020-10-28 18:58:43 +01:00
|
|
|
export function saveLocaleData(locale: string): void {
|
|
|
|
localStorage.setItem(USER_LOCALE, locale);
|
2019-09-11 09:59:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 17:57:08 +01:00
|
|
|
export function getLocaleData(): string | null {
|
2022-05-30 12:20:24 +02:00
|
|
|
return localStorage ? localStorage.getItem(USER_LOCALE) : null;
|
2020-11-30 17:57:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-28 18:58:43 +01:00
|
|
|
export function deleteUserData(): void {
|
2020-11-30 10:24:11 +01:00
|
|
|
[
|
|
|
|
AUTH_USER_ID,
|
|
|
|
AUTH_USER_EMAIL,
|
|
|
|
AUTH_ACCESS_TOKEN,
|
|
|
|
AUTH_REFRESH_TOKEN,
|
2021-08-20 18:48:40 +02:00
|
|
|
AUTH_USER_ACTOR_ID,
|
2020-11-30 10:24:11 +01:00
|
|
|
AUTH_USER_ROLE,
|
|
|
|
].forEach((key) => {
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
});
|
2019-01-18 14:47:10 +01:00
|
|
|
}
|
2019-08-12 16:04:16 +02:00
|
|
|
|
2023-10-17 16:41:31 +02:00
|
|
|
const { mutate: logoutMutation } = provideApolloClient(apolloClient)(() =>
|
|
|
|
useMutation(LOGOUT)
|
|
|
|
);
|
|
|
|
const { mutate: cleanUserClient } = provideApolloClient(apolloClient)(() =>
|
|
|
|
useMutation(UPDATE_CURRENT_USER_CLIENT)
|
|
|
|
);
|
|
|
|
const { mutate: cleanActorClient } = provideApolloClient(apolloClient)(() =>
|
|
|
|
useMutation(UPDATE_CURRENT_ACTOR_CLIENT)
|
|
|
|
);
|
2019-09-18 17:32:37 +02:00
|
|
|
|
2023-10-17 16:41:31 +02:00
|
|
|
export async function logout(performServerLogout = true): Promise<void> {
|
2021-08-13 17:47:26 +02:00
|
|
|
if (performServerLogout) {
|
2022-08-26 16:08:58 +02:00
|
|
|
logoutMutation({
|
2022-07-12 10:55:28 +02:00
|
|
|
refreshToken: localStorage.getItem(AUTH_REFRESH_TOKEN),
|
2021-08-13 17:47:26 +02:00
|
|
|
});
|
|
|
|
}
|
2021-05-25 11:00:46 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
cleanUserClient({
|
|
|
|
id: null,
|
|
|
|
email: null,
|
|
|
|
isLoggedIn: false,
|
|
|
|
role: ICurrentUserRole.USER,
|
2019-08-12 16:04:16 +02:00
|
|
|
});
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
cleanActorClient({
|
|
|
|
id: null,
|
|
|
|
avatar: null,
|
|
|
|
preferredUsername: null,
|
|
|
|
name: null,
|
2019-09-18 17:32:37 +02:00
|
|
|
});
|
|
|
|
|
2019-08-12 16:04:16 +02:00
|
|
|
deleteUserData();
|
|
|
|
}
|
2020-06-27 19:12:45 +02:00
|
|
|
|
|
|
|
export const SELECTED_PROVIDERS: { [key: string]: string } = {
|
|
|
|
twitter: "Twitter",
|
|
|
|
discord: "Discord",
|
|
|
|
facebook: "Facebook",
|
|
|
|
github: "Github",
|
|
|
|
gitlab: "Gitlab",
|
|
|
|
google: "Google",
|
|
|
|
keycloak: "Keycloak",
|
|
|
|
ldap: "LDAP",
|
2022-05-02 14:25:42 +02:00
|
|
|
cas: "CAS",
|
2020-06-27 19:12:45 +02:00
|
|
|
};
|