feat: oauth for chrome extension (#4870)
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>
This commit is contained in:
@ -1,31 +1,36 @@
|
||||
import { OperationVariables } from '@apollo/client';
|
||||
import { isUndefined } from '@sniptt/guards';
|
||||
import { DocumentNode } from 'graphql';
|
||||
|
||||
import getApolloClient from '~/utils/apolloClient';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
|
||||
export const callQuery = async <T>(
|
||||
query: DocumentNode,
|
||||
variables?: OperationVariables,
|
||||
): Promise<T | null> => {
|
||||
const client = await getApolloClient();
|
||||
try {
|
||||
const client = await getApolloClient();
|
||||
const { data } = await client.query<T>({ query, variables });
|
||||
|
||||
const { data, error } = await client.query<T>({ query, variables });
|
||||
|
||||
if (!isUndefined(error)) throw new Error(error.message);
|
||||
|
||||
return data ?? null;
|
||||
if (isDefined(data)) return data;
|
||||
else return null;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const callMutation = async <T>(
|
||||
mutation: DocumentNode,
|
||||
variables?: OperationVariables,
|
||||
): Promise<T | null> => {
|
||||
const client = await getApolloClient();
|
||||
try {
|
||||
const client = await getApolloClient();
|
||||
|
||||
const { data, errors } = await client.mutate<T>({ mutation, variables });
|
||||
const { data } = await client.mutate<T>({ mutation, variables });
|
||||
|
||||
if (!isUndefined(errors)) throw new Error(errors[0].message);
|
||||
|
||||
return data ?? null;
|
||||
if (isDefined(data)) return data;
|
||||
else return null;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user