logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

users.js (23874B)


  1. import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
  2. import { windowWidth, windowHeight } from '../services/window_utils/window_utils'
  3. import oauthApi from '../services/new_api/oauth.js'
  4. import { compact, map, each, mergeWith, last, concat, uniq, isArray } from 'lodash'
  5. import { registerPushNotifications, unregisterPushNotifications } from '../services/sw/sw.js'
  6. import { useInterfaceStore } from 'src/stores/interface.js'
  7. // TODO: Unify with mergeOrAdd in statuses.js
  8. export const mergeOrAdd = (arr, obj, item) => {
  9. if (!item) { return false }
  10. const oldItem = obj[item.id]
  11. if (oldItem) {
  12. // We already have this, so only merge the new info.
  13. mergeWith(oldItem, item, mergeArrayLength)
  14. return { item: oldItem, new: false }
  15. } else {
  16. // This is a new item, prepare it
  17. arr.push(item)
  18. obj[item.id] = item
  19. return { item, new: true }
  20. }
  21. }
  22. const mergeArrayLength = (oldValue, newValue) => {
  23. if (isArray(oldValue) && isArray(newValue)) {
  24. oldValue.length = newValue.length
  25. return mergeWith(oldValue, newValue, mergeArrayLength)
  26. }
  27. }
  28. const getNotificationPermission = () => {
  29. const Notification = window.Notification
  30. if (!Notification) return Promise.resolve(null)
  31. if (Notification.permission === 'default') return Notification.requestPermission()
  32. return Promise.resolve(Notification.permission)
  33. }
  34. const blockUser = (store, id) => {
  35. return store.rootState.api.backendInteractor.blockUser({ id })
  36. .then((relationship) => {
  37. store.commit('updateUserRelationship', [relationship])
  38. store.commit('addBlockId', id)
  39. store.commit('removeStatus', { timeline: 'friends', userId: id })
  40. store.commit('removeStatus', { timeline: 'public', userId: id })
  41. store.commit('removeStatus', { timeline: 'publicAndExternal', userId: id })
  42. })
  43. }
  44. const unblockUser = (store, id) => {
  45. return store.rootState.api.backendInteractor.unblockUser({ id })
  46. .then((relationship) => store.commit('updateUserRelationship', [relationship]))
  47. }
  48. const removeUserFromFollowers = (store, id) => {
  49. return store.rootState.api.backendInteractor.removeUserFromFollowers({ id })
  50. .then((relationship) => store.commit('updateUserRelationship', [relationship]))
  51. }
  52. const editUserNote = (store, { id, comment }) => {
  53. return store.rootState.api.backendInteractor.editUserNote({ id, comment })
  54. .then((relationship) => store.commit('updateUserRelationship', [relationship]))
  55. }
  56. const muteUser = (store, args) => {
  57. const id = typeof args === 'object' ? args.id : args
  58. const expiresIn = typeof args === 'object' ? args.expiresIn : 0
  59. const predictedRelationship = store.state.relationships[id] || { id }
  60. predictedRelationship.muting = true
  61. store.commit('updateUserRelationship', [predictedRelationship])
  62. store.commit('addMuteId', id)
  63. return store.rootState.api.backendInteractor.muteUser({ id, expiresIn })
  64. .then((relationship) => {
  65. store.commit('updateUserRelationship', [relationship])
  66. store.commit('addMuteId', id)
  67. })
  68. }
  69. const unmuteUser = (store, id) => {
  70. const predictedRelationship = store.state.relationships[id] || { id }
  71. predictedRelationship.muting = false
  72. store.commit('updateUserRelationship', [predictedRelationship])
  73. return store.rootState.api.backendInteractor.unmuteUser({ id })
  74. .then((relationship) => store.commit('updateUserRelationship', [relationship]))
  75. }
  76. const hideReblogs = (store, userId) => {
  77. return store.rootState.api.backendInteractor.followUser({ id: userId, reblogs: false })
  78. .then((relationship) => {
  79. store.commit('updateUserRelationship', [relationship])
  80. })
  81. }
  82. const showReblogs = (store, userId) => {
  83. return store.rootState.api.backendInteractor.followUser({ id: userId, reblogs: true })
  84. .then((relationship) => store.commit('updateUserRelationship', [relationship]))
  85. }
  86. const muteDomain = (store, domain) => {
  87. return store.rootState.api.backendInteractor.muteDomain({ domain })
  88. .then(() => store.commit('addDomainMute', domain))
  89. }
  90. const unmuteDomain = (store, domain) => {
  91. return store.rootState.api.backendInteractor.unmuteDomain({ domain })
  92. .then(() => store.commit('removeDomainMute', domain))
  93. }
  94. export const mutations = {
  95. tagUser (state, { user: { id }, tag }) {
  96. const user = state.usersObject[id]
  97. const tags = user.tags || []
  98. const newTags = tags.concat([tag])
  99. user.tags = newTags
  100. },
  101. untagUser (state, { user: { id }, tag }) {
  102. const user = state.usersObject[id]
  103. const tags = user.tags || []
  104. const newTags = tags.filter(t => t !== tag)
  105. user.tags = newTags
  106. },
  107. updateRight (state, { user: { id }, right, value }) {
  108. const user = state.usersObject[id]
  109. const newRights = user.rights
  110. newRights[right] = value
  111. user.rights = newRights
  112. },
  113. updateActivationStatus (state, { user: { id }, deactivated }) {
  114. const user = state.usersObject[id]
  115. user.deactivated = deactivated
  116. },
  117. setCurrentUser (state, user) {
  118. state.lastLoginName = user.screen_name
  119. state.currentUser = mergeWith(state.currentUser || {}, user, mergeArrayLength)
  120. },
  121. clearCurrentUser (state) {
  122. state.currentUser = false
  123. state.lastLoginName = false
  124. },
  125. beginLogin (state) {
  126. state.loggingIn = true
  127. },
  128. endLogin (state) {
  129. state.loggingIn = false
  130. },
  131. saveFriendIds (state, { id, friendIds }) {
  132. const user = state.usersObject[id]
  133. user.friendIds = uniq(concat(user.friendIds || [], friendIds))
  134. },
  135. saveFollowerIds (state, { id, followerIds }) {
  136. const user = state.usersObject[id]
  137. user.followerIds = uniq(concat(user.followerIds || [], followerIds))
  138. },
  139. // Because frontend doesn't have a reason to keep these stuff in memory
  140. // outside of viewing someones user profile.
  141. clearFriends (state, userId) {
  142. const user = state.usersObject[userId]
  143. if (user) {
  144. user.friendIds = []
  145. }
  146. },
  147. clearFollowers (state, userId) {
  148. const user = state.usersObject[userId]
  149. if (user) {
  150. user.followerIds = []
  151. }
  152. },
  153. addNewUsers (state, users) {
  154. each(users, (user) => {
  155. if (user.relationship) {
  156. state.relationships[user.relationship.id] = user.relationship
  157. }
  158. const res = mergeOrAdd(state.users, state.usersObject, user)
  159. const item = res.item
  160. if (res.new && item.screen_name && !item.screen_name.includes('@')) {
  161. state.usersByNameObject[item.screen_name.toLowerCase()] = item
  162. }
  163. })
  164. },
  165. updateUserRelationship (state, relationships) {
  166. relationships.forEach((relationship) => {
  167. state.relationships[relationship.id] = relationship
  168. })
  169. },
  170. updateUserInLists (state, { id, inLists }) {
  171. state.usersObject[id].inLists = inLists
  172. },
  173. saveBlockIds (state, blockIds) {
  174. state.currentUser.blockIds = blockIds
  175. },
  176. addBlockId (state, blockId) {
  177. if (state.currentUser.blockIds.indexOf(blockId) === -1) {
  178. state.currentUser.blockIds.push(blockId)
  179. }
  180. },
  181. setBlockIdsMaxId (state, blockIdsMaxId) {
  182. state.currentUser.blockIdsMaxId = blockIdsMaxId
  183. },
  184. saveMuteIds (state, muteIds) {
  185. state.currentUser.muteIds = muteIds
  186. },
  187. setMuteIdsMaxId (state, muteIdsMaxId) {
  188. state.currentUser.muteIdsMaxId = muteIdsMaxId
  189. },
  190. addMuteId (state, muteId) {
  191. if (state.currentUser.muteIds.indexOf(muteId) === -1) {
  192. state.currentUser.muteIds.push(muteId)
  193. }
  194. },
  195. saveDomainMutes (state, domainMutes) {
  196. state.currentUser.domainMutes = domainMutes
  197. },
  198. addDomainMute (state, domain) {
  199. if (state.currentUser.domainMutes.indexOf(domain) === -1) {
  200. state.currentUser.domainMutes.push(domain)
  201. }
  202. },
  203. removeDomainMute (state, domain) {
  204. const index = state.currentUser.domainMutes.indexOf(domain)
  205. if (index !== -1) {
  206. state.currentUser.domainMutes.splice(index, 1)
  207. }
  208. },
  209. setPinnedToUser (state, status) {
  210. const user = state.usersObject[status.user.id]
  211. user.pinnedStatusIds = user.pinnedStatusIds || []
  212. const index = user.pinnedStatusIds.indexOf(status.id)
  213. if (status.pinned && index === -1) {
  214. user.pinnedStatusIds.push(status.id)
  215. } else if (!status.pinned && index !== -1) {
  216. user.pinnedStatusIds.splice(index, 1)
  217. }
  218. },
  219. setUserForStatus (state, status) {
  220. status.user = state.usersObject[status.user.id]
  221. },
  222. setUserForNotification (state, notification) {
  223. if (notification.type !== 'follow') {
  224. notification.action.user = state.usersObject[notification.action.user.id]
  225. }
  226. notification.from_profile = state.usersObject[notification.from_profile.id]
  227. },
  228. setColor (state, { user: { id }, highlighted }) {
  229. const user = state.usersObject[id]
  230. user.highlight = highlighted
  231. },
  232. signUpPending (state) {
  233. state.signUpPending = true
  234. state.signUpErrors = []
  235. state.signUpNotice = {}
  236. },
  237. signUpSuccess (state) {
  238. state.signUpPending = false
  239. },
  240. signUpFailure (state, errors) {
  241. state.signUpPending = false
  242. state.signUpErrors = errors
  243. state.signUpNotice = {}
  244. },
  245. signUpNotice (state, notice) {
  246. state.signUpPending = false
  247. state.signUpErrors = []
  248. state.signUpNotice = notice
  249. }
  250. }
  251. export const getters = {
  252. findUser: state => query => {
  253. return state.usersObject[query]
  254. },
  255. findUserByName: state => query => {
  256. return state.usersByNameObject[query.toLowerCase()]
  257. },
  258. findUserByUrl: state => query => {
  259. return state.users
  260. .find(u => u.statusnet_profile_url &&
  261. u.statusnet_profile_url.toLowerCase() === query.toLowerCase())
  262. },
  263. relationship: state => id => {
  264. const rel = id && state.relationships[id]
  265. return rel || { id, loading: true }
  266. }
  267. }
  268. export const defaultState = {
  269. loggingIn: false,
  270. lastLoginName: false,
  271. currentUser: false,
  272. users: [],
  273. usersObject: {},
  274. usersByNameObject: {},
  275. signUpPending: false,
  276. signUpErrors: [],
  277. signUpNotice: {},
  278. relationships: {}
  279. }
  280. const users = {
  281. state: defaultState,
  282. mutations,
  283. getters,
  284. actions: {
  285. fetchUserIfMissing (store, id) {
  286. if (!store.getters.findUser(id)) {
  287. store.dispatch('fetchUser', id)
  288. }
  289. },
  290. fetchUser (store, id) {
  291. return store.rootState.api.backendInteractor.fetchUser({ id })
  292. .then((user) => {
  293. store.commit('addNewUsers', [user])
  294. return user
  295. })
  296. },
  297. fetchUserByName (store, name) {
  298. return store.rootState.api.backendInteractor.fetchUserByName({ name })
  299. .then((user) => {
  300. store.commit('addNewUsers', [user])
  301. return user
  302. })
  303. },
  304. fetchUserRelationship (store, id) {
  305. if (store.state.currentUser) {
  306. store.rootState.api.backendInteractor.fetchUserRelationship({ id })
  307. .then((relationships) => store.commit('updateUserRelationship', relationships))
  308. }
  309. },
  310. fetchUserInLists (store, id) {
  311. if (store.state.currentUser) {
  312. store.rootState.api.backendInteractor.fetchUserInLists({ id })
  313. .then((inLists) => store.commit('updateUserInLists', { id, inLists }))
  314. }
  315. },
  316. fetchBlocks (store, args) {
  317. const { reset } = args || {}
  318. const maxId = store.state.currentUser.blockIdsMaxId
  319. return store.rootState.api.backendInteractor.fetchBlocks({ maxId })
  320. .then((blocks) => {
  321. if (reset) {
  322. store.commit('saveBlockIds', map(blocks, 'id'))
  323. } else {
  324. map(blocks, 'id').map(id => store.commit('addBlockId', id))
  325. }
  326. if (blocks.length) {
  327. store.commit('setBlockIdsMaxId', last(blocks).id)
  328. }
  329. store.commit('addNewUsers', blocks)
  330. return blocks
  331. })
  332. },
  333. blockUser (store, id) {
  334. return blockUser(store, id)
  335. },
  336. unblockUser (store, id) {
  337. return unblockUser(store, id)
  338. },
  339. removeUserFromFollowers (store, id) {
  340. return removeUserFromFollowers(store, id)
  341. },
  342. blockUsers (store, ids = []) {
  343. return Promise.all(ids.map(id => blockUser(store, id)))
  344. },
  345. unblockUsers (store, ids = []) {
  346. return Promise.all(ids.map(id => unblockUser(store, id)))
  347. },
  348. editUserNote (store, args) {
  349. return editUserNote(store, args)
  350. },
  351. fetchMutes (store, args) {
  352. const { reset } = args || {}
  353. const maxId = store.state.currentUser.muteIdsMaxId
  354. return store.rootState.api.backendInteractor.fetchMutes({ maxId })
  355. .then((mutes) => {
  356. if (reset) {
  357. store.commit('saveMuteIds', map(mutes, 'id'))
  358. } else {
  359. map(mutes, 'id').map(id => store.commit('addMuteId', id))
  360. }
  361. if (mutes.length) {
  362. store.commit('setMuteIdsMaxId', last(mutes).id)
  363. }
  364. store.commit('addNewUsers', mutes)
  365. return mutes
  366. })
  367. },
  368. muteUser (store, id) {
  369. return muteUser(store, id)
  370. },
  371. unmuteUser (store, id) {
  372. return unmuteUser(store, id)
  373. },
  374. hideReblogs (store, id) {
  375. return hideReblogs(store, id)
  376. },
  377. showReblogs (store, id) {
  378. return showReblogs(store, id)
  379. },
  380. muteUsers (store, ids = []) {
  381. return Promise.all(ids.map(id => muteUser(store, id)))
  382. },
  383. unmuteUsers (store, ids = []) {
  384. return Promise.all(ids.map(id => unmuteUser(store, id)))
  385. },
  386. fetchDomainMutes (store) {
  387. return store.rootState.api.backendInteractor.fetchDomainMutes()
  388. .then((domainMutes) => {
  389. store.commit('saveDomainMutes', domainMutes)
  390. return domainMutes
  391. })
  392. },
  393. muteDomain (store, domain) {
  394. return muteDomain(store, domain)
  395. },
  396. unmuteDomain (store, domain) {
  397. return unmuteDomain(store, domain)
  398. },
  399. muteDomains (store, domains = []) {
  400. return Promise.all(domains.map(domain => muteDomain(store, domain)))
  401. },
  402. unmuteDomains (store, domain = []) {
  403. return Promise.all(domain.map(domain => unmuteDomain(store, domain)))
  404. },
  405. fetchFriends ({ rootState, commit }, id) {
  406. const user = rootState.users.usersObject[id]
  407. const maxId = last(user.friendIds)
  408. return rootState.api.backendInteractor.fetchFriends({ id, maxId })
  409. .then((friends) => {
  410. commit('addNewUsers', friends)
  411. commit('saveFriendIds', { id, friendIds: map(friends, 'id') })
  412. return friends
  413. })
  414. },
  415. fetchFollowers ({ rootState, commit }, id) {
  416. const user = rootState.users.usersObject[id]
  417. const maxId = last(user.followerIds)
  418. return rootState.api.backendInteractor.fetchFollowers({ id, maxId })
  419. .then((followers) => {
  420. commit('addNewUsers', followers)
  421. commit('saveFollowerIds', { id, followerIds: map(followers, 'id') })
  422. return followers
  423. })
  424. },
  425. clearFriends ({ commit }, userId) {
  426. commit('clearFriends', userId)
  427. },
  428. clearFollowers ({ commit }, userId) {
  429. commit('clearFollowers', userId)
  430. },
  431. subscribeUser ({ rootState, commit }, id) {
  432. return rootState.api.backendInteractor.followUser({ id, notify: true })
  433. .then((relationship) => commit('updateUserRelationship', [relationship]))
  434. },
  435. unsubscribeUser ({ rootState, commit }, id) {
  436. return rootState.api.backendInteractor.followUser({ id, notify: false })
  437. .then((relationship) => commit('updateUserRelationship', [relationship]))
  438. },
  439. toggleActivationStatus ({ rootState, commit }, { user }) {
  440. const api = user.deactivated ? rootState.api.backendInteractor.activateUser : rootState.api.backendInteractor.deactivateUser
  441. api({ user })
  442. .then((user) => { const deactivated = !user.is_active; commit('updateActivationStatus', { user, deactivated }) })
  443. },
  444. registerPushNotifications (store) {
  445. const token = store.state.currentUser.credentials
  446. const vapidPublicKey = store.rootState.instance.vapidPublicKey
  447. const isEnabled = store.rootState.config.webPushNotifications
  448. const notificationVisibility = store.rootState.config.notificationVisibility
  449. registerPushNotifications(isEnabled, vapidPublicKey, token, notificationVisibility)
  450. },
  451. unregisterPushNotifications (store) {
  452. const token = store.state.currentUser.credentials
  453. unregisterPushNotifications(token)
  454. },
  455. addNewUsers ({ commit }, users) {
  456. commit('addNewUsers', users)
  457. },
  458. addNewStatuses (store, { statuses }) {
  459. const users = map(statuses, 'user')
  460. const retweetedUsers = compact(map(statuses, 'retweeted_status.user'))
  461. store.commit('addNewUsers', users)
  462. store.commit('addNewUsers', retweetedUsers)
  463. each(statuses, (status) => {
  464. // Reconnect users to statuses
  465. store.commit('setUserForStatus', status)
  466. // Set pinned statuses to user
  467. store.commit('setPinnedToUser', status)
  468. })
  469. each(compact(map(statuses, 'retweeted_status')), (status) => {
  470. // Reconnect users to retweets
  471. store.commit('setUserForStatus', status)
  472. // Set pinned retweets to user
  473. store.commit('setPinnedToUser', status)
  474. })
  475. },
  476. addNewNotifications (store, { notifications }) {
  477. const users = map(notifications, 'from_profile')
  478. const targetUsers = map(notifications, 'target').filter(_ => _)
  479. const notificationIds = notifications.map(_ => _.id)
  480. store.commit('addNewUsers', users)
  481. store.commit('addNewUsers', targetUsers)
  482. const notificationsObject = store.rootState.notifications.idStore
  483. const relevantNotifications = Object.entries(notificationsObject)
  484. .filter(([k]) => notificationIds.includes(k))
  485. .map(([, val]) => val)
  486. // Reconnect users to notifications
  487. each(relevantNotifications, (notification) => {
  488. store.commit('setUserForNotification', notification)
  489. })
  490. },
  491. searchUsers ({ rootState, commit }, { query }) {
  492. return rootState.api.backendInteractor.searchUsers({ query })
  493. .then((users) => {
  494. commit('addNewUsers', users)
  495. return users
  496. })
  497. },
  498. async signUp (store, userInfo) {
  499. store.commit('signUpPending')
  500. const rootState = store.rootState
  501. try {
  502. const data = await rootState.api.backendInteractor.register(
  503. { params: { ...userInfo } }
  504. )
  505. if (data.access_token) {
  506. store.commit('signUpSuccess')
  507. store.commit('setToken', data.access_token)
  508. store.dispatch('loginUser', data.access_token)
  509. return 'ok'
  510. } else { // Request succeeded, but user cannot login yet.
  511. store.commit('signUpNotice', data)
  512. return 'request_sent'
  513. }
  514. } catch (e) {
  515. const errors = e.message
  516. store.commit('signUpFailure', errors)
  517. throw e
  518. }
  519. },
  520. async getCaptcha (store) {
  521. return store.rootState.api.backendInteractor.getCaptcha()
  522. },
  523. logout (store) {
  524. const { oauth, instance } = store.rootState
  525. const data = {
  526. ...oauth,
  527. commit: store.commit,
  528. instance: instance.server
  529. }
  530. return oauthApi.getOrCreateApp(data)
  531. .then((app) => {
  532. const params = {
  533. app,
  534. instance: data.instance,
  535. token: oauth.userToken
  536. }
  537. return oauthApi.revokeToken(params)
  538. })
  539. .then(() => {
  540. store.commit('clearCurrentUser')
  541. store.dispatch('disconnectFromSocket')
  542. store.commit('clearToken')
  543. store.dispatch('stopFetchingTimeline', 'friends')
  544. store.commit('setBackendInteractor', backendInteractorService(store.getters.getToken()))
  545. store.dispatch('stopFetchingNotifications')
  546. store.dispatch('stopFetchingLists')
  547. store.dispatch('stopFetchingBookmarkFolders')
  548. store.dispatch('stopFetchingFollowRequests')
  549. store.commit('clearNotifications')
  550. store.commit('resetStatuses')
  551. store.dispatch('resetChats')
  552. useInterfaceStore().setLastTimeline('public-timeline')
  553. useInterfaceStore().setLayoutWidth(windowWidth())
  554. useInterfaceStore().setLayoutHeight(windowHeight())
  555. store.commit('clearServerSideStorage')
  556. })
  557. },
  558. loginUser (store, accessToken) {
  559. return new Promise((resolve, reject) => {
  560. const commit = store.commit
  561. const dispatch = store.dispatch
  562. commit('beginLogin')
  563. store.rootState.api.backendInteractor.verifyCredentials(accessToken)
  564. .then((data) => {
  565. if (!data.error) {
  566. const user = data
  567. // user.credentials = userCredentials
  568. user.credentials = accessToken
  569. user.blockIds = []
  570. user.muteIds = []
  571. user.domainMutes = []
  572. commit('setCurrentUser', user)
  573. commit('setServerSideStorage', user)
  574. commit('addNewUsers', [user])
  575. dispatch('fetchEmoji')
  576. getNotificationPermission()
  577. .then(permission => useInterfaceStore().setNotificationPermission(permission))
  578. // Set our new backend interactor
  579. commit('setBackendInteractor', backendInteractorService(accessToken))
  580. dispatch('pushServerSideStorage')
  581. if (user.token) {
  582. dispatch('setWsToken', user.token)
  583. // Initialize the shout socket.
  584. dispatch('initializeSocket')
  585. }
  586. const startPolling = () => {
  587. // Start getting fresh posts.
  588. dispatch('startFetchingTimeline', { timeline: 'friends' })
  589. // Start fetching notifications
  590. dispatch('startFetchingNotifications')
  591. // Start fetching chats
  592. dispatch('startFetchingChats')
  593. }
  594. dispatch('startFetchingLists')
  595. dispatch('startFetchingBookmarkFolders')
  596. if (user.locked) {
  597. dispatch('startFetchingFollowRequests')
  598. }
  599. if (store.getters.mergedConfig.useStreamingApi) {
  600. dispatch('fetchTimeline', { timeline: 'friends', since: null })
  601. dispatch('fetchNotifications', { since: null })
  602. dispatch('enableMastoSockets', true).catch((error) => {
  603. console.error('Failed initializing MastoAPI Streaming socket', error)
  604. }).then(() => {
  605. dispatch('fetchChats', { latest: true })
  606. setTimeout(() => dispatch('setNotificationsSilence', false), 10000)
  607. })
  608. } else {
  609. startPolling()
  610. }
  611. // Get user mutes
  612. dispatch('fetchMutes')
  613. useInterfaceStore().setLayoutWidth(windowWidth())
  614. useInterfaceStore().setLayoutHeight(windowHeight())
  615. // Fetch our friends
  616. store.rootState.api.backendInteractor.fetchFriends({ id: user.id })
  617. .then((friends) => commit('addNewUsers', friends))
  618. } else {
  619. const response = data.error
  620. // Authentication failed
  621. commit('endLogin')
  622. // remove authentication token on client/authentication errors
  623. if ([400, 401, 403, 422].includes(response.status)) {
  624. commit('clearToken')
  625. }
  626. if (response.status === 401) {
  627. reject(new Error('Wrong username or password'))
  628. } else {
  629. reject(new Error('An error occurred, please try again'))
  630. }
  631. }
  632. commit('endLogin')
  633. resolve()
  634. })
  635. .catch((error) => {
  636. console.error(error)
  637. commit('endLogin')
  638. reject(new Error('Failed to connect to server, try again'))
  639. })
  640. })
  641. }
  642. }
  643. }
  644. export default users