Automatically login after registration

Closes #186

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2019-10-07 13:47:46 +02:00
parent 2fc24023cc
commit c58c9e5f97
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
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,11 +103,13 @@ 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 }) => {
const identitiesData = store.readQuery<{ identities: IPerson[] }>({ query: IDENTITIES }); if (this.userAlreadyActivated) {
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 });
}
} }
}, },
}); });

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,24 +38,37 @@ 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);
if (user.defaultActor) { await this.$apollo.mutate({
await this.$router.push({ name: RouteName.HOME }); mutation: UPDATE_CURRENT_USER_CLIENT,
} else { // If the user didn't register any profile yet, let's create one for them variables: {
await this.$router.push({ id: user.id,
name: RouteName.REGISTER_PROFILE, email: user.email,
params: { email: user.email, userAlreadyActivated: 'true' }, isLoggedIn: true,
role: ICurrentUserRole.USER,
},
}); });
if (user.defaultActor) {
await changeIdentity(this.$apollo.provider.defaultClient, user.defaultActor);
await this.$router.push({ name: RouteName.HOME });
} else { // If the user didn't register any profile yet, let's create one for them
await this.$router.push({
name: RouteName.REGISTER_PROFILE,
params: { email: user.email, userAlreadyActivated: 'true' },
});
}
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
@ -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"
}