mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 04:01:58 +00:00
handle unuath response from patchdb ws, clean up old files, better setting of auth state
This commit is contained in:
committed by
Aiden McClelland
parent
446da8fd81
commit
c3037d6251
@@ -9,7 +9,6 @@ import { Emver } from './services/emver.service'
|
||||
import { SplitPaneTracker } from './services/split-pane.service'
|
||||
import { ToastButton } from '@ionic/core'
|
||||
import { PatchDbService } from './services/patch-db/patch-db.service'
|
||||
import { HttpService } from './services/http.service'
|
||||
import { ServerStatus } from './services/patch-db/data-model'
|
||||
import { ConnectionFailure, ConnectionService } from './services/connection.service'
|
||||
import { StartupAlertsService } from './services/startup-alerts.service'
|
||||
@@ -69,7 +68,6 @@ export class AppComponent {
|
||||
private readonly authService: AuthService,
|
||||
private readonly router: Router,
|
||||
private readonly embassyApi: ApiService,
|
||||
private readonly http: HttpService,
|
||||
private readonly alertCtrl: AlertController,
|
||||
private readonly emver: Emver,
|
||||
private readonly connectionService: ConnectionService,
|
||||
@@ -141,10 +139,6 @@ export class AppComponent {
|
||||
this.router.navigate(['/login'], { replaceUrl: true })
|
||||
}
|
||||
})
|
||||
|
||||
this.http.watchUnauth$().subscribe(() => {
|
||||
this.authService.setUnverified()
|
||||
})
|
||||
}
|
||||
|
||||
async goToWebsite (): Promise<void> {
|
||||
|
||||
@@ -20,6 +20,7 @@ import { LocalStorageBootstrap } from './services/patch-db/local-storage-bootstr
|
||||
import { SharingModule } from './modules/sharing.module'
|
||||
import { FormBuilder } from '@angular/forms'
|
||||
import { GenericInputComponentModule } from './modals/generic-input/generic-input.component.module'
|
||||
import { AuthService } from './services/auth.service'
|
||||
|
||||
@NgModule({
|
||||
declarations: [AppComponent],
|
||||
@@ -46,8 +47,16 @@ import { GenericInputComponentModule } from './modals/generic-input/generic-inpu
|
||||
IonNav,
|
||||
Storage,
|
||||
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
|
||||
{ provide: ApiService , useFactory: ApiServiceFactory, deps: [ConfigService, HttpService] }, { provide: ApiService , useFactory: ApiServiceFactory, deps: [ConfigService, HttpService] },
|
||||
{ provide: PatchDbService, useFactory: PatchDbServiceFactory, deps: [ConfigService, LocalStorageBootstrap, ApiService] },
|
||||
{
|
||||
provide: ApiService,
|
||||
useFactory: ApiServiceFactory,
|
||||
deps: [ConfigService, HttpService],
|
||||
},
|
||||
{
|
||||
provide: PatchDbService,
|
||||
useFactory: PatchDbServiceFactory,
|
||||
deps: [ConfigService, ApiService, LocalStorageBootstrap, AuthService],
|
||||
},
|
||||
],
|
||||
bootstrap: [AppComponent],
|
||||
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Component } from '@angular/core'
|
||||
import { LoadingController } from '@ionic/angular'
|
||||
import { LoadingController, getPlatforms } from '@ionic/angular'
|
||||
import { Subscription } from 'rxjs'
|
||||
import { ApiService } from 'src/app/services/api/embassy-api.service'
|
||||
import { AuthService } from 'src/app/services/auth.service'
|
||||
|
||||
@Component({
|
||||
@@ -18,6 +19,7 @@ export class LoginPage {
|
||||
constructor (
|
||||
private readonly authService: AuthService,
|
||||
private readonly loadingCtrl: LoadingController,
|
||||
private readonly api: ApiService,
|
||||
) { }
|
||||
|
||||
ngOnDestroy () {
|
||||
@@ -45,7 +47,11 @@ export class LoginPage {
|
||||
await this.loader.present()
|
||||
|
||||
try {
|
||||
await this.authService.login(this.password)
|
||||
await this.api.login({
|
||||
password: this.password,
|
||||
metadata: { platforms: getPlatforms() },
|
||||
})
|
||||
this.authService.setVerified()
|
||||
this.password = ''
|
||||
} catch (e) {
|
||||
this.error = e.code === 34 ? 'Invalid Password' : e.message
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { BehaviorSubject, Observable } from 'rxjs'
|
||||
import { distinctUntilChanged } from 'rxjs/operators'
|
||||
import { ApiService } from './api/embassy-api.service'
|
||||
import { Storage } from '@ionic/storage-angular'
|
||||
import { getPlatforms, isPlatform } from '@ionic/angular'
|
||||
|
||||
export enum AuthState {
|
||||
UNVERIFIED,
|
||||
@@ -18,7 +16,6 @@ export class AuthService {
|
||||
private readonly authState$: BehaviorSubject<AuthState> = new BehaviorSubject(AuthState.INITIALIZING)
|
||||
|
||||
constructor (
|
||||
private readonly embassyApi: ApiService,
|
||||
private readonly storage: Storage,
|
||||
) { }
|
||||
|
||||
@@ -31,11 +28,7 @@ export class AuthService {
|
||||
return this.authState$.pipe(distinctUntilChanged())
|
||||
}
|
||||
|
||||
async login (password: string): Promise<void> {
|
||||
await this.embassyApi.login({
|
||||
password,
|
||||
metadata: { platforms: getPlatforms() },
|
||||
})
|
||||
async setVerified (): Promise<void> {
|
||||
await this.storage.set(this.LOGGED_IN_KEY, true)
|
||||
this.authState$.next(AuthState.VERIFIED)
|
||||
}
|
||||
|
||||
@@ -1,29 +1,26 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http'
|
||||
import { Observable, from, interval, race, Subject } from 'rxjs'
|
||||
import { Observable, from, interval, race } from 'rxjs'
|
||||
import { map, take } from 'rxjs/operators'
|
||||
import { ConfigService } from './config.service'
|
||||
import { Revision } from 'patch-db-client'
|
||||
import { AuthService } from './auth.service'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class HttpService {
|
||||
private unauthorizedApiResponse$ = new Subject()
|
||||
fullUrl: string
|
||||
|
||||
constructor (
|
||||
private readonly http: HttpClient,
|
||||
private readonly config: ConfigService,
|
||||
private readonly auth: AuthService,
|
||||
) {
|
||||
const port = config.mocks.enabled ? this.config.mocks.rpcPort : window.location.port
|
||||
this.fullUrl = `${window.location.protocol}//${window.location.hostname}:${port}`
|
||||
}
|
||||
|
||||
watchUnauth$ (): Observable<{ }> {
|
||||
return this.unauthorizedApiResponse$.asObservable()
|
||||
}
|
||||
|
||||
async rpcRequest<T> (rpcOpts: RPCOptions): Promise<T> {
|
||||
const { url, version } = this.config.api
|
||||
rpcOpts.params = rpcOpts.params || { }
|
||||
@@ -37,7 +34,7 @@ export class HttpService {
|
||||
const res = await this.httpRequest<RPCResponse<T>>(httpOpts)
|
||||
|
||||
if (isRpcError(res)) {
|
||||
if (res.error.code === 34) this.unauthorizedApiResponse$.next(true)
|
||||
if (res.error.code === 34) this.auth.setUnverified()
|
||||
throw new RpcError(res.error)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,11 +4,13 @@ import { DataModel } from './data-model'
|
||||
import { LocalStorageBootstrap } from './local-storage-bootstrap'
|
||||
import { PatchDbService } from './patch-db.service'
|
||||
import { ApiService } from 'src/app/services/api/embassy-api.service'
|
||||
import { AuthService } from '../auth.service'
|
||||
|
||||
export function PatchDbServiceFactory (
|
||||
config: ConfigService,
|
||||
bootstrapper: LocalStorageBootstrap,
|
||||
embassyApi: ApiService,
|
||||
bootstrapper: LocalStorageBootstrap,
|
||||
auth: AuthService,
|
||||
): PatchDbService {
|
||||
|
||||
const { mocks, patchDb: { poll }, supportsWebSockets } = config
|
||||
@@ -31,5 +33,5 @@ export function PatchDbServiceFactory (
|
||||
}
|
||||
}
|
||||
|
||||
return new PatchDbService(source, embassyApi, bootstrapper)
|
||||
return new PatchDbService(source, embassyApi, bootstrapper, auth)
|
||||
}
|
||||
@@ -4,11 +4,13 @@ import { BehaviorSubject, Observable, of, Subscription } from 'rxjs'
|
||||
import { catchError, debounceTime, finalize, map, tap } from 'rxjs/operators'
|
||||
import { pauseFor } from 'src/app/util/misc.util'
|
||||
import { ApiService } from '../api/embassy-api.service'
|
||||
import { AuthService } from '../auth.service'
|
||||
import { DataModel } from './data-model'
|
||||
|
||||
export const PATCH_HTTP = new InjectionToken<Source<DataModel>>('app.config')
|
||||
export const PATCH_SOURCE = new InjectionToken<Source<DataModel>>('app.config')
|
||||
export const BOOTSTRAPPER = new InjectionToken<Bootstrapper<DataModel>>('app.config')
|
||||
export const PATCH_HTTP = new InjectionToken<Source<DataModel>>('')
|
||||
export const PATCH_SOURCE = new InjectionToken<Source<DataModel>>('')
|
||||
export const BOOTSTRAPPER = new InjectionToken<Bootstrapper<DataModel>>('')
|
||||
export const AUTH = new InjectionToken<AuthService>('')
|
||||
|
||||
export enum PatchConnection {
|
||||
Initializing = 'initializing',
|
||||
@@ -31,6 +33,7 @@ export class PatchDbService {
|
||||
@Inject(PATCH_SOURCE) private readonly source: Source<DataModel>,
|
||||
@Inject(PATCH_HTTP) private readonly http: ApiService,
|
||||
@Inject(BOOTSTRAPPER) private readonly bootstrapper: Bootstrapper<DataModel>,
|
||||
@Inject(AUTH) private readonly auth: AuthService,
|
||||
) { }
|
||||
|
||||
async init (): Promise<void> {
|
||||
@@ -60,8 +63,12 @@ export class PatchDbService {
|
||||
error: async e => {
|
||||
console.error('patch-db SYNC ERROR', e)
|
||||
this.patchConnection$.next(PatchConnection.Disconnected)
|
||||
await pauseFor(4000)
|
||||
this.start()
|
||||
if (e.code === 34) {
|
||||
this.auth.setUnverified()
|
||||
} else {
|
||||
await pauseFor(4000)
|
||||
this.start()
|
||||
}
|
||||
},
|
||||
complete: () => {
|
||||
console.warn('patch-db SYNC COMPLETE')
|
||||
|
||||
Reference in New Issue
Block a user