Skip to content

Commit

Permalink
call stripe logout when logging out of Furever (#103)
Browse files Browse the repository at this point in the history
* add logout

* use context to retrieve connect instance
  • Loading branch information
kaiying-stripe committed Feb 12, 2024
1 parent 00effe0 commit 330786d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions client/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ import {useSession} from '../hooks/SessionProvider';
import {useDisplayShortName} from '../hooks/useDisplayName';
import {OnboardingNotice} from './OnboardingNotice';
import {RouterLink} from './RouterLink';
import {useConnectJSContext} from '../hooks/ConnectJSProvider';

const useLogout = () => {
const {search} = useLocation();
const connectJSContext = useConnectJSContext();

return useMutation<void, Error>('logout', async () => {
await connectJSContext.connectInstance?.logout();
const response = await fetch('/api/logout', {
method: 'POST',
});
Expand Down
27 changes: 27 additions & 0 deletions client/hooks/ConnectJSProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, {createContext, useContext} from 'react';
import {StripeConnectInstance} from '@stripe/connect-js';

type IConnectJSContext = {
connectInstance?: StripeConnectInstance;
};

const ConnectJSContext = createContext<IConnectJSContext>({});

export const useConnectJSContext = () => {
const context = useContext(ConnectJSContext);
return context;
};

export const ConnectJSProvider = ({
children,
connectInstance,
}: {
children: React.ReactNode;
connectInstance?: StripeConnectInstance;
}) => {
return (
<ConnectJSContext.Provider value={{connectInstance}}>
{children}
</ConnectJSContext.Provider>
);
};
5 changes: 4 additions & 1 deletion client/hooks/ConnectJsWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
loadConnectAndInitialize,
} from '@stripe/connect-js';
import {ConnectComponentsProvider} from '@stripe/react-connect-js';
import {ConnectJSProvider} from './ConnectJSProvider';
import {useSession} from './SessionProvider';
import {FullScreenLoading} from '../components/FullScreenLoading';
import {ErrorState} from '../components/ErrorState';
Expand Down Expand Up @@ -107,7 +108,9 @@ export const ConnectJsWrapper = ({children}: {children: React.ReactNode}) => {

return (
<ConnectComponentsProvider connectInstance={connectInstance}>
{children}
<ConnectJSProvider connectInstance={connectInstance}>
{children}
</ConnectJSProvider>
</ConnectComponentsProvider>
);
};

0 comments on commit 330786d

Please sign in to comment.