Skip to content

Commit

Permalink
Merge pull request #19 from npolar/revert-13-token-renew
Browse files Browse the repository at this point in the history
Revert "Token renewal"
  • Loading branch information
cnrdh authored Dec 16, 2020
2 parents c13d30d + f7622ee commit d1b3440
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 115 deletions.
110 changes: 2 additions & 108 deletions src/placenames-shell.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// security
import { isValidJWT, getJWTIfValid, getJWT, setJWT, deleteJWT, refreshJWT } from "./store/user.js";
import { payload } from '@npolar/fetch-api/src/jwt.js'
import { isValidJWT, getJWT, setJWT, deleteJWT } from "./store/user.js";

// routing
import { routes } from "./routes.js";
Expand All @@ -23,7 +22,6 @@ import { getUserLang } from "./store/user.js";
// UI
import { whiteLogo } from "@npolar/mdc/src/app-shell/npolar-logo.js";
import "@npolar/mdc/src/tab-bar/tab-bar.js";
import "@npolar/mdc/src/dialog/dialog.js";

// Internals
import { AppShellMixin, Router } from "@npolar/mdc/src/app-shell/exports.js";
Expand Down Expand Up @@ -64,99 +62,23 @@ export class PlacenamesShell extends AppShellMixin({
return {
...super.properties,
activeTab: { type: Number },
aboutToLogOut : { type: Boolean },
sessionTime: { type: Number },
};
}

// updated(p) {
// console.warn(p);
// }

constructor() {
super()
this._thresholdSessionTime = 60000 // 1min in ms
}

drawer() {} //disable drawer

headerRight() {
return whiteLogo({ host: this, t, html, height: "48" });
}

async getRemainingTokenValidTime() {
const jwt = await getJWTIfValid()
if(!jwt) return undefined

this.sessionTime = new Date(payload(jwt).exp*1000 - Date.now())
this.aboutToLogOut = this.sessionTime < this._thresholdSessionTime
}

renderWarningLogOut() {
let { aboutToLogOut, sessionTime, _warningIntervalID: intervalID } = this

if(!aboutToLogOut) return html``

if(intervalID === undefined) {
intervalID =
setInterval(
async () => {
await this.getRemainingTokenValidTime()
},
1000
)
this._warningIntervalID = intervalID
}

/*
* Note: if one want to put the warning over a minute before the end of the
* session, change the output (as it is it will only print the number of
* seconds in UTC standard time).
*/
return html`
<dialog-mdc open>
<button-up
slot="primaryAction"
dialogAction="refresh-session"
@click=${() =>
window.dispatchEvent(
new CustomEvent('closed',
{
detail: {action: 'refresh-session', intervalID},
bubble: true
}
)
)
}>
Refresh
</button-up>
<button-up
slot="secondaryAction"
dialogAction="sign-out"
@click=${() =>
window.dispatchEvent(
new CustomEvent('closed',
{
detail: {action: 'sign-out', intervalID},
bubble: true
}
)
)
}>
Log out
</button-up>
<div>Session is about to end. Would you like to refresh the session or log out?<div>
<div>Session expires in ${sessionTime.getUTCSeconds()}s.</div>
</dialog-mdc>
`
}

beforeMain() {
const { handleTabNav, activeTab, tabClicked, authorized } = this;

return html`
${ this.renderWarningLogOut() }
<nav class="app-nav-left">
return html`<nav class="app-nav-left">
<mwc-tab-bar activeIndex="${activeTab}">
<mwc-tab
href="${placenameBase}"
Expand Down Expand Up @@ -199,40 +121,12 @@ export class PlacenamesShell extends AppShellMixin({
`;
}

disconnectedCallback() {
const { _intervalID, _warningIntervalID } = this
super.disconnectedCallback()
for(const id of [_intervalID, _warningIntervalID])
if (id !== undefined) clearInterval(id)
}

async connectedCallback() {
super.connectedCallback();
this.href = "/";
this.handleURLChange();
this.authenticated = isValidJWT(await getJWT());

this._intervalID =
setInterval(
async () => await this.getRemainingTokenValidTime(),
this._thresholdSessionTime / 2
)

window.addEventListener("closed",
async ({ detail: {action, intervalID} }) => {
clearInterval(intervalID)
this._warningIntervalID = undefined
switch(action) {
case 'sign-out':
await this.signOut()
break
case 'refresh-session':
await refreshJWT(this)
break
}
})


window.addEventListener("popstate", () => this.handleURLChange());

window.addEventListener(
Expand Down
7 changes: 0 additions & 7 deletions src/store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ const SYSTEM = "https://api.npolar.no/placename";
export const storage = localStorage;
export const getJWT = async () => storage.getItem(JWTKEY);

export const refreshJWT = async (host) => {
const jwt = await getJWT()
const response = await refresh({jwt, host})
const { token } = await response.json()
await setJWT(token)
}

export const getJWTIfValid = async () => {
const jwt = await getJWT();
return isValidJWT(jwt) ? jwt : undefined;
Expand Down

0 comments on commit d1b3440

Please sign in to comment.