From 5ab2dfa0f1c766f31ff4e7c5fa864e1fb0b081cb Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 11 Jun 2023 20:23:36 +0200 Subject: [PATCH] added last online and unauthorized status handling --- src/Pages/GroupTasks/GroupTasksViewModal.js | 1 - src/Pages/Login/index.js | 4 ++-- src/Pages/Scanners/index.js | 5 ++++- src/Pages/UserProfile/index.js | 9 ++++++--- src/Pages/Users/index.js | 2 ++ src/utils.js | 22 ++++++++++++++++----- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Pages/GroupTasks/GroupTasksViewModal.js b/src/Pages/GroupTasks/GroupTasksViewModal.js index 235fe06..4e17f0b 100644 --- a/src/Pages/GroupTasks/GroupTasksViewModal.js +++ b/src/Pages/GroupTasks/GroupTasksViewModal.js @@ -746,7 +746,6 @@ function InputRequiredHandler({ /> ); - break; default: notificationApi["error"]({ message: `Type ${groupTaskParameter.type} not implemented`, diff --git a/src/Pages/Login/index.js b/src/Pages/Login/index.js index 7d94572..1c46a2e 100644 --- a/src/Pages/Login/index.js +++ b/src/Pages/Login/index.js @@ -1,7 +1,7 @@ import { LockOutlined, LoginOutlined, UserOutlined } from "@ant-design/icons"; import { Button, Form, Input, Modal, notification } from "antd"; import PropTypes from "prop-types"; -import { Constants } from "../../utils"; +import { Constants, setUserSessionToLocalStorage } from "../../utils"; import { useState } from "react"; import { Buffer } from "buffer"; @@ -44,7 +44,7 @@ export default function Login({ setUserSession }) { return Promise.reject(res.status); }) .then((data) => { - setUserSession(data.Session); + setUserSessionToLocalStorage(data.Session); window.location.href = "/"; }) .catch((err) => { diff --git a/src/Pages/Scanners/index.js b/src/Pages/Scanners/index.js index 652b8f2..5b584b1 100644 --- a/src/Pages/Scanners/index.js +++ b/src/Pages/Scanners/index.js @@ -5,6 +5,7 @@ import { WebSocketContext, getUserId, getUserSessionFromLocalStorage, + handleUnauthorizedStatus, } from "../../utils"; import { useContext } from "react"; import { Link } from "react-router-dom"; @@ -59,7 +60,9 @@ const columns = [ "Content-Type": "application/json", "X-Authorization": getUserSessionFromLocalStorage(), }, - }).catch((err) => console.error(err)); + }) + .then((res) => handleUnauthorizedStatus(res.status)) + .catch((err) => console.error(err)); }} > Use scanner diff --git a/src/Pages/UserProfile/index.js b/src/Pages/UserProfile/index.js index 2f76365..bb3e1b7 100644 --- a/src/Pages/UserProfile/index.js +++ b/src/Pages/UserProfile/index.js @@ -7,6 +7,7 @@ import { WebSocketContext, getConnectionStatusItem, getUserSessionFromLocalStorage, + handleUnauthorizedStatus, } from "../../utils"; import { Link } from "react-router-dom"; @@ -47,9 +48,11 @@ const columns = [ "Content-Type": "application/json", "X-Authorization": getUserSessionFromLocalStorage(), }, - }).catch((err) => { - console.error(err); - }); + }) + .then((res) => handleUnauthorizedStatus(res.status)) + .catch((err) => { + console.error(err); + }); }} > Sign out diff --git a/src/Pages/Users/index.js b/src/Pages/Users/index.js index 54f7966..f65b394 100644 --- a/src/Pages/Users/index.js +++ b/src/Pages/Users/index.js @@ -1,5 +1,6 @@ import { Popover, Table } from "antd"; import { + FormatDatetime, MyAvatar, WebSocketContext, getConnectionStatusItem, @@ -55,6 +56,7 @@ export default function Users() { ), connectionStatus: getConnectionStatusItem(user.ConnectionStatus), username: user.Username, + lastOnline: FormatDatetime(user.LastOnline), }); }); diff --git a/src/utils.js b/src/utils.js index c12aef8..1628939 100644 --- a/src/utils.js +++ b/src/utils.js @@ -62,7 +62,7 @@ export function UseUserSession() { if (session === undefined) { localStorage.removeItem("session"); } else { - localStorage.setItem("session", JSON.stringify(session)); + setUserSessionToLocalStorage(session); } }; @@ -73,7 +73,18 @@ export function UseUserSession() { } export function getUserSessionFromLocalStorage() { - return JSON.parse(localStorage.getItem("session")); + return localStorage.getItem("session"); +} + +export function setUserSessionToLocalStorage(session) { + localStorage.setItem("session", session); +} + +export function handleUnauthorizedStatus(status) { + if (status === 401) { + setUserSessionToLocalStorage(""); + window.location.href = "/"; + } } /** @@ -187,9 +198,10 @@ export function WebSocketProvider({ setAllUsers((arr) => { const newArr = [...arr]; - newArr[ - arr.findIndex((arr1) => arr1.Id === body.UserId) - ].ConnectionStatus = body.ConnectionStatus; + const arrIndex = arr.findIndex((arr1) => arr1.Id === body.UserId); + + newArr[arrIndex].ConnectionStatus = body.ConnectionStatus; + newArr[arrIndex].LastOnline = body.LastOnline; return newArr; });