feat: add renew token query for apollo client (chrome-extension) (#5200)
fixes - #5203
This commit is contained in:
@ -18,9 +18,3 @@ export const exchangeAuthorizationCode = async (
|
|||||||
return data.exchangeAuthorizationCode;
|
return data.exchangeAuthorizationCode;
|
||||||
else return null;
|
else return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// export const RenewToken = async (appToken: string): Promise<Tokens | null> => {
|
|
||||||
// const data = await callQuery<Tokens>(RENEW_TOKEN, { appToken });
|
|
||||||
// if (isDefined(data)) return data;
|
|
||||||
// else return null;
|
|
||||||
// };
|
|
||||||
|
|||||||
36
packages/twenty-chrome-extension/src/db/token.db.ts
Normal file
36
packages/twenty-chrome-extension/src/db/token.db.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { ApolloClient, InMemoryCache } from '@apollo/client';
|
||||||
|
|
||||||
|
import { Tokens } from '~/db/types/auth.types';
|
||||||
|
import { RENEW_TOKEN } from '~/graphql/auth/mutations';
|
||||||
|
import { isDefined } from '~/utils/isDefined';
|
||||||
|
|
||||||
|
export const renewToken = async (
|
||||||
|
appToken: string,
|
||||||
|
): Promise<{ renewToken: { tokens: Tokens } } | null> => {
|
||||||
|
const store = await chrome.storage.local.get();
|
||||||
|
const serverUrl = `${
|
||||||
|
isDefined(store.serverBaseUrl)
|
||||||
|
? store.serverBaseUrl
|
||||||
|
: import.meta.env.VITE_SERVER_BASE_URL
|
||||||
|
}/graphql`;
|
||||||
|
|
||||||
|
// Create new client to call refresh token graphql mutation
|
||||||
|
const client = new ApolloClient({
|
||||||
|
uri: serverUrl,
|
||||||
|
cache: new InMemoryCache({}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const { data } = await client.mutate({
|
||||||
|
mutation: RENEW_TOKEN,
|
||||||
|
variables: {
|
||||||
|
appToken,
|
||||||
|
},
|
||||||
|
fetchPolicy: 'network-only',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (isDefined(data)) {
|
||||||
|
return data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -26,3 +26,20 @@ export const EXCHANGE_AUTHORIZATION_CODE = gql`
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
export const RENEW_TOKEN = gql`
|
||||||
|
mutation RenewToken($appToken: String!) {
|
||||||
|
renewToken(appToken: $appToken) {
|
||||||
|
tokens {
|
||||||
|
accessToken {
|
||||||
|
token
|
||||||
|
expiresAt
|
||||||
|
}
|
||||||
|
refreshToken {
|
||||||
|
token
|
||||||
|
expiresAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|||||||
@ -1,20 +0,0 @@
|
|||||||
// import { gql } from '@apollo/client';
|
|
||||||
|
|
||||||
// export const RENEW_TOKEN = gql`
|
|
||||||
// query RenewToken($appToken: String!) {
|
|
||||||
// renewToken(appToken: $appToken) {
|
|
||||||
// loginToken {
|
|
||||||
// token
|
|
||||||
// expiresAt
|
|
||||||
// }
|
|
||||||
// accessToken {
|
|
||||||
// token
|
|
||||||
// expiresAt
|
|
||||||
// }
|
|
||||||
// refreshToken {
|
|
||||||
// token
|
|
||||||
// expiresAt
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// `;
|
|
||||||
@ -1,72 +1,121 @@
|
|||||||
import { ApolloClient, from, HttpLink, InMemoryCache } from '@apollo/client';
|
import {
|
||||||
|
ApolloClient,
|
||||||
|
from,
|
||||||
|
fromPromise,
|
||||||
|
HttpLink,
|
||||||
|
InMemoryCache,
|
||||||
|
} from '@apollo/client';
|
||||||
|
import { setContext } from '@apollo/client/link/context';
|
||||||
import { onError } from '@apollo/client/link/error';
|
import { onError } from '@apollo/client/link/error';
|
||||||
|
|
||||||
|
import { renewToken } from '~/db/token.db';
|
||||||
|
import { Tokens } from '~/db/types/auth.types';
|
||||||
import { isDefined } from '~/utils/isDefined';
|
import { isDefined } from '~/utils/isDefined';
|
||||||
|
|
||||||
const clearStore = () => {
|
const clearStore = () => {
|
||||||
chrome.storage.local.remove('loginToken');
|
chrome.storage.local.remove(['loginToken', 'accessToken', 'refreshToken']);
|
||||||
|
|
||||||
chrome.storage.local.remove('accessToken');
|
|
||||||
|
|
||||||
chrome.storage.local.remove('refreshToken');
|
|
||||||
|
|
||||||
chrome.storage.local.set({ isAuthenticated: false });
|
chrome.storage.local.set({ isAuthenticated: false });
|
||||||
};
|
};
|
||||||
|
|
||||||
const getApolloClient = async () => {
|
const setStore = (tokens: Tokens) => {
|
||||||
|
if (isDefined(tokens.loginToken)) {
|
||||||
|
chrome.storage.local.set({
|
||||||
|
loginToken: tokens.loginToken,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
chrome.storage.local.set({
|
||||||
|
accessToken: tokens.accessToken,
|
||||||
|
refreshToken: tokens.refreshToken,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getServerUrl = async () => {
|
||||||
const store = await chrome.storage.local.get();
|
const store = await chrome.storage.local.get();
|
||||||
const serverUrl = `${
|
const serverUrl = `${
|
||||||
isDefined(store.serverBaseUrl)
|
isDefined(store.serverBaseUrl)
|
||||||
? store.serverBaseUrl
|
? store.serverBaseUrl
|
||||||
: import.meta.env.VITE_SERVER_BASE_URL
|
: import.meta.env.VITE_SERVER_BASE_URL
|
||||||
}/graphql`;
|
}/graphql`;
|
||||||
|
return serverUrl;
|
||||||
|
};
|
||||||
|
|
||||||
const errorLink = onError(({ graphQLErrors, networkError }) => {
|
const getAuthToken = async () => {
|
||||||
if (isDefined(graphQLErrors)) {
|
const store = await chrome.storage.local.get();
|
||||||
for (const graphQLError of graphQLErrors) {
|
if (isDefined(store.accessToken)) return `Bearer ${store.accessToken.token}`;
|
||||||
if (graphQLError.message === 'Unauthorized') {
|
else return '';
|
||||||
//TODO: replace this with renewToken mutation
|
};
|
||||||
clearStore();
|
|
||||||
return;
|
const getApolloClient = async () => {
|
||||||
}
|
const store = await chrome.storage.local.get();
|
||||||
switch (graphQLError?.extensions?.code) {
|
|
||||||
case 'UNAUTHENTICATED': {
|
const authLink = setContext(async (_, { headers }) => {
|
||||||
//TODO: replace this with renewToken mutation
|
const token = await getAuthToken();
|
||||||
clearStore();
|
return {
|
||||||
break;
|
headers: {
|
||||||
|
...headers,
|
||||||
|
authorization: token,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
const errorLink = onError(
|
||||||
|
({ graphQLErrors, networkError, forward, operation }) => {
|
||||||
|
if (isDefined(graphQLErrors)) {
|
||||||
|
for (const graphQLError of graphQLErrors) {
|
||||||
|
if (graphQLError.message === 'Unauthorized') {
|
||||||
|
return fromPromise(
|
||||||
|
renewToken(store.refreshToken.token)
|
||||||
|
.then((response) => {
|
||||||
|
if (isDefined(response)) {
|
||||||
|
setStore(response.renewToken.tokens);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
clearStore();
|
||||||
|
}),
|
||||||
|
).flatMap(() => forward(operation));
|
||||||
|
}
|
||||||
|
switch (graphQLError?.extensions?.code) {
|
||||||
|
case 'UNAUTHENTICATED': {
|
||||||
|
return fromPromise(
|
||||||
|
renewToken(store.refreshToken.token)
|
||||||
|
.then((response) => {
|
||||||
|
if (isDefined(response)) {
|
||||||
|
setStore(response.renewToken.tokens);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
clearStore();
|
||||||
|
}),
|
||||||
|
).flatMap(() => forward(operation));
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error(
|
||||||
|
`[GraphQL error]: Message: ${graphQLError.message}, Location: ${
|
||||||
|
graphQLError.locations
|
||||||
|
? JSON.stringify(graphQLError.locations)
|
||||||
|
: graphQLError.locations
|
||||||
|
}, Path: ${graphQLError.path}`,
|
||||||
|
);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.error(
|
|
||||||
`[GraphQL error]: Message: ${graphQLError.message}, Location: ${
|
|
||||||
graphQLError.locations
|
|
||||||
? JSON.stringify(graphQLError.locations)
|
|
||||||
: graphQLError.locations
|
|
||||||
}, Path: ${graphQLError.path}`,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (isDefined(networkError)) {
|
if (isDefined(networkError)) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error(`[Network error]: ${networkError}`);
|
console.error(`[Network error]: ${networkError}`);
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
const httpLink = new HttpLink({
|
const httpLink = new HttpLink({
|
||||||
uri: serverUrl,
|
uri: await getServerUrl(),
|
||||||
headers: isDefined(store.accessToken)
|
|
||||||
? {
|
|
||||||
Authorization: `Bearer ${store.accessToken.token}`,
|
|
||||||
}
|
|
||||||
: {},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const client = new ApolloClient({
|
const client = new ApolloClient({
|
||||||
cache: new InMemoryCache(),
|
cache: new InMemoryCache(),
|
||||||
link: from([errorLink, httpLink]),
|
link: from([errorLink, authLink, httpLink]),
|
||||||
});
|
});
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
|
|||||||
@ -422,8 +422,9 @@ export class TokenService {
|
|||||||
|
|
||||||
assert(token, "This refresh token doesn't exist", NotFoundException);
|
assert(token, "This refresh token doesn't exist", NotFoundException);
|
||||||
|
|
||||||
const user = await this.userRepository.findOneBy({
|
const user = await this.userRepository.findOne({
|
||||||
id: jwtPayload.sub,
|
where: { id: jwtPayload.sub },
|
||||||
|
relations: ['appTokens'],
|
||||||
});
|
});
|
||||||
|
|
||||||
assert(user, 'User not found', NotFoundException);
|
assert(user, 'User not found', NotFoundException);
|
||||||
|
|||||||
@ -32,6 +32,7 @@ export class GraphQLHydrateRequestFromTokenMiddleware
|
|||||||
'SignUp',
|
'SignUp',
|
||||||
'RenewToken',
|
'RenewToken',
|
||||||
'IntrospectionQuery',
|
'IntrospectionQuery',
|
||||||
|
'ExchangeAuthorizationCode',
|
||||||
];
|
];
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in New Issue
Block a user