Skip to content

Commit

Permalink
1842 add expiration date/time to user account (#1904)
Browse files Browse the repository at this point in the history
* add expiration to user structure, check expiration every 10s, set account to null upon expiration

* fix error in expires property of cookie

* changes in various files required to prevent attempted access of properties of null account object

* 5 sec interval for checking user account expiration

* remove console log statements for production
  • Loading branch information
roslynwythe authored Oct 20, 2024
1 parent e4d2045 commit 8fc059d
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions client/src/components/Authorization/UpdateAccount.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const UpdateAccount = props => {
const classes = useStyles();
const params = useParams();
const initialValues = {
firstName: account.firstName || "",
lastName: account.lastName || "",
firstName: account?.firstName || "",
lastName: account?.lastName || "",
email: params.email || ""
};

Expand Down
1 change: 1 addition & 0 deletions client/src/components/Layout/NavBarLogin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import NavBarToolTip from "./NavBarToolTip";
const NavBarLogin = ({ classes, handleHamburgerMenuClick }) => {
const userContext = useContext(UserContext);
const account = userContext.account;

const [isCalculation, setIsCalculation] = useState(false);

const location = useLocation();
Expand Down
17 changes: 16 additions & 1 deletion client/src/components/Layout/TdmAuthProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
import UserContext from "../../contexts/UserContext";

Expand All @@ -19,6 +19,21 @@ const getUserFromLocalStorage = () => {
const TdmAuthProvider = ({ children }) => {
const [account, setAccount] = useState(getUserFromLocalStorage());

const checkUserExpiration = () => {
if (account) {
const now = new Date();
const expirationDate = new Date(account.expiration);
if (now > expirationDate) {
updateAccount(null);
}
}
};

useEffect(() => {
const intervalId = setInterval(checkUserExpiration, 5000); // Poll every 5 seconds
return () => clearInterval(intervalId); // Cleanup on unmount
});

const updateAccount = userAccount => {
/*
Storing user account object in Local Storage as well as state allows the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function TdmCalculationContainer({ contentContainerRef }) {
try {
let projectResponse = null;
let inputs = {};
if (Number(projectId) > 0 && account.id) {
if (Number(projectId) > 0 && account?.id) {
projectResponse = await projectService.getById(projectId);

// setLoginId(projectResponse.data.loginId);
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Projects/FilterDrawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const FilterPopup = ({
onChange={e => handleChange(e, "address")}
className={classes.textInput}
/>
{account.isAdmin && (
{account?.isAdmin && (
<>
<h4 className={classes.minorHeading}>Author</h4>
<input
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Projects/MultiProjectToolbarMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const MultiProjectToolbarMenu = ({
) {
project = checkedProjectsStatusData;
}
const isProjectOwner = account.id === project?.loginId;
const isProjectOwner = account ? account.id === project?.loginId : false;

const isBtnDisabled = (projProp, criteriaProp) => {
const sameDateVals = checkedProjectsStatusData[projProp] !== false;
Expand Down
10 changes: 5 additions & 5 deletions client/src/components/Projects/ProjectContextMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const ProjectContextMenu = ({

return (
<ul className={classes.list}>
{project.dateSnapshotted && project.loginId == account.id ? (
{project.dateSnapshotted && project.loginId == account?.id ? (
<li
className={classes.listItem}
onClick={() => handleClick(handleRenameSnapshotModalOpen)}
Expand All @@ -76,7 +76,7 @@ const ProjectContextMenu = ({
</li>
) : null}

{project.dateSnapshotted && project.loginId !== account.id ? (
{project.dateSnapshotted && project.loginId !== account?.id ? (
<li className={classes.listItemDisabled}>
<MdEdit
className={classes.listItemIcon}
Expand All @@ -86,7 +86,7 @@ const ProjectContextMenu = ({
</li>
) : null}

{!project.dateSnapshotted && project.loginId == account.id ? (
{!project.dateSnapshotted && project.loginId == account?.id ? (
<li
className={classes.listItem}
onClick={() => handleClick(handleSnapshotModalOpen)}
Expand Down Expand Up @@ -129,7 +129,7 @@ const ProjectContextMenu = ({
/>
Duplicate
</li>
{project.loginId !== account.id ? null : (
{project.loginId !== account?.id ? null : (
<li
onClick={() => handleClick(handleHide)}
className={classes.listItem}
Expand All @@ -153,7 +153,7 @@ const ProjectContextMenu = ({
)}
</li>
)}
{project.loginId !== account.id ? null : (
{project.loginId !== account?.id ? null : (
<li
onClick={() => handleClick(handleDeleteModalOpen)}
className={classes.listItem}
Expand Down
6 changes: 5 additions & 1 deletion client/src/components/Projects/ProjectsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,11 @@ const ProjectsPage = ({ contentContainerRef }) => {
handleHide={handleHide}
handleCheckboxChange={handleCheckboxChange}
checkedProjectIds={checkedProjectIds}
isAdmin={userContext.account?.isAdmin}
isAdmin={
UserContext.account
? UserContext.account.isAdmin
: false
}
droOptions={droOptions}
onDroChange={handleDroChange} // Pass the DRO change handler
onAdminNoteUpdate={handleAdminNoteUpdate} // Pass the admin note update handler
Expand Down
14 changes: 7 additions & 7 deletions client/src/components/Roles.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const Roles = ({ contentContainerRef }) => {
const classes = useStyles();
const toast = useToast();
const userContext = useContext(UserContext);
const loggedInUserId = userContext.account.id;
const loggedInUserId = userContext.account?.id;

useEffect(() => {
const getAccounts = async () => {
Expand Down Expand Up @@ -295,25 +295,25 @@ const Roles = ({ contentContainerRef }) => {
trigger={
<button
className={`${classes.optionsButton} ${
account.isSecurityAdmin ||
account.id === loggedInUserId
account?.isSecurityAdmin ||
account?.id === loggedInUserId
? classes.disabledOptionsButton
: ""
}`}
disabled={
account.isSecurityAdmin ||
account.id === loggedInUserId
account?.isSecurityAdmin ||
account?.id === loggedInUserId
}
>
<MdMoreVert alt={`Options for ${account.email}`} />
<MdMoreVert alt={`Options for ${account?.email}`} />
</button>
}
position="bottom center"
offsetX={-100}
on="click"
closeOnDocumentClick
arrow={false}
onOpen={() => setHoveredRow(account.id)}
onOpen={() => setHoveredRow(account?.id)}
onClose={() => setHoveredRow(null)}
>
<div className={classes.popupContent}>
Expand Down
5 changes: 3 additions & 2 deletions server/middleware/jwt-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ async function login(req, res) {
isAdmin: req.user.isAdmin,
isSecurityAdmin: req.user.isSecurityAdmin
});
const expirationDateTime = new Date(Date.now() + 43200000); // 12 hours
res.cookie("jwt", token, {
httpOnly: true,
expires: new Date(Date.now() + 43200000) // 12 hours
expires: expirationDateTime
});
const user = req.user;
const user = { ...req.user, expiration: expirationDateTime };
res.json({ isSuccess: true, token: token, user });
}

Expand Down

0 comments on commit 8fc059d

Please sign in to comment.