Fix user registration and validation

This commit is contained in:
Chocobozzz 2018-12-28 16:13:33 +01:00
parent 7ca424e8fe
commit f0026a8783
4 changed files with 39 additions and 42 deletions

View File

@ -121,12 +121,9 @@
},
};
rules = {
password_length: value => value.length > 6 || 'Password must be at least 6 caracters long',
password_length: value => value.length > 6 || 'Password must be at least 6 characters long',
required: value => !!value || 'Required.',
email: (value) => {
const pattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return pattern.test(value) || 'Invalid e-mail.';
},
email: (value: string) => value.indexOf('@') !== -1 || 'Invalid e-mail.',
};
resetState() {
@ -154,20 +151,21 @@
return this.rules.email(this.email) === true ? 'v-gravatar' : 'avatar';
}
submit() {
this.$apollo.mutate({
mutation: CREATE_USER,
variables: {
email: this.email,
password: this.password,
username: this.username,
},
}).then((data) => {
console.log(data);
async submit() {
try {
await this.$apollo.mutate({
mutation: CREATE_USER,
variables: {
email: this.email,
password: this.password,
username: this.username,
},
});
this.validationSent = true;
}).catch((error) => {
} catch (error) {
console.error(error);
});
}
}
};
</script>

View File

@ -19,7 +19,7 @@
<script lang="ts">
import { VALIDATE_USER } from '@/graphql/user';
import { Component, Prop, Vue } from 'vue-property-decorator';
import { AUTH_TOKEN, AUTH_USER_ACTOR, AUTH_USER_ID } from '@/constants';
import { AUTH_TOKEN, AUTH_USER_ID } from '@/constants';
@Component
export default class Validate extends Vue {
@ -32,27 +32,27 @@
this.validateAction();
}
validateAction() {
this.$apollo.mutate({
mutation: VALIDATE_USER,
variables: {
token: this.token,
},
}).then((data) => {
this.loading = false;
console.log(data);
async validateAction() {
try {
const data = await this.$apollo.mutate({
mutation: VALIDATE_USER,
variables: {
token: this.token,
},
});
this.saveUserData(data.data);
this.$router.push({ name: 'Home' });
}).catch((error) => {
this.loading = false;
console.log(error);
} catch (err) {
console.error(err);
this.failed = true;
});
} finally {
this.loading = false;
}
}
saveUserData({ validateUser: login }) {
localStorage.setItem(AUTH_USER_ID, login.user.id);
localStorage.setItem(AUTH_USER_ACTOR, JSON.stringify(login.actor));
localStorage.setItem(AUTH_TOKEN, login.token);
}

View File

@ -3,11 +3,8 @@ import gql from 'graphql-tag';
export const CREATE_USER = gql`
mutation CreateUser($email: String!, $username: String!, $password: String!) {
createUser(email: $email, username: $username, password: $password) {
preferredUsername,
user {
email,
confirmationSentAt
}
email,
confirmationSentAt
}
}
`;
@ -18,10 +15,6 @@ mutation ValidateUser($token: String!) {
token,
user {
id,
},
actor {
avatarUrl,
preferredUsername,
}
}
}

View File

@ -61,7 +61,13 @@ defmodule Mobilizon.Actors do
"""
@spec get_actor_for_user(Mobilizon.Actors.User.t()) :: Mobilizon.Actors.Actor.t()
def get_actor_for_user(%Mobilizon.Actors.User{} = user) do
case Repo.one(from(a in Actor, join: u in User, on: u.default_actor_id == a.id)) do
case Repo.one(
from(a in Actor,
join: u in User,
on: u.default_actor_id == a.id,
where: u.id == ^user.id
)
) do
nil -> get_actors_for_user(user) |> hd
actor -> actor
end