-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
231 additions
and
143 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useEffect } from "react"; | ||
import qString from "query-string"; | ||
import API from "../api/api"; | ||
import { LoadingSpinner } from "../components/LoadingSpinner.jsx"; | ||
|
||
/** | ||
* @param {string} props.app - The name of the app being authorized | ||
* @param {string} props.friggBaseUrl - The base URL for the Frigg service | ||
* @param {string} props.authToken - JWT token for authenticated user in Frigg | ||
* @param {string} props.primaryEntityName - The name of the primary entity in the app | ||
* @returns {JSX.Element} The rendered component | ||
* @constructor | ||
*/ | ||
const RedirectFromAuth = (props) => { | ||
useEffect(() => { | ||
const handleAuth = async () => { | ||
const api = new API(props.friggBaseUrl, props.authToken); | ||
const params = qString.parse(window.location.search); | ||
|
||
if (params.code) { | ||
const integrations = await api.listIntegrations(); | ||
const targetEntity = await api.authorize(props.app, { | ||
code: params.code, | ||
}); | ||
|
||
if (targetEntity?.error) { | ||
alert(targetEntity.error); | ||
window.location.href = "/integrations"; | ||
return; | ||
} | ||
|
||
const config = { | ||
type: props.app, | ||
category: "CRM", | ||
}; | ||
|
||
const primaryEntity = integrations.entities.authorized.find( | ||
(entity) => entity.type === props.primaryEntityName | ||
); | ||
|
||
//todo: shouldn't the integration be created only if primary and target entities exist and are different? | ||
//todo2: move the createIntegration function to the integrationList component | ||
const integration = await api.createIntegration( | ||
primaryEntity.id ?? targetEntity.entity_id, | ||
targetEntity.entity_id, | ||
config | ||
); | ||
|
||
if (integration.error) { | ||
alert(integration.error); | ||
return; | ||
} | ||
|
||
window.location.href = "/integrations"; | ||
} | ||
}; | ||
|
||
handleAuth(); | ||
}, [props.app]); | ||
|
||
return ( | ||
<div className="container"> | ||
<div id="card-wrap" className="card"> | ||
<div className="card-body"> | ||
<LoadingSpinner /> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default RedirectFromAuth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.