Previously we had to create a separate API key to give access to chrome extension so we can make calls to the DB. This PR includes logic to initiate a oauth flow with PKCE method which redirects to the `Authorise` screen to give access to server tokens. Implemented in this PR- 1. make `redirectUrl` a non-nullable parameter 2. Add `NODE_ENV` to environment variable service 3. new env variable `CHROME_EXTENSION_REDIRECT_URL` on server side 4. strict checks for redirectUrl 5. try catch blocks on utils db query methods 6. refactor Apollo Client to handle `unauthorized` condition 7. input field to enter server url (for self-hosting) 8. state to show user if its already connected 9. show error if oauth flow is cancelled by user Follow up PR - Renew token logic --------- Co-authored-by: Félix Malfait <felix@twenty.com>
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import {
|
|
CompanyInput,
|
|
CreateCompanyResponse,
|
|
FindCompanyResponse,
|
|
} from '~/db/types/company.types';
|
|
import { Company, CompanyFilterInput } from '~/generated/graphql';
|
|
import { CREATE_COMPANY } from '~/graphql/company/mutations';
|
|
import { FIND_COMPANY } from '~/graphql/company/queries';
|
|
import { isDefined } from '~/utils/isDefined';
|
|
|
|
import { callMutation, callQuery } from '../utils/requestDb';
|
|
|
|
export const fetchCompany = async (
|
|
companyfilerInput: CompanyFilterInput,
|
|
): Promise<Company | null> => {
|
|
const data = await callQuery<FindCompanyResponse>(FIND_COMPANY, {
|
|
filter: {
|
|
...companyfilerInput,
|
|
},
|
|
});
|
|
if (isDefined(data?.companies.edges)) {
|
|
return data.companies.edges.length > 0
|
|
? isDefined(data.companies.edges[0].node)
|
|
? data.companies.edges[0].node
|
|
: null
|
|
: null;
|
|
}
|
|
return null;
|
|
};
|
|
|
|
export const createCompany = async (
|
|
company: CompanyInput,
|
|
): Promise<string | null> => {
|
|
const data = await callMutation<CreateCompanyResponse>(CREATE_COMPANY, {
|
|
input: company,
|
|
});
|
|
if (isDefined(data)) {
|
|
return data.createCompany.id;
|
|
}
|
|
return null;
|
|
};
|