Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle mode value to avoid signin error caused by 3pcd ✨ #834

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pink-rockets-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@viron/app": major
---

handle mode value to avoid signin error caused by 3pcd.
5 changes: 5 additions & 0 deletions packages/app/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export class EndpointExportError extends EndpointError {
name = 'Endpoint Export Error';
}

export class EndpointOAuthIllegalResponseError extends EndpointError {
code = '#endpointOAuthIllegalResponse';
name = 'Endpoint OAuth Illegal Response Error';
}

export class EndpointGroupError extends BaseError {
code = '#endpointGroup';
name = 'Endpoint Group Error';
Expand Down
31 changes: 30 additions & 1 deletion packages/app/src/hooks/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
constructRequestInit,
constructRequestPayloads,
} from '~/utils/oas';
import { extractAuthorizationUrl } from '~/utils/oas/oauth';

export type UseEndpointReturn = {
list: Endpoint[];
Expand Down Expand Up @@ -535,7 +536,35 @@ export const useEndpoint = (): UseEndpointReturn => {
);
try {
set(KEY.OAUTH_ENDPOINT_ID, endpoint.id);
globalThis.location.href = requestInfo.toString();

if (authConfig.mode !== 'cors') {
globalThis.location.href = requestInfo.toString();
} else {
const requestInit = constructRequestInit(request, requestPayloads);
const [response, responseError] = await promiseErrorHandler(
globalThis.fetch(requestInfo, requestInit)
);
if (!!responseError) {
return {
error: new NetworkError(responseError.message),
};
}
if (!response.ok) {
return {
error: await getHTTPError(response),
};
}
const authorizationUrlResult = extractAuthorizationUrl(
request.operation.responses,
await response.json()
);
if (authorizationUrlResult.isFailure()) {
return {
error: authorizationUrlResult.value,
};
}
globalThis.location.href = authorizationUrlResult.value;
}
} catch (e: unknown) {
remove(KEY.OAUTH_ENDPOINT_ID);
let message = '';
Expand Down
32 changes: 32 additions & 0 deletions packages/app/src/utils/oas/oauth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
Success,
Failure,
EndpointOAuthIllegalResponseError,
Result,
} from '~/errors';
import { Responses } from '~/types/oas';

export const extractAuthorizationUrl = (
responses: Responses,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json: any
): Result<string, EndpointOAuthIllegalResponseError> => {
const response = responses['200'];
if (!response || !response.content?.['application/json']) {
return new Failure(new EndpointOAuthIllegalResponseError());
}
const schema = response.content['application/json'].schema;
if (!schema.properties) {
return new Failure(new EndpointOAuthIllegalResponseError());
}

const uriKey = Object.keys(schema.properties).find((key) => {
const property = schema.properties?.[key];
return property?.type === 'string' && property.format === 'uri';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to document this specification.

});
if (!uriKey) {
return new Failure(new EndpointOAuthIllegalResponseError());
}

return new Success(json[uriKey]);
};