Merge branch 'feature/login-after-registration' into 'master'

Automatically login after registration

Closes #186

See merge request framasoft/mobilizon!223
This commit is contained in:
Thomas Citharel 2019-10-07 15:55:17 +02:00
commit 36a17ff37c
9 changed files with 54 additions and 25 deletions

View File

@ -94,6 +94,9 @@ import { RouteName } from '@/router';
identities: { identities: {
query: IDENTITIES, query: IDENTITIES,
update: ({ identities }) => identities ? identities.map(identity => new Person(identity)) : [], update: ({ identities }) => identities ? identities.map(identity => new Person(identity)) : [],
skip() {
return this.currentUser.isLoggedIn === false;
},
}, },
config: { config: {
query: CONFIG, query: CONFIG,
@ -114,6 +117,7 @@ export default class NavBar extends Vue {
@Watch('currentActor') @Watch('currentActor')
async initializeListOfIdentities() { async initializeListOfIdentities() {
if (!this.currentUser.isLoggedIn) return;
const { data } = await this.$apollo.query<{ identities: IPerson[] }>({ const { data } = await this.$apollo.query<{ identities: IPerson[] }>({
query: IDENTITIES, query: IDENTITIES,
}); });

View File

@ -18,7 +18,12 @@ mutation ValidateUser($token: String!) {
id, id,
email, email,
defaultActor { defaultActor {
id id,
preferredUsername,
name,
avatar {
url
}
} }
} }
} }

View File

@ -1,4 +1,5 @@
import { IEvent, IParticipant } from '@/types/event.model'; import { IEvent, IParticipant } from '@/types/event.model';
import { IPerson } from '@/types/actor/person.model';
export enum ICurrentUserRole { export enum ICurrentUserRole {
USER = 'USER', USER = 'USER',
@ -12,5 +13,6 @@ export interface ICurrentUser {
isLoggedIn: boolean; isLoggedIn: boolean;
role: ICurrentUserRole; role: ICurrentUserRole;
participations: IParticipant[]; participations: IParticipant[];
defaultActor: IPerson;
drafts: IEvent[]; drafts: IEvent[];
} }

View File

@ -103,12 +103,14 @@ export default class Register extends Vue {
mutation: REGISTER_PERSON, mutation: REGISTER_PERSON,
variables: Object.assign({ email: this.email }, this.person), variables: Object.assign({ email: this.email }, this.person),
update: (store, { data }) => { update: (store, { data }) => {
if (this.userAlreadyActivated) {
const identitiesData = store.readQuery<{ identities: IPerson[] }>({ query: IDENTITIES }); const identitiesData = store.readQuery<{ identities: IPerson[] }>({ query: IDENTITIES });
if (identitiesData && data) { if (identitiesData && data) {
identitiesData.identities.push(data.registerPerson); identitiesData.identities.push(data.registerPerson);
store.writeQuery({ query: IDENTITIES, data: identitiesData }); store.writeQuery({ query: IDENTITIES, data: identitiesData });
} }
}
}, },
}); });
if (data) { if (data) {

View File

@ -157,6 +157,7 @@ export default class Home extends Vue {
const lastWeek = new Date(); const lastWeek = new Date();
lastWeek.setDate(new Date().getDate() - 7); lastWeek.setDate(new Date().getDate() - 7);
if (this.currentUser.isLoggedIn === false) return;
const { data } = await this.$apollo.query({ const { data } = await this.$apollo.query({
query: LOGGED_USER_PARTICIPATIONS, query: LOGGED_USER_PARTICIPATIONS,
variables: { variables: {

View File

@ -36,7 +36,7 @@
</div> </div>
</div> </div>
<div class="column"> <div class="column">
<form @submit="submit"> <form v-on:submit.prevent="submit()">
<b-field <b-field
:label="$t('Email')" :label="$t('Email')"
:type="errors.email ? 'is-danger' : null" :type="errors.email ? 'is-danger' : null"
@ -69,7 +69,7 @@
<b-field grouped> <b-field grouped>
<div class="control"> <div class="control">
<button type="button" class="button is-primary" @click="submit()"> <button class="button is-primary">
{{ $t('Register') }} {{ $t('Register') }}
</button> </button>
</div> </div>

View File

@ -17,11 +17,13 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { VALIDATE_USER } from '@/graphql/user'; import { VALIDATE_USER, UPDATE_CURRENT_USER_CLIENT } from '@/graphql/user';
import { Component, Prop, Vue } from 'vue-property-decorator'; import { Component, Prop, Vue } from 'vue-property-decorator';
import { AUTH_USER_ID } from '@/constants'; import { AUTH_USER_ID } from '@/constants';
import { RouteName } from '@/router'; import { RouteName } from '@/router';
import { saveTokenData } from '@/utils/auth'; import { saveUserData, changeIdentity } from '@/utils/auth';
import { ILogin } from '@/types/login.model';
import { ICurrentUserRole } from '@/types/current-user.model';
@Component @Component
export default class Validate extends Vue { export default class Validate extends Vue {
@ -36,18 +38,30 @@ export default class Validate extends Vue {
async validateAction() { async validateAction() {
try { try {
const { data } = await this.$apollo.mutate({ const { data } = await this.$apollo.mutate<{ validateUser: ILogin }>({
mutation: VALIDATE_USER, mutation: VALIDATE_USER,
variables: { variables: {
token: this.token, token: this.token,
}, },
}); });
this.saveUserData(data); if (data) {
saveUserData(data.validateUser);
const user = data.validateUser.user; const user = data.validateUser.user;
console.log(user);
await this.$apollo.mutate({
mutation: UPDATE_CURRENT_USER_CLIENT,
variables: {
id: user.id,
email: user.email,
isLoggedIn: true,
role: ICurrentUserRole.USER,
},
});
if (user.defaultActor) { if (user.defaultActor) {
await changeIdentity(this.$apollo.provider.defaultClient, user.defaultActor);
await this.$router.push({ name: RouteName.HOME }); await this.$router.push({ name: RouteName.HOME });
} else { // If the user didn't register any profile yet, let's create one for them } else { // If the user didn't register any profile yet, let's create one for them
await this.$router.push({ await this.$router.push({
@ -55,6 +69,7 @@ export default class Validate extends Vue {
params: { email: user.email, userAlreadyActivated: 'true' }, params: { email: user.email, userAlreadyActivated: 'true' },
}); });
} }
}
} catch (err) { } catch (err) {
console.error(err); console.error(err);
this.failed = true; this.failed = true;
@ -62,11 +77,5 @@ export default class Validate extends Vue {
this.loading = false; this.loading = false;
} }
} }
saveUserData({ validateUser: login }) {
localStorage.setItem(AUTH_USER_ID, login.user.id);
saveTokenData(login);
}
} }
</script> </script>

View File

@ -65,5 +65,8 @@ describe('Registration', () => {
cy.location().should((loc) => { cy.location().should((loc) => {
expect(loc.pathname).to.eq('/'); expect(loc.pathname).to.eq('/');
}); });
cy.contains('.navbar-link', 'tester');
cy.contains('article.message.is-info', 'Welcome back tester account');
}); });
}); });

3
tsconfig.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "./js/tsconfig.json"
}