2019-01-21 15:08:22 +01:00
|
|
|
<template>
|
2019-04-03 17:29:03 +02:00
|
|
|
<div class="container">
|
2019-01-21 15:08:22 +01:00
|
|
|
<section class="hero">
|
|
|
|
<h1 class="title">
|
|
|
|
<translate>Welcome back!</translate>
|
|
|
|
</h1>
|
|
|
|
</section>
|
2019-04-01 11:49:54 +02:00
|
|
|
|
|
|
|
<b-message v-if="errorCode === LoginErrorCode.NEED_TO_LOGIN" title="Info" type="is-info">
|
|
|
|
<translate>You need to login.</translate>
|
|
|
|
</b-message>
|
|
|
|
|
|
|
|
<section v-if="!currentUser.isLoggedIn">
|
2019-01-21 15:08:22 +01:00
|
|
|
<div class="columns is-mobile is-centered">
|
2019-04-03 17:29:03 +02:00
|
|
|
<div class="column is-half">
|
2019-01-21 15:08:22 +01:00
|
|
|
<b-message title="Error" type="is-danger" v-for="error in errors" :key="error">{{ error }}</b-message>
|
|
|
|
<form @submit="loginAction">
|
2019-04-03 17:29:03 +02:00
|
|
|
<b-field :label="$gettext('Email')">
|
2019-01-21 15:08:22 +01:00
|
|
|
<b-input aria-required="true" required type="email" v-model="credentials.email"/>
|
|
|
|
</b-field>
|
|
|
|
|
2019-04-03 17:29:03 +02:00
|
|
|
<b-field :label="$gettext('Password')">
|
2019-01-21 15:08:22 +01:00
|
|
|
<b-input
|
|
|
|
aria-required="true"
|
|
|
|
required
|
|
|
|
type="password"
|
|
|
|
password-reveal
|
|
|
|
v-model="credentials.password"
|
|
|
|
/>
|
|
|
|
</b-field>
|
|
|
|
|
|
|
|
<div class="control has-text-centered">
|
|
|
|
<button class="button is-primary is-large">
|
|
|
|
<translate>Login</translate>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="control">
|
|
|
|
<router-link
|
|
|
|
class="button is-text"
|
|
|
|
:to="{ name: 'SendPasswordReset', params: { email: credentials.email }}"
|
|
|
|
>
|
|
|
|
<translate>Forgot your password ?</translate>
|
|
|
|
</router-link>
|
|
|
|
</div>
|
2019-03-22 13:58:19 +01:00
|
|
|
<div class="control" v-if="config && config.registrationsOpen">
|
2019-01-21 15:08:22 +01:00
|
|
|
<router-link
|
|
|
|
class="button is-text"
|
|
|
|
:to="{ name: 'Register', params: { default_email: credentials.email, default_password: credentials.password }}"
|
|
|
|
>
|
|
|
|
<translate>Register</translate>
|
|
|
|
</router-link>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</section>
|
2019-04-01 11:49:54 +02:00
|
|
|
|
|
|
|
<b-message v-else title="Error" type="is-error">
|
|
|
|
<translate>You are already logged-in.</translate>
|
|
|
|
</b-message>
|
2019-01-21 15:08:22 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2019-03-22 10:57:14 +01:00
|
|
|
import { Component, Prop, Vue } from 'vue-property-decorator';
|
|
|
|
import { LOGIN } from '@/graphql/auth';
|
|
|
|
import { validateEmailField, validateRequiredField } from '@/utils/validators';
|
|
|
|
import { saveUserData } from '@/utils/auth';
|
|
|
|
import { ILogin } from '@/types/login.model';
|
2019-04-01 11:49:54 +02:00
|
|
|
import { CURRENT_USER_CLIENT, UPDATE_CURRENT_USER_CLIENT } from '@/graphql/user';
|
2019-03-22 10:57:14 +01:00
|
|
|
import { onLogin } from '@/vue-apollo';
|
|
|
|
import { RouteName } from '@/router';
|
2019-04-03 17:29:03 +02:00
|
|
|
import { LoginErrorCode } from '@/types/login-error-code.model';
|
|
|
|
import { ICurrentUser } from '@/types/current-user.model';
|
|
|
|
import { CONFIG } from '@/graphql/config';
|
|
|
|
import { IConfig } from '@/types/config.model';
|
2019-01-21 15:08:22 +01:00
|
|
|
|
2019-03-22 13:58:19 +01:00
|
|
|
@Component({
|
|
|
|
apollo: {
|
|
|
|
config: {
|
2019-04-03 17:29:03 +02:00
|
|
|
query: CONFIG,
|
2019-04-01 11:49:54 +02:00
|
|
|
},
|
|
|
|
currentUser: {
|
2019-04-03 17:29:03 +02:00
|
|
|
query: CURRENT_USER_CLIENT,
|
|
|
|
},
|
|
|
|
},
|
2019-03-22 13:58:19 +01:00
|
|
|
})
|
2019-01-21 15:08:22 +01:00
|
|
|
export default class Login extends Vue {
|
2019-03-22 10:57:14 +01:00
|
|
|
@Prop({ type: String, required: false, default: '' }) email!: string;
|
|
|
|
@Prop({ type: String, required: false, default: '' }) password!: string;
|
2019-01-21 15:08:22 +01:00
|
|
|
|
2019-04-01 11:49:54 +02:00
|
|
|
LoginErrorCode = LoginErrorCode;
|
|
|
|
|
|
|
|
errorCode: LoginErrorCode | null = null;
|
2019-03-22 13:58:19 +01:00
|
|
|
config!: IConfig;
|
2019-04-01 11:49:54 +02:00
|
|
|
currentUser!: ICurrentUser;
|
|
|
|
|
2019-01-21 15:08:22 +01:00
|
|
|
credentials = {
|
2019-03-22 10:57:14 +01:00
|
|
|
email: '',
|
|
|
|
password: '',
|
2019-01-21 15:08:22 +01:00
|
|
|
};
|
|
|
|
validationSent = false;
|
2019-04-01 11:49:54 +02:00
|
|
|
|
2019-01-21 15:08:22 +01:00
|
|
|
errors: string[] = [];
|
|
|
|
rules = {
|
|
|
|
required: validateRequiredField,
|
2019-03-22 10:57:14 +01:00
|
|
|
email: validateEmailField,
|
2019-01-21 15:08:22 +01:00
|
|
|
};
|
|
|
|
|
2019-04-01 11:49:54 +02:00
|
|
|
private redirect: string | null = null;
|
2019-01-21 15:08:22 +01:00
|
|
|
|
|
|
|
mounted() {
|
|
|
|
this.credentials.email = this.email;
|
|
|
|
this.credentials.password = this.password;
|
2019-04-01 11:49:54 +02:00
|
|
|
|
2019-04-03 17:29:03 +02:00
|
|
|
const query = this.$route.query;
|
|
|
|
this.errorCode = query['code'] as LoginErrorCode;
|
|
|
|
this.redirect = query['redirect'] as string;
|
2019-01-21 15:08:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async loginAction(e: Event) {
|
|
|
|
e.preventDefault();
|
2019-04-01 11:49:54 +02:00
|
|
|
|
|
|
|
this.errors = [];
|
2019-01-21 15:08:22 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await this.$apollo.mutate<{ login: ILogin }>({
|
|
|
|
mutation: LOGIN,
|
|
|
|
variables: {
|
|
|
|
email: this.credentials.email,
|
2019-03-22 10:57:14 +01:00
|
|
|
password: this.credentials.password,
|
|
|
|
},
|
2019-01-21 15:08:22 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
saveUserData(result.data.login);
|
|
|
|
|
|
|
|
await this.$apollo.mutate({
|
|
|
|
mutation: UPDATE_CURRENT_USER_CLIENT,
|
|
|
|
variables: {
|
|
|
|
id: result.data.login.user.id,
|
2019-03-22 10:57:14 +01:00
|
|
|
email: this.credentials.email,
|
2019-04-01 11:49:54 +02:00
|
|
|
isLoggedIn: true,
|
2019-03-22 10:57:14 +01:00
|
|
|
},
|
2019-01-21 15:08:22 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
onLogin(this.$apollo);
|
|
|
|
|
2019-04-01 11:49:54 +02:00
|
|
|
if (this.redirect) {
|
2019-04-03 17:29:03 +02:00
|
|
|
this.$router.push(this.redirect);
|
2019-04-01 11:49:54 +02:00
|
|
|
} else {
|
|
|
|
this.$router.push({ name: RouteName.HOME });
|
|
|
|
}
|
2019-01-21 15:08:22 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
err.graphQLErrors.forEach(({ message }) => {
|
|
|
|
this.errors.push(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|