fix(auth): add captcha auto-refresh via ApolloLink (#12758)
- Introduced `createCaptchaRefreshLink` to trigger captcha token refresh automatically. - Removed redundant manual captcha refresh calls and integrated it into Apollo Provider.
This commit is contained in:
@ -1,10 +1,17 @@
|
||||
import { ApolloProvider as ApolloProviderBase } from '@apollo/client';
|
||||
|
||||
import { useApolloFactory } from '@/apollo/hooks/useApolloFactory';
|
||||
import { useRequestFreshCaptchaToken } from '@/captcha/hooks/useRequestFreshCaptchaToken';
|
||||
import { createCaptchaRefreshLink } from '@/apollo/utils/captchaRefreshLink';
|
||||
|
||||
export const ApolloProvider = ({ children }: React.PropsWithChildren) => {
|
||||
const { requestFreshCaptchaToken } = useRequestFreshCaptchaToken();
|
||||
|
||||
const captchaRefreshLink = createCaptchaRefreshLink(requestFreshCaptchaToken);
|
||||
|
||||
const apolloClient = useApolloFactory({
|
||||
connectToDevTools: true,
|
||||
extraLinks: [captchaRefreshLink],
|
||||
});
|
||||
|
||||
// This will attach the right apollo client to Apollo Dev Tools
|
||||
|
||||
@ -0,0 +1,19 @@
|
||||
import { ApolloLink } from '@apollo/client';
|
||||
|
||||
export const createCaptchaRefreshLink = (
|
||||
requestFreshCaptchaToken: () => void,
|
||||
) => {
|
||||
return new ApolloLink((operation, forward) => {
|
||||
const { variables } = operation;
|
||||
|
||||
const hasCaptchaToken = variables && 'captchaToken' in variables;
|
||||
|
||||
return forward(operation).map((response) => {
|
||||
if (hasCaptchaToken) {
|
||||
requestFreshCaptchaToken();
|
||||
}
|
||||
|
||||
return response;
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user