2020-06-27 19:12:45 +02:00
|
|
|
<script lang="ts">
|
2020-11-27 19:27:44 +01:00
|
|
|
import { Component, Vue } from "vue-property-decorator";
|
|
|
|
import { ICurrentUserRole } from "@/types/enums";
|
|
|
|
import { UPDATE_CURRENT_USER_CLIENT, LOGGED_USER } from "../../graphql/user";
|
2020-06-27 19:12:45 +02:00
|
|
|
import RouteName from "../../router/name";
|
|
|
|
import { saveUserData, changeIdentity } from "../../utils/auth";
|
2020-11-27 19:27:44 +01:00
|
|
|
import { IUser } from "../../types/current-user.model";
|
2020-06-27 19:12:45 +02:00
|
|
|
|
|
|
|
@Component
|
|
|
|
export default class ProviderValidate extends Vue {
|
2020-11-27 19:27:44 +01:00
|
|
|
async mounted(): Promise<void> {
|
2020-06-27 19:12:45 +02:00
|
|
|
const accessToken = this.getValueFromMeta("auth-access-token");
|
|
|
|
const refreshToken = this.getValueFromMeta("auth-refresh-token");
|
|
|
|
const userId = this.getValueFromMeta("auth-user-id");
|
|
|
|
const userEmail = this.getValueFromMeta("auth-user-email");
|
2020-11-30 10:24:11 +01:00
|
|
|
const userRole = this.getValueFromMeta(
|
|
|
|
"auth-user-role"
|
|
|
|
) as ICurrentUserRole;
|
2020-06-27 19:12:45 +02:00
|
|
|
|
|
|
|
if (!(userId && userEmail && userRole && accessToken && refreshToken)) {
|
2020-11-27 19:27:44 +01:00
|
|
|
await this.$router.push("/");
|
2020-06-27 19:12:45 +02:00
|
|
|
} else {
|
2020-11-27 19:27:44 +01:00
|
|
|
const login = {
|
2020-11-30 10:24:11 +01:00
|
|
|
user: {
|
|
|
|
id: userId,
|
|
|
|
email: userEmail,
|
|
|
|
role: userRole,
|
|
|
|
isLoggedIn: true,
|
|
|
|
},
|
2020-11-27 19:27:44 +01:00
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
|
|
|
};
|
|
|
|
saveUserData(login);
|
|
|
|
await this.$apollo.mutate({
|
|
|
|
mutation: UPDATE_CURRENT_USER_CLIENT,
|
|
|
|
variables: {
|
|
|
|
id: userId,
|
|
|
|
email: userEmail,
|
|
|
|
isLoggedIn: true,
|
|
|
|
role: ICurrentUserRole.USER,
|
|
|
|
},
|
2020-06-27 19:12:45 +02:00
|
|
|
});
|
2020-11-27 19:27:44 +01:00
|
|
|
const { data } = await this.$apollo.query<{ loggedUser: IUser }>({
|
|
|
|
query: LOGGED_USER,
|
|
|
|
});
|
|
|
|
const { loggedUser } = data;
|
|
|
|
|
|
|
|
if (loggedUser.defaultActor) {
|
2020-11-30 10:24:11 +01:00
|
|
|
await changeIdentity(
|
|
|
|
this.$apollo.provider.defaultClient,
|
|
|
|
loggedUser.defaultActor
|
|
|
|
);
|
2020-11-27 19:27:44 +01:00
|
|
|
await this.$router.push({ name: RouteName.HOME });
|
|
|
|
} else {
|
|
|
|
// If the user didn't register any profile yet, let's create one for them
|
|
|
|
await this.$router.push({
|
|
|
|
name: RouteName.REGISTER_PROFILE,
|
|
|
|
params: { email: loggedUser.email, userAlreadyActivated: "true" },
|
|
|
|
});
|
|
|
|
}
|
2020-06-27 19:12:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:27:44 +01:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
getValueFromMeta(name: string): string | null {
|
2020-06-27 19:12:45 +02:00
|
|
|
const element = document.querySelector(`meta[name="${name}"]`);
|
|
|
|
if (element && element.getAttribute("content")) {
|
|
|
|
return element.getAttribute("content");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|