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,
|
|
|
|
} from '@/constants';
|
2019-08-12 16:04:16 +02:00
|
|
|
import { ILogin, IToken } from '@/types/login.model';
|
|
|
|
import { UPDATE_CURRENT_USER_CLIENT } from '@/graphql/user';
|
|
|
|
import { onLogout } from '@/vue-apollo';
|
|
|
|
import ApolloClient from 'apollo-client';
|
2019-09-09 09:31:08 +02:00
|
|
|
import { ICurrentUserRole } from '@/types/current-user.model';
|
2019-09-11 09:59:01 +02:00
|
|
|
import { IPerson } from '@/types/actor';
|
2019-09-18 17:32:37 +02:00
|
|
|
import { IDENTITIES, UPDATE_CURRENT_ACTOR_CLIENT } from '@/graphql/actor';
|
2019-01-11 13:58:29 +01:00
|
|
|
|
|
|
|
export function saveUserData(obj: ILogin) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:59:01 +02:00
|
|
|
export function saveActorData(obj: IPerson) {
|
|
|
|
localStorage.setItem(AUTH_USER_ACTOR_ID, `${obj.id}`);
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:04:16 +02:00
|
|
|
export function saveTokenData(obj: IToken) {
|
|
|
|
localStorage.setItem(AUTH_ACCESS_TOKEN, obj.accessToken);
|
|
|
|
localStorage.setItem(AUTH_REFRESH_TOKEN, obj.refreshToken);
|
2019-01-11 13:58:29 +01:00
|
|
|
}
|
2019-01-18 14:47:10 +01:00
|
|
|
|
|
|
|
export function deleteUserData() {
|
2019-09-18 17:32:37 +02:00
|
|
|
for (const key of [AUTH_USER_ID, AUTH_USER_EMAIL, AUTH_ACCESS_TOKEN, AUTH_REFRESH_TOKEN, AUTH_USER_ROLE]) {
|
2019-01-18 14:47:10 +01:00
|
|
|
localStorage.removeItem(key);
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 16:04:16 +02:00
|
|
|
|
2019-10-12 13:16:36 +02:00
|
|
|
export class NoIdentitiesException extends Error {}
|
|
|
|
|
2019-09-18 17:32:37 +02:00
|
|
|
/**
|
|
|
|
* 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>) {
|
|
|
|
const actorId = localStorage.getItem(AUTH_USER_ACTOR_ID);
|
|
|
|
|
|
|
|
const result = await apollo.query({
|
|
|
|
query: IDENTITIES,
|
2019-09-26 18:01:38 +02:00
|
|
|
fetchPolicy: 'network-only',
|
2019-09-18 17:32:37 +02:00
|
|
|
});
|
|
|
|
const identities = result.data.identities;
|
2019-10-12 13:16:36 +02:00
|
|
|
if (identities.length < 1) {
|
|
|
|
console.warn('Logged user has no identities!');
|
|
|
|
throw new NoIdentitiesException;
|
|
|
|
}
|
2019-09-18 17:32:37 +02:00
|
|
|
const activeIdentity = identities.find(identity => identity.id === actorId) || identities[0] as IPerson;
|
|
|
|
|
|
|
|
if (activeIdentity) {
|
|
|
|
return await changeIdentity(apollo, activeIdentity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:59:01 +02:00
|
|
|
export async function changeIdentity(apollo: ApolloClient<any>, identity: IPerson) {
|
|
|
|
await apollo.mutate({
|
|
|
|
mutation: UPDATE_CURRENT_ACTOR_CLIENT,
|
|
|
|
variables: identity,
|
|
|
|
});
|
|
|
|
saveActorData(identity);
|
|
|
|
}
|
|
|
|
|
2019-09-18 17:32:37 +02:00
|
|
|
export async function logout(apollo: ApolloClient<any>) {
|
|
|
|
await apollo.mutate({
|
2019-08-12 16:04:16 +02:00
|
|
|
mutation: UPDATE_CURRENT_USER_CLIENT,
|
|
|
|
variables: {
|
|
|
|
id: null,
|
|
|
|
email: null,
|
|
|
|
isLoggedIn: false,
|
2019-09-09 09:31:08 +02:00
|
|
|
role: ICurrentUserRole.USER,
|
2019-08-12 16:04:16 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-09-18 17:32:37 +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();
|
|
|
|
|
2019-09-18 17:32:37 +02:00
|
|
|
await onLogout();
|
2019-08-12 16:04:16 +02:00
|
|
|
}
|