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

View File

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

View File

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

View File

@ -103,11 +103,13 @@ export default class Register extends Vue {
mutation: REGISTER_PERSON,
variables: Object.assign({ email: this.email }, this.person),
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) {
identitiesData.identities.push(data.registerPerson);
store.writeQuery({ query: IDENTITIES, data: identitiesData });
if (identitiesData && data) {
identitiesData.identities.push(data.registerPerson);
store.writeQuery({ query: IDENTITIES, data: identitiesData });
}
}
},
});

View File

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

View File

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

View File

@ -17,11 +17,13 @@
</template>
<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 { AUTH_USER_ID } from '@/constants';
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
export default class Validate extends Vue {
@ -36,24 +38,37 @@ export default class Validate extends Vue {
async validateAction() {
try {
const { data } = await this.$apollo.mutate({
const { data } = await this.$apollo.mutate<{ validateUser: ILogin }>({
mutation: VALIDATE_USER,
variables: {
token: this.token,
},
});
this.saveUserData(data);
if (data) {
saveUserData(data.validateUser);
const user = data.validateUser.user;
console.log(user);
if (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' },
const user = data.validateUser.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) {
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) {
console.error(err);
@ -62,11 +77,5 @@ export default class Validate extends Vue {
this.loading = false;
}
}
saveUserData({ validateUser: login }) {
localStorage.setItem(AUTH_USER_ID, login.user.id);
saveTokenData(login);
}
}
</script>

View File

@ -65,5 +65,8 @@ describe('Registration', () => {
cy.location().should((loc) => {
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"
}