From 564d024dd1d88124fa626bee5c370f9f8d21a294 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 9 Jan 2018 17:37:50 -0500 Subject: [PATCH] convert user service to jslib --- src/abstractions/index.ts | 1 + src/abstractions/user.service.ts | 12 +++++ src/services/index.ts | 1 + src/services/user.service.ts | 80 ++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/abstractions/user.service.ts create mode 100644 src/services/user.service.ts diff --git a/src/abstractions/index.ts b/src/abstractions/index.ts index b3d6ecb527..b2348f7326 100644 --- a/src/abstractions/index.ts +++ b/src/abstractions/index.ts @@ -5,4 +5,5 @@ export { MessagingService } from './messaging.service'; export { PlatformUtilsService } from './platformUtils.service'; export { StorageService } from './storage.service'; export { TokenService } from './token.service'; +export { UserService } from './user.service'; export { UtilsService } from './utils.service'; diff --git a/src/abstractions/user.service.ts b/src/abstractions/user.service.ts new file mode 100644 index 0000000000..7afbd0b748 --- /dev/null +++ b/src/abstractions/user.service.ts @@ -0,0 +1,12 @@ +export interface UserService { + userId: string; + email: string; + stamp: string; + setUserIdAndEmail(userId: string, email: string): Promise; + setSecurityStamp(stamp: string): Promise; + getUserId(): Promise; + getEmail(): Promise; + getSecurityStamp(): Promise; + clear(): Promise; + isAuthenticated(): Promise; +} diff --git a/src/services/index.ts b/src/services/index.ts index dd4122a826..1ef71d40bb 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -3,4 +3,5 @@ export { AppIdService } from './appId.service'; export { ConstantsService } from './constants.service'; export { CryptoService } from './crypto.service'; export { TokenService } from './token.service'; +export { UserService } from './user.service'; export { UtilsService } from './utils.service'; diff --git a/src/services/user.service.ts b/src/services/user.service.ts new file mode 100644 index 0000000000..aa2b19e179 --- /dev/null +++ b/src/services/user.service.ts @@ -0,0 +1,80 @@ +import { StorageService } from '../abstractions/storage.service'; +import { TokenService } from '../abstractions/token.service'; +import { UserService as UserServiceInterface } from '../abstractions/user.service'; + +const Keys = { + userId: 'userId', + userEmail: 'userEmail', + stamp: 'securityStamp', +}; + +export class UserService implements UserServiceInterface { + userId: string; + email: string; + stamp: string; + + constructor(private tokenService: TokenService, private storageService: StorageService) { + } + + setUserIdAndEmail(userId: string, email: string): Promise { + this.email = email; + this.userId = userId; + + return Promise.all([ + this.storageService.save(Keys.userEmail, email), + this.storageService.save(Keys.userId, userId), + ]); + } + + setSecurityStamp(stamp: string): Promise { + this.stamp = stamp; + return this.storageService.save(Keys.stamp, stamp); + } + + async getUserId(): Promise { + if (this.userId != null) { + return this.userId; + } + + this.userId = await this.storageService.get(Keys.userId); + return this.userId; + } + + async getEmail(): Promise { + if (this.email != null) { + return this.email; + } + + this.email = await this.storageService.get(Keys.userEmail); + return this.email; + } + + async getSecurityStamp(): Promise { + if (this.stamp != null) { + return this.stamp; + } + + this.stamp = await this.storageService.get(Keys.stamp); + return this.stamp; + } + + async clear(): Promise { + await Promise.all([ + this.storageService.remove(Keys.userId), + this.storageService.remove(Keys.userEmail), + this.storageService.remove(Keys.stamp), + ]); + + this.userId = this.email = this.stamp = null; + } + + async isAuthenticated(): Promise { + const token = await this.tokenService.getToken(); + if (token == null) { + return false; + } + + const userId = await this.getUserId(); + return userId != null; + } +}