mirror of
https://github.com/freqtrade/frequi.git
synced 2024-11-23 11:35:14 +00:00
Update userService
This commit is contained in:
parent
f3dbffd669
commit
b2ba9638e2
|
@ -1,30 +1,52 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
import { AuthPayload } from '@/types';
|
import { AuthPayload, AuthStorage } from '@/types';
|
||||||
|
|
||||||
const AUTH_REFRESH_TOKEN = 'auth_ref_token';
|
const AUTH_LOGIN_INFO = 'auth_login_info';
|
||||||
const AUTH_ACCESS_TOKEN = 'auth_access_token';
|
|
||||||
const AUTH_API_URL = 'auth_api_url';
|
|
||||||
const APIBASE = '/api/v1';
|
const APIBASE = '/api/v1';
|
||||||
|
|
||||||
export class UserService {
|
export class UserService {
|
||||||
private setAPIUrl(apiurl: string): void {
|
private storeLoginInfo(loginInfo: AuthStorage): void {
|
||||||
localStorage.setItem(AUTH_API_URL, JSON.stringify(apiurl));
|
localStorage.setItem(AUTH_LOGIN_INFO, JSON.stringify(loginInfo));
|
||||||
}
|
}
|
||||||
|
|
||||||
private setAccessToken(token: string): void {
|
private setAccessToken(token: string): void {
|
||||||
localStorage.setItem(AUTH_ACCESS_TOKEN, JSON.stringify(token));
|
const loginInfo = this.getLoginInfo();
|
||||||
|
loginInfo.accessToken = token;
|
||||||
|
this.storeLoginInfo(loginInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
private setRefreshTokens(refreshToken: string): void {
|
private getLoginInfo(): AuthStorage {
|
||||||
localStorage.setItem(AUTH_REFRESH_TOKEN, JSON.stringify(refreshToken));
|
const info = JSON.parse(localStorage.getItem(AUTH_LOGIN_INFO) || '{}');
|
||||||
|
if ('apiUrl' in info && 'refreshToken' in info) {
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
apiUrl: '',
|
||||||
|
refreshToken: '',
|
||||||
|
accessToken: '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAccessToken(): string {
|
||||||
|
return this.getLoginInfo().accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getRefreshToken() {
|
||||||
|
return this.getLoginInfo().refreshToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public loggedIn() {
|
||||||
|
return this.getLoginInfo().refreshToken !== '';
|
||||||
|
}
|
||||||
|
|
||||||
|
private getAPIUrl(): string {
|
||||||
|
return this.getLoginInfo().apiUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public logout(): void {
|
public logout(): void {
|
||||||
console.log('Logging out');
|
console.log('Logging out');
|
||||||
localStorage.removeItem(AUTH_REFRESH_TOKEN);
|
localStorage.removeItem(AUTH_LOGIN_INFO);
|
||||||
localStorage.removeItem(AUTH_ACCESS_TOKEN);
|
|
||||||
localStorage.removeItem(AUTH_API_URL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async login(auth: AuthPayload) {
|
public async login(auth: AuthPayload) {
|
||||||
|
@ -36,19 +58,19 @@ export class UserService {
|
||||||
auth: { ...auth },
|
auth: { ...auth },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
console.log(result.data);
|
if (result.data.access_token && result.data.refresh_token) {
|
||||||
if (result.data.access_token) {
|
const obj: AuthStorage = {
|
||||||
this.setAPIUrl(auth.url);
|
apiUrl: auth.url,
|
||||||
this.setAccessToken(result.data.access_token);
|
accessToken: result.data.access_token || '',
|
||||||
}
|
refreshToken: result.data.refresh_token || '',
|
||||||
if (result.data.refresh_token) {
|
};
|
||||||
this.setRefreshTokens(result.data.refresh_token);
|
this.storeLoginInfo(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public refreshToken(): Promise<string> {
|
public refreshToken(): Promise<string> {
|
||||||
console.log('Refreshing token...');
|
console.log('Refreshing token...');
|
||||||
const token = JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}');
|
const token = this.getRefreshToken();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
axios
|
axios
|
||||||
.post(
|
.post(
|
||||||
|
@ -78,15 +100,6 @@ export class UserService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public loggedIn() {
|
|
||||||
return localStorage.getItem(AUTH_ACCESS_TOKEN) !== null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private getAPIUrl(): string {
|
|
||||||
const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}');
|
|
||||||
return typeof apiUrl === 'object' ? '' : apiUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getBaseUrl(): string {
|
public getBaseUrl(): string {
|
||||||
const baseURL = this.getAPIUrl();
|
const baseURL = this.getAPIUrl();
|
||||||
if (baseURL === null) {
|
if (baseURL === null) {
|
||||||
|
@ -99,17 +112,40 @@ export class UserService {
|
||||||
return `${baseURL}${APIBASE}`;
|
return `${baseURL}${APIBASE}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAccessToken(): string {
|
/**
|
||||||
return JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}');
|
* Call on startup to migrate old login info to new login
|
||||||
}
|
*/
|
||||||
|
public migrateLogin() {
|
||||||
|
const AUTH_REFRESH_TOKEN = 'auth_ref_token'; // Legacy key - do not use
|
||||||
|
const AUTH_ACCESS_TOKEN = 'auth_access_token';
|
||||||
|
const AUTH_API_URL = 'auth_api_url';
|
||||||
|
|
||||||
getRefreshToken() {
|
const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}');
|
||||||
return JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}');
|
const refreshToken = JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}');
|
||||||
|
const accessToken = JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}');
|
||||||
|
if (
|
||||||
|
typeof apiUrl === 'string' &&
|
||||||
|
typeof refreshToken === 'string' &&
|
||||||
|
typeof accessToken === 'string'
|
||||||
|
) {
|
||||||
|
const loginInfo: AuthStorage = {
|
||||||
|
apiUrl,
|
||||||
|
refreshToken,
|
||||||
|
accessToken,
|
||||||
|
};
|
||||||
|
this.storeLoginInfo(loginInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage.removeItem(AUTH_REFRESH_TOKEN);
|
||||||
|
localStorage.removeItem(AUTH_ACCESS_TOKEN);
|
||||||
|
localStorage.removeItem(AUTH_API_URL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useUserService() {
|
export function useUserService() {
|
||||||
return new UserService();
|
const userservice = new UserService();
|
||||||
|
userservice.migrateLogin();
|
||||||
|
return userservice;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useUserService();
|
export default useUserService();
|
||||||
|
|
|
@ -3,3 +3,9 @@ export interface AuthPayload {
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AuthStorage {
|
||||||
|
apiUrl: string;
|
||||||
|
refreshToken: string;
|
||||||
|
accessToken: string;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user