chore: store authenticator devices labels in preferences instead of server (#2252)
This commit is contained in:
@@ -3,14 +3,17 @@
|
||||
import { AuthenticatorApiServiceInterface } from '@standardnotes/api'
|
||||
import { Username, Uuid } from '@standardnotes/domain-core'
|
||||
import { isErrorResponse } from '@standardnotes/responses'
|
||||
import { PrefKey } from '@standardnotes/models'
|
||||
|
||||
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
import { AuthenticatorClientInterface } from './AuthenticatorClientInterface'
|
||||
import { PreferenceServiceInterface } from '../Preferences/PreferenceServiceInterface'
|
||||
|
||||
export class AuthenticatorManager extends AbstractService implements AuthenticatorClientInterface {
|
||||
constructor(
|
||||
private authenticatorApiService: AuthenticatorApiServiceInterface,
|
||||
private preferencesService: PreferenceServiceInterface,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(internalEventBus)
|
||||
@@ -24,7 +27,18 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
|
||||
return []
|
||||
}
|
||||
|
||||
return result.data.authenticators
|
||||
const authenticatorNames = this.getAuthenticatorNamesFromPreferences()
|
||||
|
||||
const nameDecoratedAuthenticators: { id: string; name: string }[] = result.data.authenticators.map(
|
||||
(authenticator: { id: string }) => ({
|
||||
id: authenticator.id,
|
||||
name: authenticatorNames.has(authenticator.id)
|
||||
? (authenticatorNames.get(authenticator.id) as string)
|
||||
: 'Security Key',
|
||||
}),
|
||||
)
|
||||
|
||||
return nameDecoratedAuthenticators
|
||||
} catch (error) {
|
||||
return []
|
||||
}
|
||||
@@ -37,6 +51,11 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
|
||||
return false
|
||||
}
|
||||
|
||||
const authenticatorNames = this.getAuthenticatorNamesFromPreferences()
|
||||
authenticatorNames.delete(authenticatorId.value)
|
||||
|
||||
await this.preferencesService.setValue(PrefKey.AuthenticatorNames, JSON.stringify([...authenticatorNames]))
|
||||
|
||||
return true
|
||||
} catch (error) {
|
||||
return false
|
||||
@@ -73,7 +92,12 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
|
||||
return false
|
||||
}
|
||||
|
||||
return result.data.success
|
||||
const authenticatorNames = this.getAuthenticatorNamesFromPreferences()
|
||||
authenticatorNames.set(result.data.id, name)
|
||||
|
||||
await this.preferencesService.setValue(PrefKey.AuthenticatorNames, JSON.stringify([...authenticatorNames]))
|
||||
|
||||
return true
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
@@ -92,4 +116,18 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private getAuthenticatorNamesFromPreferences(): Map<string, string> {
|
||||
let authenticatorNames: Map<string, string> = new Map()
|
||||
const authenticatorNamesFromPreferences = this.preferencesService.getValue(PrefKey.AuthenticatorNames)
|
||||
if (authenticatorNamesFromPreferences !== undefined) {
|
||||
try {
|
||||
authenticatorNames = new Map(JSON.parse(authenticatorNamesFromPreferences))
|
||||
} catch (error) {
|
||||
authenticatorNames = new Map()
|
||||
}
|
||||
}
|
||||
|
||||
return authenticatorNames
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user