Add logic for fetching device tokens and using them to resume session
This commit is contained in:
parent
294b312641
commit
2ab5c7a6f5
@ -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;
|
||||
|
@ -158,6 +158,46 @@ export class ApiService {
|
||||
return `${this.baseEndpoint.endsWith('/') ? this.baseEndpoint.slice(0, -1) : this.baseEndpoint}${endpoint}`;
|
||||
}
|
||||
|
||||
public getDeviceToken(): Promise<string> {
|
||||
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<void> {
|
||||
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<any[]> {
|
||||
return new Promise(async (res, rej) => {
|
||||
await this.db.createSchemata();
|
||||
@ -193,6 +233,7 @@ export class ApiService {
|
||||
public getSessionData(): Promise<any> {
|
||||
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<void> {
|
||||
return new Promise(async (res, rej) => {
|
||||
// Update the local session data
|
||||
|
Loading…
Reference in New Issue
Block a user