2020-06-20 14:55:33 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
|
2020-08-29 09:23:39 +00:00
|
|
|
import { AuthPayload } from '@/types';
|
2020-07-17 05:20:26 +00:00
|
|
|
|
2020-07-17 05:09:52 +00:00
|
|
|
const AUTH_REFRESH_TOKEN = 'auth_ref_token';
|
2020-06-20 14:55:33 +00:00
|
|
|
const AUTH_ACCESS_TOKEN = 'auth_access_token';
|
|
|
|
const AUTH_API_URL = 'auth_api_url';
|
2020-06-23 18:38:02 +00:00
|
|
|
const apiBase = '/api/v1';
|
2020-06-20 14:55:33 +00:00
|
|
|
|
|
|
|
export default {
|
2020-06-23 18:38:02 +00:00
|
|
|
apiBase,
|
2020-06-22 18:18:45 +00:00
|
|
|
AUTH_API_URL,
|
2020-07-17 05:09:52 +00:00
|
|
|
setAPIUrl(apiurl: string): void {
|
2020-06-20 14:55:33 +00:00
|
|
|
localStorage.setItem(AUTH_API_URL, JSON.stringify(apiurl));
|
|
|
|
},
|
|
|
|
|
2020-07-17 05:09:52 +00:00
|
|
|
setAccessToken(token: string): void {
|
2020-06-20 14:55:33 +00:00
|
|
|
localStorage.setItem(AUTH_ACCESS_TOKEN, JSON.stringify(token));
|
|
|
|
},
|
|
|
|
|
2020-07-17 05:09:52 +00:00
|
|
|
setRefreshTokens(refreshToken: string): void {
|
|
|
|
localStorage.setItem(AUTH_REFRESH_TOKEN, JSON.stringify(refreshToken));
|
2020-06-20 14:55:33 +00:00
|
|
|
},
|
|
|
|
|
2020-07-17 05:09:52 +00:00
|
|
|
logout(): void {
|
2020-06-20 14:55:33 +00:00
|
|
|
console.log('Logging out');
|
2020-07-17 05:09:52 +00:00
|
|
|
localStorage.removeItem(AUTH_REFRESH_TOKEN);
|
2020-06-20 14:55:33 +00:00
|
|
|
localStorage.removeItem(AUTH_ACCESS_TOKEN);
|
|
|
|
localStorage.removeItem(AUTH_API_URL);
|
|
|
|
},
|
|
|
|
|
2020-07-17 05:20:26 +00:00
|
|
|
async login(auth: AuthPayload) {
|
2020-06-20 14:55:33 +00:00
|
|
|
// Login using username / password
|
2020-06-22 18:18:45 +00:00
|
|
|
const result = await axios.post(
|
|
|
|
`${auth.url}/api/v1/token/login`,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
auth: { ...auth },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
console.log(result.data);
|
|
|
|
this.setAPIUrl(auth.url);
|
|
|
|
if (result.data.access_token) {
|
|
|
|
this.setAccessToken(result.data.access_token);
|
|
|
|
}
|
|
|
|
if (result.data.refresh_token) {
|
|
|
|
this.setRefreshTokens(result.data.refresh_token);
|
|
|
|
}
|
2020-06-20 14:55:33 +00:00
|
|
|
},
|
|
|
|
|
2020-07-17 05:09:52 +00:00
|
|
|
refreshToken(): Promise<string> {
|
2020-06-20 14:55:33 +00:00
|
|
|
console.log('Refreshing token...');
|
2020-07-17 05:09:52 +00:00
|
|
|
const token = JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post(
|
2020-12-26 15:27:10 +00:00
|
|
|
`${this.getAPIUrl()}${apiBase}/token/refresh`,
|
2020-07-17 05:09:52 +00:00
|
|
|
{},
|
|
|
|
{
|
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.then((response) => {
|
|
|
|
if (response.data.access_token) {
|
|
|
|
this.setAccessToken(response.data.access_token);
|
|
|
|
// Return plain access token
|
|
|
|
resolve(response.data.access_token);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
if (err.response && err.response.status === 401) {
|
|
|
|
// in case of errors when using the refresh token - logout.
|
|
|
|
this.logout();
|
|
|
|
} else if (err.response && (err.response.status === 500 || err.response.status === 404)) {
|
|
|
|
console.log('Bot seems to be offline... - retrying later');
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-06-20 14:55:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
loggedIn() {
|
|
|
|
return localStorage.getItem(AUTH_ACCESS_TOKEN) !== null;
|
|
|
|
},
|
|
|
|
|
2020-06-22 18:18:45 +00:00
|
|
|
getAPIUrl(): string {
|
|
|
|
const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}');
|
|
|
|
return typeof apiUrl === 'object' ? '' : apiUrl;
|
2020-06-20 14:55:33 +00:00
|
|
|
},
|
|
|
|
|
2020-07-17 05:09:52 +00:00
|
|
|
getAccessToken(): string {
|
2020-06-20 14:55:33 +00:00
|
|
|
return JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}');
|
|
|
|
},
|
|
|
|
|
|
|
|
getRefreshToken() {
|
2020-07-17 05:09:52 +00:00
|
|
|
return JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}');
|
2020-06-20 14:55:33 +00:00
|
|
|
},
|
|
|
|
};
|