From 1b470f3f044eec43bbc636722147171cdead30c3 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 30 Jan 2019 15:54:21 +0100 Subject: [PATCH] Handle validated users without profiles Signed-off-by: Thomas Citharel Format Signed-off-by: Thomas Citharel --- js/src/graphql/user.ts | 6 +++++- js/src/router/user.ts | 3 ++- js/src/views/Account/Register.vue | 11 ++++++++--- js/src/views/User/Validate.vue | 21 ++++++++++++++------- lib/mobilizon_web/resolvers/person.ex | 4 ++++ lib/mobilizon_web/resolvers/user.ex | 2 +- 6 files changed, 34 insertions(+), 13 deletions(-) diff --git a/js/src/graphql/user.ts b/js/src/graphql/user.ts index 0db4b5486..07a16aba0 100644 --- a/js/src/graphql/user.ts +++ b/js/src/graphql/user.ts @@ -15,6 +15,10 @@ mutation ValidateUser($token: String!) { token, user { id, + email, + defaultActor { + id + } } } } @@ -33,4 +37,4 @@ export const UPDATE_CURRENT_USER_CLIENT = gql` mutation UpdateCurrentUser($id: Int!, $email: String!) { updateCurrentUser(id: $id, email: $email) @client } -` +`; diff --git a/js/src/router/user.ts b/js/src/router/user.ts index bdec67529..811723730 100644 --- a/js/src/router/user.ts +++ b/js/src/router/user.ts @@ -46,7 +46,8 @@ export default [ path: '/validate/:token', name: 'Validate', component: Validate, - props: true, + // We can only pass string values through params, therefore + props: (route) => ({ email: route.params.email, userAlreadyActivated: route.params.userAlreadyActivated === 'true'}), meta: { requiresAuth: false }, }, { diff --git a/js/src/views/Account/Register.vue b/js/src/views/Account/Register.vue index a644049f0..0e9b2a727 100644 --- a/js/src/views/Account/Register.vue +++ b/js/src/views/Account/Register.vue @@ -57,7 +57,7 @@ -
+

{ acc[error.details] = error.message; diff --git a/js/src/views/User/Validate.vue b/js/src/views/User/Validate.vue index 801a13a9a..9187aed12 100644 --- a/js/src/views/User/Validate.vue +++ b/js/src/views/User/Validate.vue @@ -5,8 +5,8 @@

- - Error while validating account + + Either the account is already validated, either the validation token is incorrect.

@@ -28,21 +28,28 @@ export default class Validate extends Vue { loading = true; failed = false; - created() { - this.validateAction(); + async created() { + await this.validateAction(); } async validateAction() { try { - const data = await this.$apollo.mutate({ + const { data } = await this.$apollo.mutate({ mutation: VALIDATE_USER, variables: { token: this.token } }); - this.saveUserData(data.data); - this.$router.push({ name: "Home" }); + this.saveUserData(data); + + const user = data.validateUser.user; + console.log(user); + if (user.defaultActor) { + this.$router.push({name: "Home"}); + } else { // If the user didn't register any profile yet, let's create one for them + this.$router.push({ name: 'RegisterProfile', params: {email: user.email, userAlreadyActivated: 'true'} }); + } } catch (err) { console.error(err); this.failed = true; diff --git a/lib/mobilizon_web/resolvers/person.ex b/lib/mobilizon_web/resolvers/person.ex index 2258d49d7..8a3d4ba75 100644 --- a/lib/mobilizon_web/resolvers/person.ex +++ b/lib/mobilizon_web/resolvers/person.ex @@ -77,8 +77,12 @@ defmodule MobilizonWeb.Resolvers.Person do else {:error, :user_not_found} -> {:error, "User with email not found"} + {:no_actor, _} -> {:error, "You already have a profile for this user"} + + {:error, %Ecto.Changeset{} = e} -> + {:error, e} end end end diff --git a/lib/mobilizon_web/resolvers/user.ex b/lib/mobilizon_web/resolvers/user.ex index f8d45fbb7..12594c6a6 100644 --- a/lib/mobilizon_web/resolvers/user.ex +++ b/lib/mobilizon_web/resolvers/user.ex @@ -63,7 +63,7 @@ defmodule MobilizonWeb.Resolvers.User do {:get_actor, actor} <- {:get_actor, Actors.get_actor_for_user(user)}, {:guardian_encode_and_sign, {:ok, token, _}} <- {:guardian_encode_and_sign, MobilizonWeb.Guardian.encode_and_sign(user)} do - {:ok, %{token: token, user: user, person: actor}} + {:ok, %{token: token, user: Map.put(user, :default_actor, actor)}} else err -> Logger.info("Unable to validate user with token #{token}")