mirror of
https://github.com/freqtrade/frequi.git
synced 2024-11-27 05:25:17 +00:00
Allow authinfo to store multiple bots
This commit is contained in:
parent
9ba4f7a70b
commit
29d908bf47
|
@ -1,6 +1,6 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
import { AuthPayload, AuthStorage } from '@/types';
|
import { AuthPayload, AuthStorage, AuthStorageMulti } from '@/types';
|
||||||
|
|
||||||
const AUTH_LOGIN_INFO = 'auth_login_info';
|
const AUTH_LOGIN_INFO = 'auth_login_info';
|
||||||
const APIBASE = '/api/v1';
|
const APIBASE = '/api/v1';
|
||||||
|
@ -13,8 +13,22 @@ export class UserService {
|
||||||
this.botId = botId;
|
this.botId = botId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores info for current botId in the object of all bots.
|
||||||
|
*/
|
||||||
private storeLoginInfo(loginInfo: AuthStorage): void {
|
private storeLoginInfo(loginInfo: AuthStorage): void {
|
||||||
localStorage.setItem(AUTH_LOGIN_INFO, JSON.stringify(loginInfo));
|
const allInfo = this.getAllLoginInfos();
|
||||||
|
allInfo[this.botId] = loginInfo;
|
||||||
|
localStorage.setItem(AUTH_LOGIN_INFO, JSON.stringify(allInfo));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logout - removing info for this particular bot.
|
||||||
|
*/
|
||||||
|
private removeLoginInfo(): void {
|
||||||
|
const info = this.getAllLoginInfos();
|
||||||
|
delete info[this.botId];
|
||||||
|
localStorage.setItem(AUTH_LOGIN_INFO, JSON.stringify(info));
|
||||||
}
|
}
|
||||||
|
|
||||||
private setAccessToken(token: string): void {
|
private setAccessToken(token: string): void {
|
||||||
|
@ -23,10 +37,23 @@ export class UserService {
|
||||||
this.storeLoginInfo(loginInfo);
|
this.storeLoginInfo(loginInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getLoginInfo(): AuthStorage {
|
/**
|
||||||
|
* Retrieve full logininfo object (for all registered bots)
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
private getAllLoginInfos(): AuthStorageMulti {
|
||||||
const info = JSON.parse(localStorage.getItem(AUTH_LOGIN_INFO) || '{}');
|
const info = JSON.parse(localStorage.getItem(AUTH_LOGIN_INFO) || '{}');
|
||||||
if ('apiUrl' in info && 'refreshToken' in info) {
|
return info;
|
||||||
return info;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve Login info object for the given bot
|
||||||
|
* @returns Login Info object
|
||||||
|
*/
|
||||||
|
private getLoginInfo(): AuthStorage {
|
||||||
|
const info = this.getAllLoginInfos();
|
||||||
|
if (this.botId in info && 'apiUrl' in info[this.botId] && 'refreshToken' in info[this.botId]) {
|
||||||
|
return info[this.botId];
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
apiUrl: '',
|
apiUrl: '',
|
||||||
|
@ -53,7 +80,8 @@ export class UserService {
|
||||||
|
|
||||||
public logout(): void {
|
public logout(): void {
|
||||||
console.log('Logging out');
|
console.log('Logging out');
|
||||||
localStorage.removeItem(AUTH_LOGIN_INFO);
|
|
||||||
|
this.removeLoginInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async login(auth: AuthPayload) {
|
public async login(auth: AuthPayload) {
|
||||||
|
|
|
@ -9,3 +9,7 @@ export interface AuthStorage {
|
||||||
refreshToken: string;
|
refreshToken: string;
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AuthStorageMulti {
|
||||||
|
[key: string]: AuthStorage;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user