mobilizon/js/src/utils/auth.ts

144 lines
3.5 KiB
TypeScript
Raw Normal View History

import {
AUTH_ACCESS_TOKEN,
AUTH_REFRESH_TOKEN,
AUTH_USER_ACTOR_ID,
AUTH_USER_EMAIL,
AUTH_USER_ID,
AUTH_USER_ROLE,
USER_LOCALE,
} from "@/constants";
import { ILogin, IToken } from "@/types/login.model";
import { UPDATE_CURRENT_USER_CLIENT } from "@/graphql/user";
import { ApolloClient } from "@apollo/client/core/ApolloClient";
import { IPerson } from "@/types/actor";
import { IDENTITIES, UPDATE_CURRENT_ACTOR_CLIENT } from "@/graphql/actor";
import { ICurrentUserRole } from "@/types/enums";
import { NormalizedCacheObject } from "@apollo/client/cache/inmemory/types";
import { LOGOUT } from "@/graphql/auth";
2019-01-11 13:58:29 +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);
localStorage.setItem(AUTH_USER_ROLE, obj.user.role);
2019-08-12 16:04:16 +02:00
saveTokenData(obj);
}
export function saveLocaleData(locale: string): void {
localStorage.setItem(USER_LOCALE, locale);
}
export function getLocaleData(): string | null {
return localStorage.getItem(USER_LOCALE);
}
export function saveActorData(obj: IPerson): void {
localStorage.setItem(AUTH_USER_ACTOR_ID, `${obj.id}`);
2019-01-11 13:58:29 +01:00
}
2019-01-18 14:47:10 +01:00
export function deleteUserData(): void {
[
AUTH_USER_ID,
AUTH_USER_EMAIL,
AUTH_ACCESS_TOKEN,
AUTH_REFRESH_TOKEN,
AUTH_USER_ACTOR_ID,
AUTH_USER_ROLE,
].forEach((key) => {
localStorage.removeItem(key);
});
2019-01-18 14:47:10 +01:00
}
2019-08-12 16:04:16 +02:00
export class NoIdentitiesException extends Error {}
export async function changeIdentity(
apollo: ApolloClient<NormalizedCacheObject>,
identity: IPerson
): Promise<void> {
await apollo.mutate({
mutation: UPDATE_CURRENT_ACTOR_CLIENT,
variables: identity,
});
saveActorData(identity);
}
/**
* We fetch from localStorage the latest actor ID used,
* then fetch the current identities to set in cache
* the current identity used
*/
export async function initializeCurrentActor(
apollo: ApolloClient<any>
): Promise<void> {
const actorId = localStorage.getItem(AUTH_USER_ACTOR_ID);
const result = await apollo.query({
query: IDENTITIES,
fetchPolicy: "network-only",
});
const { identities } = result.data;
if (identities.length < 1) {
console.warn("Logged user has no identities!");
throw new NoIdentitiesException();
}
const activeIdentity =
identities.find((identity: IPerson) => identity.id === actorId) ||
(identities[0] as IPerson);
if (activeIdentity) {
await changeIdentity(apollo, activeIdentity);
}
}
export async function logout(
apollo: ApolloClient<NormalizedCacheObject>,
performServerLogout = true
): Promise<void> {
if (performServerLogout) {
await apollo.mutate({
mutation: LOGOUT,
variables: {
refreshToken: localStorage.getItem(AUTH_REFRESH_TOKEN),
},
});
}
await apollo.mutate({
2019-08-12 16:04:16 +02:00
mutation: UPDATE_CURRENT_USER_CLIENT,
variables: {
id: null,
email: null,
isLoggedIn: false,
role: ICurrentUserRole.USER,
2019-08-12 16:04:16 +02:00
},
});
await apollo.mutate({
mutation: UPDATE_CURRENT_ACTOR_CLIENT,
variables: {
id: null,
avatar: null,
preferredUsername: null,
name: null,
},
});
2019-08-12 16:04:16 +02:00
deleteUserData();
}
export const SELECTED_PROVIDERS: { [key: string]: string } = {
twitter: "Twitter",
discord: "Discord",
facebook: "Facebook",
github: "Github",
gitlab: "Gitlab",
google: "Google",
keycloak: "Keycloak",
ldap: "LDAP",
};