UserActions.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. import Reflux from 'reflux';
  15. import Api from '../../components/ApiCaller';
  16. import {servicesUrl, defaultDomain} from '../../config';
  17. import Location from '../../core/Location';
  18. let UserAction = Reflux.createActions({
  19. login: { children: ["success", "failed"] },
  20. loginGoogle: { children: ["success", "failed"] },
  21. loginScope: { children: ["success", "failed"] },
  22. logout: {},
  23. tokenLogin: { children: ["failed"] },
  24. setCurrentUser: {},
  25. switchProject: {},
  26. getScopedProjects: { children: ["completed", "failed"] },
  27. loadProjects: { children: ["completed", "failed"] },
  28. federateToken: { }
  29. })
  30. UserAction.login.listen(userData => {
  31. let auth = {
  32. "auth": {
  33. "identity": {
  34. "methods": [
  35. "password"
  36. ],
  37. "password": {
  38. "user": {
  39. "name": userData.name,
  40. "domain": {
  41. "name": userData.domain ? userData.domain : defaultDomain
  42. },
  43. "password": userData.password
  44. }
  45. }
  46. },
  47. scope: "unscoped"
  48. }
  49. }
  50. Api.setDefaultHeader({ "X-Auth-Token": null })
  51. Api.sendAjaxRequest({
  52. url: servicesUrl.identity,
  53. method: "POST",
  54. data: auth
  55. })
  56. .then((response) => {
  57. UserAction.login.success(response)
  58. Location.push('/replicas')
  59. }, UserAction.login.failed)
  60. })
  61. UserAction.loginScope.listen((token, projectId) => {
  62. let auth = {
  63. auth: {
  64. identity: {
  65. methods: [
  66. "token"
  67. ],
  68. token: {
  69. id: token
  70. }
  71. },
  72. scope: {
  73. project: {
  74. id: projectId
  75. }
  76. }
  77. }
  78. }
  79. Api.setDefaultHeader({ "X-Auth-Token": null })
  80. Api.sendAjaxRequest({
  81. url: servicesUrl.identity,
  82. method: "POST",
  83. data: auth
  84. })
  85. .then((response) => {
  86. UserAction.loginScope.success(response)
  87. }, UserAction.login.failed)
  88. })
  89. UserAction.tokenLogin.listen((token) => {
  90. Api.sendAjaxRequest({
  91. url: servicesUrl.identity,
  92. method: "GET",
  93. headers: { 'X-Subject-Token': token }
  94. })
  95. .then(UserAction.login.success, UserAction.tokenLogin.failed)
  96. .catch((response) => {
  97. UserAction.tokenLogin.failed(response)
  98. });
  99. })
  100. UserAction.getScopedProjects.listen((callback) => {
  101. Api.sendAjaxRequest({
  102. url: servicesUrl.projects,
  103. method: "GET"
  104. })
  105. .then(
  106. (response) => {
  107. if (callback) {
  108. callback(response)
  109. }
  110. UserAction.getScopedProjects.completed(response)
  111. }, UserAction.getScopedProjects.failed)
  112. .catch((response) => {
  113. UserAction.getScopedProjects.failed(response)
  114. });
  115. })
  116. export default UserAction;