From ea9f896ef8dce4e0fb6d99256815612d6cee9f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Wed, 27 Feb 2019 11:06:02 +0100 Subject: [PATCH] Refactor store to use the new backend API --- store/auth.js | 31 +++++++++++++------------------ store/auth.test.js | 39 +++++---------------------------------- 2 files changed, 18 insertions(+), 52 deletions(-) diff --git a/store/auth.js b/store/auth.js index 91baad151..4785ff0c0 100644 --- a/store/auth.js +++ b/store/auth.js @@ -67,6 +67,7 @@ export const actions = { } return getters.isLoggedIn }, + async fetchCurrentUser({ commit, dispatch }) { const client = this.app.apolloProvider.defaultClient const { @@ -80,15 +81,17 @@ export const actions = { email avatar role + about + locationName } }`) }) if (!currentUser) return dispatch('logout') - const { token, ...user } = currentUser - commit('SET_USER', user) - return user + commit('SET_USER', currentUser) + return currentUser }, - async login({ commit }, { email, password }) { + + async login({ commit, dispatch }, { email, password }) { commit('SET_PENDING', true) try { const client = this.app.apolloProvider.defaultClient @@ -97,35 +100,27 @@ export const actions = { } = await client.mutate({ mutation: gql(` mutation($email: String!, $password: String!) { - login(email: $email, password: $password) { - id - name - slug - email - avatar - role - token - } + login(email: $email, password: $password) } `), variables: { email, password } }) - const { token, ...user } = login - - await this.app.$apolloHelpers.onLogin(token) - commit('SET_TOKEN', token) - commit('SET_USER', user) + await this.app.$apolloHelpers.onLogin(login) + commit('SET_TOKEN', login) + await dispatch('fetchCurrentUser') } catch (err) { throw new Error(err) } finally { commit('SET_PENDING', false) } }, + async logout({ commit }) { commit('SET_USER', null) commit('SET_TOKEN', null) return this.app.$apolloHelpers.onLogout() }, + register( { dispatch, commit }, { email, password, inviteCode, invitedByUserId } diff --git a/store/auth.test.js b/store/auth.test.js index 290938b7e..6758e3427 100644 --- a/store/auth.test.js +++ b/store/auth.test.js @@ -14,22 +14,8 @@ const currentUser = { avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/mutu_krish/128.jpg', role: 'user' } -const successfulLoginResponse = { - data: { - login: { - ...currentUser, - token - } - } -} -const successfulCurrentUserResponse = { - data: { - currentUser: { - ...currentUser, - token - } - } -} +const successfulLoginResponse = { data: { login: token } } +const successfulCurrentUserResponse = { data: { currentUser } } const incorrectPasswordResponse = { data: { @@ -168,7 +154,7 @@ describe('actions', () => { } action = actions.login.bind(module) await action( - { commit }, + { commit, dispatch }, { email: 'user@example.org', password: '1234' } ) }) @@ -183,23 +169,8 @@ describe('actions', () => { ) }) - it('saves user data without token', () => { - expect(commit.mock.calls).toEqual( - expect.arrayContaining([ - [ - 'SET_USER', - { - id: 'u3', - name: 'Jenny Rostock', - slug: 'jenny-rostock', - email: 'user@example.org', - avatar: - 'https://s3.amazonaws.com/uifaces/faces/twitter/mutu_krish/128.jpg', - role: 'user' - } - ] - ]) - ) + it('fetches the user', () => { + expect(dispatch.mock.calls).toEqual( [['fetchCurrentUser']]) }) it('saves pending flags in order', () => {