2023-06-02 15:55:08 +02:00
|
|
|
import type { AxiosError, AxiosPromise } from 'axios';
|
2023-08-25 19:44:52 +02:00
|
|
|
import {
|
|
|
|
|
notificationController,
|
|
|
|
|
NotificationType,
|
|
|
|
|
} from '../lib/components/shared-components/notification/notification';
|
|
|
|
|
import { handleError } from '../lib/utils/handle-error';
|
2022-12-26 10:35:52 -05:00
|
|
|
import { api } from './api';
|
2023-06-02 15:55:08 +02:00
|
|
|
import type { UserResponseDto } from './open-api';
|
2022-12-26 10:35:52 -05:00
|
|
|
|
|
|
|
|
export type ApiError = AxiosError<{ message: string }>;
|
|
|
|
|
|
2023-08-25 19:44:52 +02:00
|
|
|
export const copyToClipboard = async (secret: string) => {
|
|
|
|
|
try {
|
|
|
|
|
await navigator.clipboard.writeText(secret);
|
|
|
|
|
notificationController.show({ message: 'Copied to clipboard!', type: NotificationType.Info });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
handleError(error, 'Cannot copy to clipboard, make sure you are accessing the page through https');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-26 10:35:52 -05:00
|
|
|
export const oauth = {
|
2023-07-01 00:50:47 -04:00
|
|
|
isCallback: (location: Location) => {
|
|
|
|
|
const search = location.search;
|
|
|
|
|
return search.includes('code=') || search.includes('error=');
|
|
|
|
|
},
|
|
|
|
|
isAutoLaunchDisabled: (location: Location) => {
|
|
|
|
|
const values = ['autoLaunch=0', 'password=1', 'password=true'];
|
|
|
|
|
for (const value of values) {
|
|
|
|
|
if (location.search.includes(value)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
getConfig: (location: Location) => {
|
|
|
|
|
const redirectUri = location.href.split('?')[0];
|
|
|
|
|
console.log(`OAuth Redirect URI: ${redirectUri}`);
|
|
|
|
|
return api.oauthApi.generateConfig({ oAuthConfigDto: { redirectUri } });
|
|
|
|
|
},
|
|
|
|
|
login: (location: Location) => {
|
|
|
|
|
return api.oauthApi.callback({ oAuthCallbackDto: { url: location.href } });
|
|
|
|
|
},
|
|
|
|
|
link: (location: Location): AxiosPromise<UserResponseDto> => {
|
|
|
|
|
return api.oauthApi.link({ oAuthCallbackDto: { url: location.href } });
|
|
|
|
|
},
|
|
|
|
|
unlink: () => {
|
|
|
|
|
return api.oauthApi.unlink();
|
|
|
|
|
},
|
2022-12-26 10:35:52 -05:00
|
|
|
};
|