view client/src/store/user.js @ 3264:9ae43313b463

Handle some possibly missing elements in NtS response The NtS XSD does not guarantee that value, value_min and value_max are present in a response. This led to circumventing the NOT NULL constraint for the waterlevel value by silently persisting missing values as zero and filling up missing confidence interval values especially for measurements with zeros.
author Tom Gottfried <tom@intevation.de>
date Wed, 15 May 2019 12:31:57 +0200
parents a6eea3002e6e
children
line wrap: on
line source

/* This is Free Software under GNU Affero General Public License v >= 3.0
 * without warranty, see README.md and license for details.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 * License-Filename: LICENSES/AGPL-3.0.txt
 *
 * Copyright (C) 2018 by via donau
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 *
 * Author(s):
 * Thomas Junk <thomas.junk@intevation.de>
 * Markus Kottländer <markus@intevation.de>
 */

import { HTTP } from "@/lib/http";
import { toMillisFromString } from "@/lib/session";

const init = () => {
  return {
    isAuthenticated: false,
    expires: null,
    roles: [],
    user: ""
  };
};

export default {
  init,
  namespaced: true,
  state: init(),
  getters: {
    isWaterwayAdmin: state => {
      return state.roles.includes("waterway_admin");
    },
    isSysAdmin: state => {
      return state.roles.includes("sys_admin");
    }
  },
  mutations: {
    authSuccess: (state, data) => {
      const { token, user, expires, roles } = data;
      localStorage.setItem("expires", expires);
      localStorage.setItem("roles", roles);
      localStorage.setItem("token", token);
      localStorage.setItem("user", user);
      state.expires = expires;
      state.roles = roles;
      state.user = user;
      state.authenticated = true;
    },
    clearAuth: () => {
      localStorage.removeItem("expires");
      localStorage.removeItem("roles");
      localStorage.removeItem("token");
      localStorage.removeItem("user");
    },
    setUser: (state, name) => {
      state.user = name;
    },
    setRoles: (state, roles) => {
      state.roles = roles;
    },
    setExpires: (state, expires) => {
      state.expires = expires;
    },
    setIsAuthenticate: state => {
      state.isAuthenticated = true;
    }
  },
  actions: {
    login({ commit }, user) {
      // using POST is a bit more secure than GET
      return new Promise((resolve, reject) => {
        const handleResponse = response => {
          const { expires } = response.data;
          const renew =
            (new Date(toMillisFromString(expires)) - new Date()) * 0.9;
          commit("authSuccess", response.data);
          resolve(response);
          setTimeout(() => {
            HTTP.get("/renew", {
              headers: { "X-Gemma-Auth": localStorage.getItem("token") }
            }).then(handleResponse);
          }, renew);
        };

        HTTP.post("/login", user)
          .then(handleResponse)
          .catch(error => {
            commit("reset", null, { root: true });
            commit("clearAuth");
            reject(error);
          });
      });
    }
  }
};