2024-02-13 17:07:37 -05:00
|
|
|
import { finishOAuth, linkOAuthAccount, startOAuth, unlinkOAuthAccount } from '@immich/sdk';
|
2024-02-14 08:09:49 -05:00
|
|
|
import { type UserResponseDto } from '@immich/sdk/axios';
|
2024-02-13 17:07:37 -05:00
|
|
|
import type { AxiosError } from 'axios';
|
2023-08-25 19:44:52 +02:00
|
|
|
import {
|
|
|
|
|
NotificationType,
|
2024-02-13 17:07:37 -05:00
|
|
|
notificationController,
|
2023-08-25 19:44:52 +02:00
|
|
|
} from '../lib/components/shared-components/notification/notification';
|
|
|
|
|
import { handleError } from '../lib/utils/handle-error';
|
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');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-03 21:54:48 -05:00
|
|
|
export const makeSharedLinkUrl = (externalDomain: string, key: string) => {
|
2024-01-09 23:10:06 -05:00
|
|
|
let url = externalDomain || window.location.origin;
|
|
|
|
|
if (!url.endsWith('/')) {
|
|
|
|
|
url += '/';
|
|
|
|
|
}
|
|
|
|
|
return `${url}share/${key}`;
|
2024-01-03 21:54:48 -05:00
|
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
|
},
|
2023-09-01 07:08:42 -04:00
|
|
|
authorize: async (location: Location) => {
|
|
|
|
|
try {
|
|
|
|
|
const redirectUri = location.href.split('?')[0];
|
2024-02-13 17:07:37 -05:00
|
|
|
const { url } = await startOAuth({ oAuthConfigDto: { redirectUri } });
|
|
|
|
|
window.location.href = url;
|
2024-02-02 06:27:54 +01:00
|
|
|
return true;
|
2023-09-01 07:08:42 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
handleError(error, 'Unable to login with OAuth');
|
2024-02-02 06:27:54 +01:00
|
|
|
return false;
|
2023-09-01 07:08:42 -04:00
|
|
|
}
|
|
|
|
|
},
|
2023-07-01 00:50:47 -04:00
|
|
|
login: (location: Location) => {
|
2024-02-13 17:07:37 -05:00
|
|
|
return finishOAuth({ oAuthCallbackDto: { url: location.href } });
|
2023-07-01 00:50:47 -04:00
|
|
|
},
|
2024-02-13 17:07:37 -05:00
|
|
|
link: (location: Location): Promise<UserResponseDto> => {
|
|
|
|
|
return linkOAuthAccount({ oAuthCallbackDto: { url: location.href } });
|
2023-07-01 00:50:47 -04:00
|
|
|
},
|
|
|
|
|
unlink: () => {
|
2024-02-13 17:07:37 -05:00
|
|
|
return unlinkOAuthAccount();
|
2023-07-01 00:50:47 -04:00
|
|
|
},
|
2022-12-26 10:35:52 -05:00
|
|
|
};
|