123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- import { REST } from "vuejs/api/rest-axios";
- // import { JSONAPI } from 'vuejs/api/json-axios';
- import { MA } from "vuejs/api/ma-axios";
- import qs from "querystring";
- export default {
- namespaced: true,
- // initial state
- state: {
- uid: null,
- // username: '',
- mail: "",
- token: null,
- csrftoken: null,
- logout_token: null,
- isloggedin: false,
- isAdmin: false,
- isAdherent: false,
- canSearch: false,
- roles: [],
- flags: false
- },
- // getters
- getters: {},
- // mutations
- mutations: {
- SetCsrftoken(state, token) {
- state.csrftoken = token;
- },
- setToken(state, data) {
- state.uid = data.current_user.uid;
- // state.username = data.username;
- state.mail = data.current_user.mail;
- state.token = data.csrf_token;
- state.isloggedin = true;
- state.logout_token = data.logout_token;
- },
- setUid(state, uid) {
- state.uid = uid;
- state.isloggedin = true;
- },
- setUser(state, data) {
- state.mail = data.mail[0].value;
- state.uuid = data.uuid[0].value;
- },
- setRoles(state, roles) {
- console.log("User setRoles", roles);
- state.roles = [];
- for (let i = 0; i < roles.length; i++) {
- state.roles.push(roles[i].target_id);
- }
- // check if admin
- if (
- state.roles.indexOf("admin") !== -1 ||
- state.roles.indexOf("root") !== -1
- ) {
- // console.log('is admin');
- state.isAdmin = true;
- }
- // check if has access to search
- if (state.roles.indexOf("adherent") !== -1) {
- // console.log('is admin');
- state.canSearch = true;
- state.isAdherent = true;
- }
- },
- setLoggedOut(state) {
- console.log("setLoggedOut state", state);
- state.uid = null;
- state.mail = "";
- state.token = null;
- state.isloggedin = false;
- state.logout_token = null;
- if (state.isAdmin) {
- // TODO: what if on a page where login is needed (as commerce checkout and cart)
- window.location.reload(true);
- }
- state.asAdmin = false;
- state.canSearch = false;
- },
- setFlags(state, flags) {
- console.log("User setFlags", flags);
- state.flags = flags;
- }
- },
- // actions
- actions: {
- userRegister({ dispatch, commit, state }, credentials) {
- return new Promise((resolve, reject) => {
- REST.get("/session/token").then(({ token }) => {
- commit("SetCsrftoken", token);
- REST.post("/user/register?_format=json", credentials, {
- "X-CSRF-Token": state.csrftoken
- })
- .then(({ data }) => {
- console.log("user REST registered", data);
- dispatch("userLogin", credentials).then(() => {
- resolve();
- });
- })
- .catch(error => {
- console.warn("Issue with register", error);
- Promise.reject(error);
- });
- });
- });
- },
- userLogin({ dispatch, commit, state }, credentials) {
- return new Promise((resolve, reject) => {
- dispatch("getToken", credentials).then(() => {
- dispatch("getUser").then(userdata => {
- console.log("User Loggedin");
- if (state.isAdmin) {
- window.location.reload(true);
- }
- resolve();
- });
- });
- });
- },
- getToken({ dispatch, commit, state }, credentials) {
- return REST.post("/user/login?_format=json", credentials)
- .then(({ data }) => {
- console.log("user REST getToken data", data);
- commit("setToken", data);
- })
- .catch(error => {
- console.warn("Issue with getToken", error);
- Promise.reject(error);
- });
- },
- getUser({ dispatch, commit, state }) {
- const params = {
- token: state.token
- };
- return REST.get(`/user/${state.uid}?_format=json`, params)
- .then(({ data }) => {
- console.log("user REST getUser data", data);
- console.log("roles", data.roles);
- commit("setUser", data);
- if (data.roles) {
- commit("setRoles", data.roles);
- }
- dispatch("getUserFlags");
- })
- .catch(error => {
- console.warn("Issue with getUser", error);
- Promise.reject(error);
- });
- },
- getUserFlags({ dispatch, commit, state }) {
- // flags
- // REST.get('/flagging_collection/1?_format=json')
- // .then(( data ) => {
- // console.log('TEST FLAG REST data', data)
- // })
- // .catch(error => {
- // console.warn('Issue USER TEST FLAG REST', error)
- // Promise.reject(error)
- // })
- return MA.get("materio_flag/user_flagging_collections")
- .then(({ data }) => {
- console.log("user MA getFlags data", data);
- commit("setFlags", data);
- })
- .catch(error => {
- console.warn("Issue USER MA getFlags", error);
- Promise.reject(error);
- });
- },
- createFlag({ dispatch, commit, state }, new_flag_name) {
- console.log("user createFlag", new_flag_name);
- // https://drupal.stackexchange.com/questions/248539/cant-get-flagging-api-to-accept-post-request
- const params = {
- name: new_flag_name
- };
- return MA.post("materio_flag/create_user_flagging_collection", params)
- .then(({ data }) => {
- console.log("user MA createFlag data", data);
- if (data.status) {
- dispatch('getUserFlags');
- }
- // commit("setFlags", data);
- })
- .catch(error => {
- console.warn("Issue USER MA createFlag", error);
- Promise.reject(error);
- });
- },
- userLogout({ commit, state }) {
- const credentials = qs.stringify({
- token: state.token
- });
- REST.post("/user/logout", credentials)
- .then(resp => {
- console.log("userLogout resp", resp);
- commit("setLoggedOut");
- })
- .catch(error => {
- console.warn("Issue with logout", error);
- Promise.reject(error);
- });
- }
- }
- };
|