user.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { REST } from "vuejs/api/rest-axios";
  2. // import { JSONAPI } from 'vuejs/api/json-axios';
  3. import { MA } from "vuejs/api/ma-axios";
  4. import qs from "querystring";
  5. export default {
  6. namespaced: true,
  7. // initial state
  8. state: {
  9. uid: null,
  10. // username: '',
  11. mail: "",
  12. token: null,
  13. csrftoken: null,
  14. logout_token: null,
  15. isloggedin: false,
  16. isAdmin: false,
  17. isAdherent: false,
  18. canSearch: false,
  19. roles: [],
  20. flags: false
  21. },
  22. // getters
  23. getters: {},
  24. // mutations
  25. mutations: {
  26. SetCsrftoken(state, token) {
  27. state.csrftoken = token;
  28. },
  29. setToken(state, data) {
  30. state.uid = data.current_user.uid;
  31. // state.username = data.username;
  32. state.mail = data.current_user.mail;
  33. state.token = data.csrf_token;
  34. state.isloggedin = true;
  35. state.logout_token = data.logout_token;
  36. },
  37. setUid(state, uid) {
  38. state.uid = uid;
  39. state.isloggedin = true;
  40. },
  41. setUser(state, data) {
  42. state.mail = data.mail[0].value;
  43. state.uuid = data.uuid[0].value;
  44. },
  45. setRoles(state, roles) {
  46. console.log("User setRoles", roles);
  47. state.roles = [];
  48. for (let i = 0; i < roles.length; i++) {
  49. state.roles.push(roles[i].target_id);
  50. }
  51. // check if admin
  52. if (
  53. state.roles.indexOf("admin") !== -1 ||
  54. state.roles.indexOf("root") !== -1
  55. ) {
  56. // console.log('is admin');
  57. state.isAdmin = true;
  58. }
  59. // check if has access to search
  60. if (state.roles.indexOf("adherent") !== -1) {
  61. // console.log('is admin');
  62. state.canSearch = true;
  63. state.isAdherent = true;
  64. }
  65. },
  66. setLoggedOut(state) {
  67. console.log("setLoggedOut state", state);
  68. state.uid = null;
  69. state.mail = "";
  70. state.token = null;
  71. state.isloggedin = false;
  72. state.logout_token = null;
  73. if (state.isAdmin) {
  74. // TODO: what if on a page where login is needed (as commerce checkout and cart)
  75. window.location.reload(true);
  76. }
  77. state.asAdmin = false;
  78. state.canSearch = false;
  79. },
  80. setFlags(state, flags) {
  81. console.log("User setFlags", flags);
  82. state.flags = flags;
  83. }
  84. },
  85. // actions
  86. actions: {
  87. userRegister({ dispatch, commit, state }, credentials) {
  88. return new Promise((resolve, reject) => {
  89. REST.get("/session/token").then(({ token }) => {
  90. commit("SetCsrftoken", token);
  91. REST.post("/user/register?_format=json", credentials, {
  92. "X-CSRF-Token": state.csrftoken
  93. })
  94. .then(({ data }) => {
  95. console.log("user REST registered", data);
  96. dispatch("userLogin", credentials).then(() => {
  97. resolve();
  98. });
  99. })
  100. .catch(error => {
  101. console.warn("Issue with register", error);
  102. Promise.reject(error);
  103. });
  104. });
  105. });
  106. },
  107. userLogin({ dispatch, commit, state }, credentials) {
  108. return new Promise((resolve, reject) => {
  109. dispatch("getToken", credentials).then(() => {
  110. dispatch("getUser").then(userdata => {
  111. console.log("User Loggedin");
  112. if (state.isAdmin) {
  113. window.location.reload(true);
  114. }
  115. resolve();
  116. });
  117. });
  118. });
  119. },
  120. getToken({ dispatch, commit, state }, credentials) {
  121. return REST.post("/user/login?_format=json", credentials)
  122. .then(({ data }) => {
  123. console.log("user REST getToken data", data);
  124. commit("setToken", data);
  125. })
  126. .catch(error => {
  127. console.warn("Issue with getToken", error);
  128. Promise.reject(error);
  129. });
  130. },
  131. getUser({ dispatch, commit, state }) {
  132. const params = {
  133. token: state.token
  134. };
  135. return REST.get(`/user/${state.uid}?_format=json`, params)
  136. .then(({ data }) => {
  137. console.log("user REST getUser data", data);
  138. console.log("roles", data.roles);
  139. commit("setUser", data);
  140. if (data.roles) {
  141. commit("setRoles", data.roles);
  142. }
  143. dispatch("getUserFlags");
  144. })
  145. .catch(error => {
  146. console.warn("Issue with getUser", error);
  147. Promise.reject(error);
  148. });
  149. },
  150. getUserFlags({ dispatch, commit, state }) {
  151. // flags
  152. // REST.get('/flagging_collection/1?_format=json')
  153. // .then(( data ) => {
  154. // console.log('TEST FLAG REST data', data)
  155. // })
  156. // .catch(error => {
  157. // console.warn('Issue USER TEST FLAG REST', error)
  158. // Promise.reject(error)
  159. // })
  160. return MA.get("materio_flag/user_flagging_collections")
  161. .then(({ data }) => {
  162. console.log("user MA getFlags data", data);
  163. commit("setFlags", data);
  164. })
  165. .catch(error => {
  166. console.warn("Issue USER MA getFlags", error);
  167. Promise.reject(error);
  168. });
  169. },
  170. createFlag({ dispatch, commit, state }, new_flag_name) {
  171. console.log("user createFlag", new_flag_name);
  172. // https://drupal.stackexchange.com/questions/248539/cant-get-flagging-api-to-accept-post-request
  173. const params = {
  174. name: new_flag_name
  175. };
  176. return MA.post("materio_flag/create_user_flagging_collection", params)
  177. .then(({ data }) => {
  178. console.log("user MA createFlag data", data);
  179. if (data.status) {
  180. dispatch('getUserFlags');
  181. }
  182. // commit("setFlags", data);
  183. })
  184. .catch(error => {
  185. console.warn("Issue USER MA createFlag", error);
  186. Promise.reject(error);
  187. });
  188. },
  189. userLogout({ commit, state }) {
  190. const credentials = qs.stringify({
  191. token: state.token
  192. });
  193. REST.post("/user/logout", credentials)
  194. .then(resp => {
  195. console.log("userLogout resp", resp);
  196. commit("setLoggedOut");
  197. })
  198. .catch(error => {
  199. console.warn("Issue with logout", error);
  200. Promise.reject(error);
  201. });
  202. }
  203. }
  204. };