Skip to content

Commit

Permalink
remove implementations of momentjs
Browse files Browse the repository at this point in the history
  • Loading branch information
gmrabian committed Aug 22, 2024
1 parent e7eab66 commit e78a1ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions services/ui-src/src/components/layout/Timeout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
UserContext,
} from "utils";
import { PROMPT_AT, IDLE_WINDOW } from "../../constants";
import moment from "moment";
import { add } from "date-fns";

export const Timeout = () => {
const context = useContext(UserContext);
Expand All @@ -43,7 +43,7 @@ export const Timeout = () => {
}, [location]);

const setTimer = () => {
const expiration = moment().add(IDLE_WINDOW, "milliseconds");
const expiration = add(Date.now(), { seconds: IDLE_WINDOW / 1000 });
if (timeoutPromptId) {
clearTimers();
}
Expand Down
27 changes: 15 additions & 12 deletions services/ui-src/src/utils/auth/authLifecycle.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { initAuthManager, updateTimeout, getExpiration } from "utils";
import { refreshCredentials } from "./authLifecycle";
import moment from "moment";
import { sub } from "date-fns";
import { Hub } from "aws-amplify";

describe("utils/auth", () => {
describe("Test AuthManager Init", () => {
test("Initiallizing when past expiration will require a new login", async () => {
test("Initializing when past expiration will require a new login", async () => {
// Set an initial time, because jest runs too fast to have different timestamps
const expired = moment().subtract(5, "days").format().toString();
const expired = sub(Date.now(), { days: 5 }).toString();
localStorage.setItem("mdctmfp_session_exp", expired);

initAuthManager();
Expand All @@ -25,28 +25,29 @@ describe("utils/auth", () => {
});

test("Test updateTimeout", () => {
const currentTime = moment();
const currentTime = Date.now();
updateTimeout();
jest.runAllTimers(); // Dodge 2 second debounce, get the updated timestamp

const savedTime = localStorage.getItem("mdctmfp_session_exp");
expect(moment(savedTime).isSameOrAfter(currentTime)).toBeTruthy();
expect(new Date(savedTime!).valueOf()).toBeGreaterThanOrEqual(
new Date(currentTime).valueOf()
);
});

test("Test getExpiration and refreshCredentials", async () => {
// Set an initial time, because jest runs too fast to have different timestamps
const initialExpiration = moment()
.subtract(5, "seconds")
.format()
.toString();
const initialExpiration = sub(Date.now(), { seconds: 5 }).toString();
localStorage.setItem("mdctmfp_session_exp", initialExpiration);
await refreshCredentials();
jest.runAllTimers(); // Dodge 2 second debounce, get the updated timestamp

// Check that the new timestamp is updated
const storedExpiration = getExpiration();
expect(storedExpiration).not.toEqual(initialExpiration);
expect(moment(storedExpiration).isAfter(initialExpiration)).toBeTruthy();
expect(new Date(storedExpiration!).valueOf()).toBeGreaterThan(
new Date(initialExpiration).valueOf()
);
});
test("Test getExpiration returns an empty string if nothing is set", async () => {
localStorage.removeItem("mdctmfp_session_exp");
Expand All @@ -73,15 +74,17 @@ describe("utils/auth", () => {
});

test("Ignore unrelated auth events", () => {
const currentTime = moment();
const currentTime = Date.now();
Hub.listen = jest
.fn()
.mockImplementation((channel: string, callback: any) => {
callback({ payload: { event: "nonExistantEvent" } });
});
initAuthManager();
const savedTime = localStorage.getItem("mdctmfp_session_exp");
expect(moment(savedTime).isSameOrAfter(currentTime)).toBeTruthy();
expect(new Date(savedTime!).valueOf()).toBeGreaterThanOrEqual(
new Date(currentTime).valueOf()
);
expect(localStorage.setItem).not.toHaveBeenCalled();
});
});
Expand Down
15 changes: 8 additions & 7 deletions services/ui-src/src/utils/auth/authLifecycle.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Auth, Hub } from "aws-amplify";
import moment from "moment";
import { add } from "date-fns";
import { IDLE_WINDOW } from "../../constants";

let authManager: AuthManager;
Expand All @@ -13,8 +13,10 @@ class AuthManager {

constructor() {
// Force users with stale tokens > then the timeout to log in for a fresh session
const exp = localStorage.getItem("mdctmfp_session_exp");
if (exp && moment(exp).isBefore()) {
const expiration = localStorage.getItem("mdctmcr_session_exp");
const isExpired =
expiration && new Date(expiration).valueOf() < Date.now().valueOf();
if (isExpired) {
localStorage.removeItem("mdctmfp_session_exp");
Auth.signOut().then(() => {
window.location.href = "/";
Expand Down Expand Up @@ -53,10 +55,9 @@ class AuthManager {
* Timer function for idle timeout, keeps track of an idle timer that triggers a forced logout timer if not reset.
*/
setTimer = () => {
const expiration = moment()
.add(IDLE_WINDOW, "milliseconds")
.format()
.toString();
const expiration = add(Date.now(), {
seconds: IDLE_WINDOW / 1000,
}).toString();
localStorage.setItem("mdctmfp_session_exp", expiration);
};
}
Expand Down
3 changes: 1 addition & 2 deletions services/ui-src/src/utils/other/time.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz";
import { DateShape, TimeShape } from "types";
import moment from "moment";

export const midnight: TimeShape = { hour: 0, minute: 0, second: 0 };
export const oneSecondToMidnight: TimeShape = {
Expand Down Expand Up @@ -146,7 +145,7 @@ export const checkDateRangeStatus = (
*/
export const calculateRemainingSeconds = (expiresAt?: any) => {
if (!expiresAt) return 0;
return moment(expiresAt).diff(moment()) / 1000;
return (new Date(expiresAt).valueOf() - Date.now()) / 1000;
};

export const displayLongformPeriod = (
Expand Down

0 comments on commit e78a1ba

Please sign in to comment.