aboutsummaryrefslogtreecommitdiff
path: root/src/modules/users.js
blob: 199799b82d0f3e3f2712599db5a4c53a9628e698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import timelineFetcher from '../services/timeline_fetcher/timeline_fetcher.service.js'
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'

const users = {
  state: {
    currentUser: false,
    loggingIn: false
  },
  mutations: {
    setCurrentUser (state, user) {
      state.currentUser = user
    },
    beginLogin (state) {
      state.loggingIn = true
    },
    endLogin (state) {
      state.loggingIn = false
    }
  },
  actions: {
    loginUser (store, userCredentials) {
      const commit = store.commit
      commit('beginLogin')
      return store.rootState.api.backendInteractor.verifyCredentials(userCredentials)
        .then((response) => {
          if (response.ok) {
            response.json()
              .then((user) => {
                user.credentials = userCredentials
                commit('setCurrentUser', user)
              })
              // Start getting fresh tweets.
              .then(() => timelineFetcher.startFetching({store, credentials: userCredentials}))
              // Set our new backend interactor
              .then(() => commit('setBackendInteractor', backendInteractorService(userCredentials)))
          }
          commit('endLogin')
        })
        .catch((error) => {
          console.log(error)
          commit('endLogin')
        })
    }
  }
}

export default users