Skip to content

Commit

Permalink
Fix exp comparison (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
shincap8 authored Nov 27, 2024
1 parent 7731ccb commit 97877ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
15 changes: 11 additions & 4 deletions frontends/web/src/common/ApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,9 @@ export default class ApiService {
isTokenExpired(token) {
try {
const decoded = decode(token);
if (decoded.exp < Date.now() / 1048576) {
//The exp field is in seconds, so we multiply by 1000 to get milliseconds
//equal to the date object got by Date.now().
if (decoded.exp * 1000 < Date.now()) {
// Checking if token is expired. N
return true;
} else return false;
Expand All @@ -916,9 +918,14 @@ export default class ApiService {
}

logout() {
this.fetch(`${this.domain}/authenticate/logout`, {
method: "POST",
});
try {
this.fetch(`${this.domain}/authenticate/logout`, {
method: "POST",
});
} catch (e) {
console.log("Could not logout");
console.warn(e);
}
localStorage.removeItem("id_token");
}

Expand Down
21 changes: 18 additions & 3 deletions frontends/web/src/containers/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import qs from "qs";
import React from "react";
import { Container, Nav, Navbar, NavDropdown, Row } from "react-bootstrap";
import ReactGA from "react-ga";
import { BrowserRouter, Link, Route, Switch } from "react-router-dom";
import { BrowserRouter, Link, Route, Switch, Redirect } from "react-router-dom";
import { Provider as FetchProvider } from "use-http";
import { OverlayProvider } from "new_front/components/OverlayInstructions/Provider";
import { ParallaxProvider } from "react-scroll-parallax";
Expand Down Expand Up @@ -129,7 +129,9 @@ class App extends React.Component {
(error) => {
console.warn(error);
if (error.status_code === 401) {
this.api.logout();
console.log("Logging out due to 401");
<Redirect push to="/logout" />;
//In Case Redirect doesn't work window.location.href = "/logout";
}
}
);
Expand All @@ -148,6 +150,14 @@ class App extends React.Component {
this.refreshData();
}

componentDidUpdate(prevProps, prevState) {
const userCredentials = this.api.getCredentials();
if (!this.api.loggedIn() && userCredentials.id) {
console.log("Redirecting to logout");
return <Redirect push to="/logout" />;
}
}

render() {
//href={`/tasks/${taskCode}`}
var query = qs.parse(window.location.search, {
Expand Down Expand Up @@ -617,7 +627,12 @@ class App extends React.Component {
class Logout extends React.Component {
static contextType = UserContext;
componentDidMount() {
this.context.api.logout();
try {
console.log("logout");
this.context.api.logout();
} catch (e) {
console.warn(e);
}
this.context.updateState({ user: {} });
this.props.history.push("/");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ const SelectMultipleTextMultipleTags: FC<
value[valueLength - 1].start,
value[valueLength - 1].end
);
valueLength > 0 && console.log("last value", value[valueLength - 1].start);
if (
valueLength > 0 &&
(isNaN(value[valueLength - 1].end) ||
Expand Down

0 comments on commit 97877ca

Please sign in to comment.