user.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { REST } from 'vuejs/api/rest-axios'
  2. import { JSONAPI } from 'vuejs/api/json-axios'
  3. import qs from 'querystring'
  4. export default {
  5. namespaced: true,
  6. // initial state
  7. state : {
  8. uid:null,
  9. // username: '',
  10. mail:'',
  11. token: null,
  12. logout_token: null,
  13. isloggedin: false,
  14. isAdmin: false,
  15. canSearch: false
  16. },
  17. // getters
  18. getters : {},
  19. // mutations
  20. mutations : {
  21. setToken (state, data) {
  22. state.uid = data.current_user.uid
  23. // state.username = data.username
  24. state.mail = data.current_user.mail
  25. state.token = data.csrf_token
  26. state.isloggedin = true
  27. state.logout_token = data.logout_token
  28. },
  29. setUid (state, uid) {
  30. state.uid = uid
  31. state.isloggedin = true
  32. },
  33. setUser (state, data) {
  34. state.mail = data.mail[0].value
  35. state.uuid = data.uuid[0].value
  36. },
  37. setRoles (state, roles) {
  38. console.log("User setRoles", roles);
  39. state.roles = []
  40. for (var i = 0; i < roles.length; i++) {
  41. state.roles.push(roles[i].target_id)
  42. }
  43. // check if admin
  44. if(state.roles.indexOf('admin') != -1 || state.roles.indexOf('root') != -1){
  45. // console.log('is admin');
  46. state.isAdmin = true
  47. }
  48. // check if has access to search
  49. if(state.roles.indexOf('adherent') != -1){
  50. // console.log('is admin');
  51. state.canSearch = true
  52. }
  53. },
  54. setLoggedOut (state) {
  55. console.log("setLoggedOut state", state);
  56. state.uid= null
  57. state.mail = ''
  58. state.token = null
  59. state.isloggedin = false
  60. state.logout_token = null
  61. if (state.isAdmin){
  62. window.location.reload(true);
  63. }
  64. state.asAdmin = false
  65. state.canSearch = false
  66. }
  67. },
  68. // actions
  69. actions : {
  70. userLogin({ dispatch, commit, state }, credentials){
  71. dispatch('getToken', credentials)
  72. .then(() => {
  73. dispatch('getUser').then((userdata) => {
  74. console.log('User Loggedin');
  75. if (state.isAdmin){
  76. window.location.reload(true);
  77. }
  78. })
  79. })
  80. },
  81. getToken ({ dispatch, commit, state }, credentials) {
  82. return REST.post('/user/login?_format=json', credentials)
  83. .then(({ data }) => {
  84. console.log('user REST getToken data', data)
  85. commit('setToken', data)
  86. })
  87. .catch(( error ) => {
  88. console.warn('Issue with getToken', error)
  89. Promise.reject(error)
  90. })
  91. },
  92. getUser ({ dispatch, commit, state }) {
  93. let params = {
  94. token: state.token
  95. }
  96. return REST.get(`/user/${state.uid}?_format=json`, params)
  97. .then(({ data }) => {
  98. console.log('user REST getUser data', data)
  99. console.log('roles', data['roles'])
  100. commit('setUser', data)
  101. if(data.roles){
  102. commit('setRoles', data.roles)
  103. }
  104. })
  105. .catch(( error ) => {
  106. console.warn('Issue with getUser', error)
  107. Promise.reject(error)
  108. })
  109. },
  110. userLogout ({ commit, state }) {
  111. let credentials = qs.stringify({
  112. token: state.token
  113. })
  114. REST.post('/user/logout', credentials)
  115. .then((resp) => {
  116. console.log('userLogout resp', resp)
  117. commit('setLoggedOut')
  118. })
  119. .catch(( error ) => {
  120. console.warn('Issue with logout', error)
  121. Promise.reject(error)
  122. })
  123. }
  124. }
  125. }