added user key store
This commit is contained in:
parent
19d1d8982b
commit
c3d8f5377d
18
src/service/http_service.ts
Normal file
18
src/service/http_service.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
export const serverAdress = "http://localhost:5000"
|
||||||
|
export const wsServerAdress = "ws://localhost:5000"
|
||||||
|
|
||||||
|
interface HttpService {
|
||||||
|
get: (route: string, succes: () => any, error: () => any) => any
|
||||||
|
}
|
||||||
|
|
||||||
|
class HttpServiceImpl implements HttpService{
|
||||||
|
public serverAdress : string
|
||||||
|
|
||||||
|
constructor(serverAdress : string){
|
||||||
|
this.serverAdress = serverAdress
|
||||||
|
}
|
||||||
|
|
||||||
|
public get(route: string, succes: () => any, error: () => any) : any {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
51
src/store/key_store.ts
Normal file
51
src/store/key_store.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import { serverAdress } from "../service/http_service"
|
||||||
|
|
||||||
|
const key = "user_key"
|
||||||
|
|
||||||
|
export type OnUserKeyFound = (userKey: string) => any
|
||||||
|
|
||||||
|
export interface UserKeyStore {
|
||||||
|
getUserKey: (callback: OnUserKeyFound) => any
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UserKeyStoreImpl implements UserKeyStore {
|
||||||
|
private keyStoreHttp = new UserKeyStoreLocalHttp()
|
||||||
|
private keyStoreLocalStoreage = new UserKeyStoreLocalStoreage()
|
||||||
|
|
||||||
|
public getUserKey(callback: (userKey: string) => any): any {
|
||||||
|
this.keyStoreLocalStoreage.getUserKey((userKey)=>{
|
||||||
|
if(userKey){
|
||||||
|
callback(userKey)
|
||||||
|
}else{
|
||||||
|
this.keyStoreHttp.getUserKey((userKey)=>{
|
||||||
|
this.keyStoreLocalStoreage.storeUserKey(userKey)
|
||||||
|
callback(userKey)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UserKeyStoreLocalHttp implements UserKeyStore {
|
||||||
|
public getUserKey(callback: (userKey: string) => any): any {
|
||||||
|
const url = serverAdress + "/register/"
|
||||||
|
const requestOptions = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
};
|
||||||
|
fetch(url, requestOptions)
|
||||||
|
.then(response => response.text())
|
||||||
|
.then(data => callback(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UserKeyStoreLocalStoreage implements UserKeyStore {
|
||||||
|
public getUserKey(callback: (userKey: string) => any): any {
|
||||||
|
const userKey = localStorage.getItem(key)
|
||||||
|
callback(userKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
public storeUserKey(userKey : string) : any {
|
||||||
|
localStorage.setItem(key, userKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user