@@ -10,10 +10,10 @@ import {
|
||||
import { ILogin, IToken } from "@/types/login.model";
|
||||
import { UPDATE_CURRENT_USER_CLIENT } from "@/graphql/user";
|
||||
import ApolloClient from "apollo-client";
|
||||
import { ICurrentUserRole } from "@/types/current-user.model";
|
||||
import { IPerson } from "@/types/actor";
|
||||
import { IDENTITIES, UPDATE_CURRENT_ACTOR_CLIENT } from "@/graphql/actor";
|
||||
import { NormalizedCacheObject } from "apollo-cache-inmemory";
|
||||
import { ICurrentUserRole } from "@/types/enums";
|
||||
|
||||
export function saveTokenData(obj: IToken): void {
|
||||
localStorage.setItem(AUTH_ACCESS_TOKEN, obj.accessToken);
|
||||
@@ -37,14 +37,19 @@ export function saveActorData(obj: IPerson): void {
|
||||
}
|
||||
|
||||
export function deleteUserData(): void {
|
||||
[AUTH_USER_ID, AUTH_USER_EMAIL, AUTH_ACCESS_TOKEN, AUTH_REFRESH_TOKEN, AUTH_USER_ROLE].forEach((key) => {
|
||||
localStorage.removeItem(key);
|
||||
});
|
||||
[AUTH_USER_ID, AUTH_USER_EMAIL, AUTH_ACCESS_TOKEN, AUTH_REFRESH_TOKEN, AUTH_USER_ROLE].forEach(
|
||||
(key) => {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export class NoIdentitiesException extends Error {}
|
||||
|
||||
export async function changeIdentity(apollo: ApolloClient<NormalizedCacheObject>, identity: IPerson): Promise<void> {
|
||||
export async function changeIdentity(
|
||||
apollo: ApolloClient<NormalizedCacheObject>,
|
||||
identity: IPerson
|
||||
): Promise<void> {
|
||||
await apollo.mutate({
|
||||
mutation: UPDATE_CURRENT_ACTOR_CLIENT,
|
||||
variables: identity,
|
||||
@@ -69,7 +74,8 @@ export async function initializeCurrentActor(apollo: ApolloClient<any>): Promise
|
||||
console.warn("Logged user has no identities!");
|
||||
throw new NoIdentitiesException();
|
||||
}
|
||||
const activeIdentity = identities.find((identity: IPerson) => identity.id === actorId) || (identities[0] as IPerson);
|
||||
const activeIdentity =
|
||||
identities.find((identity: IPerson) => identity.id === actorId) || (identities[0] as IPerson);
|
||||
|
||||
if (activeIdentity) {
|
||||
await changeIdentity(apollo, activeIdentity);
|
||||
|
||||
Reference in New Issue
Block a user