2019-01-21 15:08:22 +01:00
|
|
|
<template>
|
2019-04-03 17:29:03 +02:00
|
|
|
<div class="container">
|
2019-04-01 11:49:54 +02:00
|
|
|
<b-message v-if="errorCode === LoginErrorCode.NEED_TO_LOGIN" title="Info" type="is-info">
|
2019-09-12 11:34:01 +02:00
|
|
|
{{ $t('You need to login.') }}
|
2019-04-01 11:49:54 +02:00
|
|
|
</b-message>
|
|
|
|
|
|
|
|
<section v-if="!currentUser.isLoggedIn">
|
2019-01-21 15:08:22 +01:00
|
|
|
<div class="columns is-mobile is-centered">
|
2019-10-10 14:20:09 +02:00
|
|
|
<div class="column is-half-desktop">
|
|
|
|
<h1 class="title">
|
|
|
|
{{ $t('Welcome back!') }}
|
|
|
|
</h1>
|
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-09-12 11:34:01 +02:00
|
|
|
<b-field :label="$t('Email')">
|
2019-01-21 15:08:22 +01:00
|
|
|
<b-input aria-required="true" required type="email" v-model="credentials.email"/>
|
|
|
|
</b-field>
|
|
|
|
|
2019-09-12 11:34:01 +02:00
|
|
|
<b-field :label="$t('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>
|
|
|
|
|
2019-10-10 14:20:09 +02:00
|
|
|
<p class="control has-text-centered">
|
2019-01-21 15:08:22 +01:00
|
|
|
<button class="button is-primary is-large">
|
2019-09-12 11:34:01 +02:00
|
|
|
{{ $t('Login') }}
|
2019-01-21 15:08:22 +01:00
|
|
|
</button>
|
2019-10-10 14:20:09 +02:00
|
|
|
</p>
|
|
|
|
<p class="control">
|
2019-01-21 15:08:22 +01:00
|
|
|
<router-link
|
|
|
|
class="button is-text"
|
2019-10-03 12:32:20 +02:00
|
|
|
:to="{ name: RouteName.SEND_PASSWORD_RESET, params: { email: credentials.email }}"
|
2019-01-21 15:08:22 +01:00
|
|
|
>
|
2019-09-12 11:34:01 +02:00
|
|
|
{{ $t('Forgot your password ?') }}
|
2019-01-21 15:08:22 +01:00
|
|
|
</router-link>
|
2019-10-10 14:20:09 +02:00
|
|
|
</p>
|
|
|
|
<p class="control" v-if="config && config.registrationsOpen">
|
2019-01-21 15:08:22 +01:00
|
|
|
<router-link
|
|
|
|
class="button is-text"
|
2019-10-03 12:32:20 +02:00
|
|
|
:to="{ name: RouteName.REGISTER, params: { default_email: credentials.email, default_password: credentials.password }}"
|
2019-01-21 15:08:22 +01:00
|
|
|
>
|
2019-09-12 11:34:01 +02:00
|
|
|
{{ $t('Register') }}
|
2019-01-21 15:08:22 +01:00
|
|
|
</router-link>
|
2019-10-10 14:20:09 +02:00
|
|
|
</p>
|
2019-01-21 15:08:22 +01:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</section>
|
2019-04-01 11:49:54 +02:00
|
|
|
|
|
|
|
<b-message v-else title="Error" type="is-error">
|
2019-09-12 11:34:01 +02:00
|
|
|
{{ $t('You are already logged-in.') }}
|
2019-04-01 11:49:54 +02:00
|
|
|
</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';
|
2019-10-12 13:16:36 +02:00
|
|
|
import { initializeCurrentActor, NoIdentitiesException, saveUserData } from '@/utils/auth';
|
2019-03-22 10:57:14 +01:00
|
|
|
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-10-10 16:47:38 +02:00
|
|
|
},
|
|
|
|
metaInfo() {
|
|
|
|
return {
|
|
|
|
// if no subcomponents specify a metaInfo.title, this title will be used
|
|
|
|
title: this.$t('Login on Mobilizon!') as string,
|
|
|
|
// all titles will be injected into this template
|
|
|
|
titleTemplate: '%s | Mobilizon',
|
|
|
|
};
|
2019-04-03 17:29:03 +02:00
|
|
|
},
|
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-10-03 12:32:20 +02:00
|
|
|
RouteName = RouteName;
|
|
|
|
|
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 {
|
2019-09-02 18:52:23 +02:00
|
|
|
const { data } = await this.$apollo.mutate<{ login: ILogin }>({
|
2019-01-21 15:08:22 +01:00
|
|
|
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
|
|
|
});
|
2019-09-02 18:52:23 +02:00
|
|
|
if (data == null) {
|
|
|
|
throw new Error('Data is undefined');
|
|
|
|
}
|
2019-01-21 15:08:22 +01:00
|
|
|
|
2019-09-02 18:52:23 +02:00
|
|
|
saveUserData(data.login);
|
2019-01-21 15:08:22 +01:00
|
|
|
|
|
|
|
await this.$apollo.mutate({
|
|
|
|
mutation: UPDATE_CURRENT_USER_CLIENT,
|
|
|
|
variables: {
|
2019-09-02 18:52:23 +02:00
|
|
|
id: 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-09-09 09:31:08 +02:00
|
|
|
role: data.login.user.role,
|
2019-03-22 10:57:14 +01:00
|
|
|
},
|
2019-01-21 15:08:22 +01:00
|
|
|
});
|
2019-10-12 13:16:36 +02:00
|
|
|
try {
|
|
|
|
await initializeCurrentActor(this.$apollo.provider.defaultClient);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NoIdentitiesException) {
|
|
|
|
return await this.$router.push({
|
|
|
|
name: RouteName.REGISTER_PROFILE,
|
|
|
|
params: { email: this.currentUser.email, userAlreadyActivated: 'true' },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 15:08:22 +01:00
|
|
|
|
|
|
|
onLogin(this.$apollo);
|
|
|
|
|
2019-04-01 11:49:54 +02:00
|
|
|
if (this.redirect) {
|
2019-08-21 11:25:09 +02:00
|
|
|
await this.$router.push(this.redirect);
|
2019-04-01 11:49:54 +02:00
|
|
|
} else {
|
2019-08-21 11:25:09 +02:00
|
|
|
await this.$router.push({ name: RouteName.HOME });
|
2019-04-01 11:49:54 +02:00
|
|
|
}
|
2019-01-21 15:08:22 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
err.graphQLErrors.forEach(({ message }) => {
|
|
|
|
this.errors.push(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
2019-10-10 14:20:09 +02:00
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.container .columns {
|
|
|
|
margin: 1rem auto 3rem;
|
|
|
|
}
|
|
|
|
</style>
|