From 2ab5c7a6f548904907d75ef1a7cb33b983992323 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Wed, 21 Oct 2020 23:52:32 -0500 Subject: [PATCH] Add logic for fetching device tokens and using them to resume session --- src/app/app.component.ts | 17 +++++++-- src/app/service/api.service.ts | 67 ++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 0648889..1a35126 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -442,11 +442,22 @@ export class AppComponent implements OnInit { } }); - const stat: any = await this.session.stat(); + let stat: any = await this.session.stat(); if ( !stat.authenticated_user ) { - window.location.href = `${stat.system_base}start`; - return; + if ( !this.api.isOffline ) { + await this.api.resumeSession(); + stat = await this.session.stat(); + + if ( !stat.authenticated_user ) { + window.location.href = `${stat.system_base}start`; + return; + } + } else { + await this.db.purge(); + window.location.href = `${stat.system_base}start`; + return; + } } this.session.appName = stat.app_name; diff --git a/src/app/service/api.service.ts b/src/app/service/api.service.ts index 56e6439..e25567b 100644 --- a/src/app/service/api.service.ts +++ b/src/app/service/api.service.ts @@ -158,6 +158,46 @@ export class ApiService { return `${this.baseEndpoint.endsWith('/') ? this.baseEndpoint.slice(0, -1) : this.baseEndpoint}${endpoint}`; } + public getDeviceToken(): Promise { + return new Promise(async (res, rej) => { + const tokenKV = await this.db.getKeyValue('device_token'); + if ( !tokenKV.data && this.isOffline ) { + return rej(new ResourceNotAvailableOfflineError()); + } + + if ( tokenKV.data ) { + const expDate = new Date(tokenKV.data.expiration_date); + if (expDate > new Date()) { + return res(tokenKV.data.token); + } + } + + this.get(`/session/device-token`).subscribe({ + next: async result => { + tokenKV.data = result.data; + await tokenKV.save(); + res(result.data.token); + }, + error: rej, + }); + }); + } + + public resumeSession(): Promise { + return new Promise(async (res, rej) => { + if ( !this.isOffline ) { + this.getDeviceToken().then(token => { + this.post(`/session/resume/${token}`).subscribe({ + next: result => { + res(); + }, + error: rej, + }); + }).catch(rej); + } + }); + } + public getMenuItems(): Promise { return new Promise(async (res, rej) => { await this.db.createSchemata(); @@ -193,6 +233,7 @@ export class ApiService { public getSessionData(): Promise { return new Promise(async (res, rej) => { const sessionKV = await this.db.getKeyValue('session_data'); + const authenticatedUserKV = await this.db.getKeyValue('authenticated_user'); // If offline, just return the locally cached session data if ( this.isOffline ) { @@ -205,6 +246,16 @@ export class ApiService { // Otherwise, fetch the session data from the server and cache it locally this.get('/session').subscribe(async result => { + // If the locally logged in user is not the one in the server session, purge everything and reset + if ( + authenticatedUserKV.data + && authenticatedUserKV.data?.user?.id + && authenticatedUserKV.data.user.id !== result.data.user.id + ) { + await this.cleanSession(result.data); + return res(result.data); + } + sessionKV.data = result.data; await sessionKV.save(); res(sessionKV.data); @@ -212,6 +263,22 @@ export class ApiService { }); } + protected async cleanSession(data: any) { + await this.db.purge(); + await new Promise((res, rej) => { + this.stat().subscribe({ + next: res, + error: rej, + }); + }); + + const sessionKV = await this.db.getKeyValue('session_data'); + sessionKV.data = data; + await sessionKV.save(); + + await this.getDeviceToken(); + } + public saveSessionData(data: any): Promise { return new Promise(async (res, rej) => { // Update the local session data