annotate client/src/lib/session.js @ 4401:9842812b92e0

identify: fix numbers for nsc
author Thomas Junk <thomas.junk@intevation.de>
date Mon, 16 Sep 2019 16:46:29 +0200
parents f0be2aec953a
children 8dde014af77b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1362
ca33ad696594 remove the first empyty lines
Fadi Abbud <fadi.abbud@intevation.de>
parents: 1361
diff changeset
1 /* This is Free Software under GNU Affero General Public License v >= 3.0
1272
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
2 * without warranty, see README.md and license for details.
1361
ea3a89a1813a remove trailing whitespace, add headers for Makefile, add the missed authors
Fadi Abbud <fadi.abbud@intevation.de>
parents: 1272
diff changeset
3 *
1272
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
4 * SPDX-License-Identifier: AGPL-3.0-or-later
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
5 * License-Filename: LICENSES/AGPL-3.0.txt
1361
ea3a89a1813a remove trailing whitespace, add headers for Makefile, add the missed authors
Fadi Abbud <fadi.abbud@intevation.de>
parents: 1272
diff changeset
6 *
ea3a89a1813a remove trailing whitespace, add headers for Makefile, add the missed authors
Fadi Abbud <fadi.abbud@intevation.de>
parents: 1272
diff changeset
7 * Copyright (C) 2018 by via donau
1272
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
8 * – Österreichische Wasserstraßen-Gesellschaft mbH
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
9 * Software engineering by Intevation GmbH
1361
ea3a89a1813a remove trailing whitespace, add headers for Makefile, add the missed authors
Fadi Abbud <fadi.abbud@intevation.de>
parents: 1272
diff changeset
10 *
1272
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
11 * Author(s):
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
12 * Thomas Junk <thomas.junk@intevation.de>
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
13 */
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
14
1701
f4bd67daaaff feat: force login on invalid session
Thomas Junk <thomas.junk@intevation.de>
parents: 1362
diff changeset
15 import app from "@/main";
3685
8775bea8042a client: layers: unset layer config on logout
Markus Kottlaender <markus@intevation.de>
parents: 1701
diff changeset
16 import { unsetLayerConfigs } from "@/components/map/layers";
4379
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
17 import { HTTP } from "@/lib/http";
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
18 import { displayError } from "@/lib/errors";
1701
f4bd67daaaff feat: force login on invalid session
Thomas Junk <thomas.junk@intevation.de>
parents: 1362
diff changeset
19
f4bd67daaaff feat: force login on invalid session
Thomas Junk <thomas.junk@intevation.de>
parents: 1362
diff changeset
20 const logOff = () => {
4379
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
21 HTTP.get("/logout", {
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
22 headers: {
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
23 "X-Gemma-Auth": localStorage.getItem("token"),
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
24 "Content-type": "text/xml; charset=UTF-8"
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
25 }
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
26 })
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
27 .then(() => {
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
28 app.$snotify.clear();
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
29 app.$store.commit("reset");
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
30 app.$store.commit("user/clearAuth");
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
31 app.$router.push("/login");
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
32 unsetLayerConfigs();
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
33 })
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
34 .catch(error => {
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
35 const { status, data } = error.response;
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
36 displayError({
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
37 title: this.$gettext("Backend Error"),
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
38 message: `${status}: ${data.message || data}`
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
39 });
f0be2aec953a logout: Logging out triggers a request to log the user out on server side
Thomas Junk <thomas.junk@intevation.de>
parents: 3685
diff changeset
40 });
1701
f4bd67daaaff feat: force login on invalid session
Thomas Junk <thomas.junk@intevation.de>
parents: 1362
diff changeset
41 };
f4bd67daaaff feat: force login on invalid session
Thomas Junk <thomas.junk@intevation.de>
parents: 1362
diff changeset
42
1272
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
43 /**
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
44 * Compares whether session is current
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
45 * based on the expiry information and the
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
46 * current date
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
47 *
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
48 * @param {number} expiresFromPastSession
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
49 */
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
50 function sessionStillActive(expiresFromPastSession) {
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
51 if (!expiresFromPastSession) return false;
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
52 const now = Date.now();
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
53 const stillActive = now < expiresFromPastSession;
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
54 return stillActive;
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
55 }
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
56 /**
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
57 * Converts a given unix time to Milliseconds
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
58 *
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
59 * @param {string} timestring
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
60 */
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
61 function toMillisFromString(timestring) {
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
62 return timestring * 1000;
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
63 }
bc55ffaeb639 cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
diff changeset
64
1701
f4bd67daaaff feat: force login on invalid session
Thomas Junk <thomas.junk@intevation.de>
parents: 1362
diff changeset
65 export { logOff, sessionStillActive, toMillisFromString };