mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-27 17:18:04 +01:00
convert user service to jslib
This commit is contained in:
parent
07e4758dee
commit
564d024dd1
@ -5,4 +5,5 @@ export { MessagingService } from './messaging.service';
|
|||||||
export { PlatformUtilsService } from './platformUtils.service';
|
export { PlatformUtilsService } from './platformUtils.service';
|
||||||
export { StorageService } from './storage.service';
|
export { StorageService } from './storage.service';
|
||||||
export { TokenService } from './token.service';
|
export { TokenService } from './token.service';
|
||||||
|
export { UserService } from './user.service';
|
||||||
export { UtilsService } from './utils.service';
|
export { UtilsService } from './utils.service';
|
||||||
|
12
src/abstractions/user.service.ts
Normal file
12
src/abstractions/user.service.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
export interface UserService {
|
||||||
|
userId: string;
|
||||||
|
email: string;
|
||||||
|
stamp: string;
|
||||||
|
setUserIdAndEmail(userId: string, email: string): Promise<any>;
|
||||||
|
setSecurityStamp(stamp: string): Promise<any>;
|
||||||
|
getUserId(): Promise<string>;
|
||||||
|
getEmail(): Promise<string>;
|
||||||
|
getSecurityStamp(): Promise<string>;
|
||||||
|
clear(): Promise<any>;
|
||||||
|
isAuthenticated(): Promise<boolean>;
|
||||||
|
}
|
@ -3,4 +3,5 @@ export { AppIdService } from './appId.service';
|
|||||||
export { ConstantsService } from './constants.service';
|
export { ConstantsService } from './constants.service';
|
||||||
export { CryptoService } from './crypto.service';
|
export { CryptoService } from './crypto.service';
|
||||||
export { TokenService } from './token.service';
|
export { TokenService } from './token.service';
|
||||||
|
export { UserService } from './user.service';
|
||||||
export { UtilsService } from './utils.service';
|
export { UtilsService } from './utils.service';
|
||||||
|
80
src/services/user.service.ts
Normal file
80
src/services/user.service.ts
Normal file
@ -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<any> {
|
||||||
|
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<any> {
|
||||||
|
this.stamp = stamp;
|
||||||
|
return this.storageService.save(Keys.stamp, stamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUserId(): Promise<string> {
|
||||||
|
if (this.userId != null) {
|
||||||
|
return this.userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.userId = await this.storageService.get<string>(Keys.userId);
|
||||||
|
return this.userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getEmail(): Promise<string> {
|
||||||
|
if (this.email != null) {
|
||||||
|
return this.email;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.email = await this.storageService.get<string>(Keys.userEmail);
|
||||||
|
return this.email;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getSecurityStamp(): Promise<string> {
|
||||||
|
if (this.stamp != null) {
|
||||||
|
return this.stamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.stamp = await this.storageService.get<string>(Keys.stamp);
|
||||||
|
return this.stamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
async clear(): Promise<any> {
|
||||||
|
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<boolean> {
|
||||||
|
const token = await this.tokenService.getToken();
|
||||||
|
if (token == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userId = await this.getUserId();
|
||||||
|
return userId != null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user