Skip to content

Commit

Permalink
fix: reset ForceLogout component upon relogin (awslabs#640)
Browse files Browse the repository at this point in the history
* fix: reset ForceLogout component upon relogin

* fix: enter intervalId for clearInterval call

* Trigger notification

* fix: update clearInterval call in unit test

* fix: clear IntervalId
  • Loading branch information
SanketD92 authored Aug 9, 2021
1 parent 5abf9c2 commit bfc9f6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 19 additions & 7 deletions addons/addon-base-ui/packages/base-ui/src/parts/ForceLogout.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ForceLogout extends React.Component {
super(props);
runInAction(() => {
this.tokenActive = true;
this.intervalId = undefined;
});
}

Expand All @@ -45,22 +46,32 @@ class ForceLogout extends React.Component {
}

componentDidMount() {
this.timer = setInterval(() => {
runInAction(() => {
this.tokenActive = !this.hasTokenExpired();
});
}, 1000);
runInAction(() => {
this.intervalId = setInterval(() => {
runInAction(() => {
this.tokenActive = !this.hasTokenExpired();
});
}, 1000);
});
}

componentWillUnmount() {
clearInterval(this.timer);
clearInterval();
}

doLogout = async () => {
clearInterval(this.timer);
clearInterval();
return this.authentication.logout({ autoLogout: true });
};

clearInterval() {
if (!_.isUndefined(this.intervalId)) {
clearInterval(this.intervalId);
this.intervalId = undefined;
}
this.tokenActive = true;
}

hasTokenExpired = () => {
try {
const idToken = localStorage.getItem('appIdToken');
Expand Down Expand Up @@ -111,6 +122,7 @@ decorate(ForceLogout, {
authentication: computed,
modalOpen: computed,
tokenActive: observable,
intervalId: observable,
doLogout: action,
handleLogout: action,
clearInterval: action,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ describe('ForceLogout', () => {
);
const component = tempWrapper.instance();
expect(component.render()).toEqual(null);
clearInterval(component.timer);
clearInterval(component.intervalId);
});

afterAll(async () => {
const component = wrapper.instance();
clearInterval(component.timer);
clearInterval(component.intervalId);
});
});

0 comments on commit bfc9f6d

Please sign in to comment.