UserStore.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import { observable, action } from 'mobx'
  16. import type { User, Credentials } from '../types/User'
  17. import UserSource from '../sources/UserSource'
  18. import ProjectStore from './ProjectStore'
  19. import NotificationStore from '../stores/NotificationStore'
  20. /**
  21. * This is the authentication / authorization flow:
  22. * 1. Post username and password unscoped login. Set unscoped token in cookies.
  23. * 2. Post unscoped token with project id. Set scoped token and project id in cookies.
  24. * 3. Get token login on subsequent app reloads to retrieve the user info.
  25. *
  26. * After token expiration, the app is redirected to login page.
  27. */
  28. class UserStore {
  29. @observable user: ?User = null
  30. @observable loading: boolean = false
  31. @observable loginFailedResponse: any = null
  32. @action login(creds: Credentials): Promise<void> {
  33. this.loading = true
  34. this.user = null
  35. this.loginFailedResponse = null
  36. return UserSource.login(creds).then(() => {
  37. return this.loginScoped()
  38. }).then((user: User) => {
  39. this.loading = false
  40. NotificationStore.notify('Signed in', 'success')
  41. this.user = user
  42. this.getUserInfo(user)
  43. }).catch((reason) => {
  44. this.loading = false
  45. this.loginFailedResponse = reason
  46. })
  47. }
  48. @action loginScoped(projectId?: string): Promise<User> {
  49. return new Promise((resolve) => {
  50. const sourceLoginScoped = () => {
  51. UserSource.loginScoped(projectId || ProjectStore.projects[0].id).then((user: User) => {
  52. this.user = { ...user, scoped: true }
  53. resolve(user)
  54. })
  55. }
  56. if (ProjectStore.projects && ProjectStore.projects.length) {
  57. sourceLoginScoped()
  58. } else {
  59. ProjectStore.getProjects().then(() => {
  60. sourceLoginScoped()
  61. })
  62. }
  63. })
  64. }
  65. @action getUserInfo(user: User): Promise<void> {
  66. return UserSource.getUserInfo(user).then((userData: User) => {
  67. this.user = { ...this.user, ...userData }
  68. }).catch(reason => {
  69. console.error('Error while getting user data', reason)
  70. NotificationStore.notify('Error while getting user data', 'error')
  71. })
  72. }
  73. @action tokenLogin(): Promise<void> {
  74. this.user = null
  75. this.loading = true
  76. return UserSource.tokenLogin().then(user => {
  77. this.loading = false
  78. this.user = { ...this.user, ...user }
  79. NotificationStore.notify('Signed in', 'success')
  80. this.getUserInfo(user)
  81. }).catch(() => {
  82. this.loading = false
  83. })
  84. }
  85. @action switchProject(projectId: string): Promise<void> {
  86. NotificationStore.notify('Switching projects')
  87. return new Promise((resolve, reject) => {
  88. UserSource.switchProject().then(() => {
  89. return this.loginScoped(projectId)
  90. }).then(() => {
  91. resolve()
  92. }).catch(reason => {
  93. console.error('Error switching projects', reason)
  94. NotificationStore.notify('Error switching projects')
  95. this.logout()
  96. reject()
  97. })
  98. })
  99. }
  100. @action logout(): Promise<void> {
  101. return UserSource.logout().catch(reason => {
  102. console.log('Error logging out', reason)
  103. NotificationStore.notify('Error logging out')
  104. })
  105. }
  106. }
  107. export default new UserStore()