Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- loginUser (store, accessToken) {
- return new Promise((resolve, reject) => {
- const commit = store.commit
- commit('beginLogin')
- store.rootState.api.backendInteractor.verifyCredentials(accessToken)
- .then((data) => {
- if (!data.error) {
- const user = data
- // user.credentials = userCredentials
- user.credentials = accessToken
- user.blockIds = []
- user.muteIds = []
- commit('setCurrentUser', user)
- commit('addNewUsers', [user])
- getNotificationPermission()
- .then(permission => commit('setNotificationPermission', permission))
- // Set our new backend interactor
- commit('setBackendInteractor', backendInteractorService(accessToken))
- if (user.token) {
- store.dispatch('setWsToken', user.token)
- // Initialize the chat socket.
- store.dispatch('initializeSocket')
- }
- // Start getting fresh posts.
- store.dispatch('startFetchingTimeline', { timeline: 'friends' })
- // Start fetching notifications
- store.dispatch('startFetchingNotifications')
- // Get user mutes
- store.dispatch('fetchMutes')
- // Fetch our friends
- store.rootState.api.backendInteractor.fetchFriends({ id: user.id })
- .then((friends) => commit('addNewUsers', friends))
- } else {
- const response = data.error
- // Authentication failed
- commit('endLogin')
- if (response.status === 401) {
- reject('Wrong username or password')
- } else {
- reject('An error occurred, please try again')
- }
- }
- commit('endLogin')
- resolve()
- })
- .catch((error) => {
- console.log(error)
- commit('endLogin')
- reject('Failed to connect to server, try again')
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement