mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-24 09:52:49 +08:00
fix(core): let backend check whethere user can subscribe (#8573)
This commit is contained in:
@@ -16,7 +16,11 @@ import {
|
|||||||
RouteLogic,
|
RouteLogic,
|
||||||
useNavigateHelper,
|
useNavigateHelper,
|
||||||
} from '../../components/hooks/use-navigate-helper';
|
} from '../../components/hooks/use-navigate-helper';
|
||||||
import { AuthService, SubscriptionService } from '../../modules/cloud';
|
import {
|
||||||
|
AuthService,
|
||||||
|
BackendError,
|
||||||
|
SubscriptionService,
|
||||||
|
} from '../../modules/cloud';
|
||||||
import { container } from './subscribe.css';
|
import { container } from './subscribe.css';
|
||||||
|
|
||||||
interface ProductTriple {
|
interface ProductTriple {
|
||||||
@@ -113,6 +117,7 @@ export const Component = () => {
|
|||||||
await authService.session.waitForRevalidation(signal);
|
await authService.session.waitForRevalidation(signal);
|
||||||
const loggedIn =
|
const loggedIn =
|
||||||
authService.session.status$.value === 'authenticated';
|
authService.session.status$.value === 'authenticated';
|
||||||
|
|
||||||
if (!loggedIn) {
|
if (!loggedIn) {
|
||||||
setMessage('Redirecting to sign in...');
|
setMessage('Redirecting to sign in...');
|
||||||
jumpToSignIn(
|
jumpToSignIn(
|
||||||
@@ -121,53 +126,40 @@ export const Component = () => {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setMessage('Checking subscription status...');
|
setMessage('Checkout...');
|
||||||
await subscriptionService.subscription.waitForRevalidation(signal);
|
|
||||||
const subscribed =
|
|
||||||
plan === SubscriptionPlan.AI
|
|
||||||
? !!subscriptionService.subscription.ai$.value
|
|
||||||
: recurring === SubscriptionRecurring.Lifetime
|
|
||||||
? !!subscriptionService.subscription.isBeliever$.value
|
|
||||||
: !!subscriptionService.subscription.pro$.value;
|
|
||||||
|
|
||||||
if (!subscribed) {
|
try {
|
||||||
setMessage('Creating checkout...');
|
const account = authService.session.account$.value;
|
||||||
|
// should never reach
|
||||||
|
if (!account) throw new Error('No account');
|
||||||
|
|
||||||
try {
|
track.subscriptionLanding.$.$.checkout({
|
||||||
const account = authService.session.account$.value;
|
control: 'pricing',
|
||||||
// should never reach
|
plan,
|
||||||
if (!account) throw new Error('No account');
|
recurring,
|
||||||
|
|
||||||
track.subscriptionLanding.$.$.checkout({
|
|
||||||
control: 'pricing',
|
|
||||||
plan,
|
|
||||||
recurring,
|
|
||||||
});
|
|
||||||
|
|
||||||
const checkout = await subscriptionService.createCheckoutSession({
|
|
||||||
idempotencyKey,
|
|
||||||
plan,
|
|
||||||
recurring,
|
|
||||||
variant,
|
|
||||||
coupon,
|
|
||||||
successCallbackLink: generateSubscriptionCallbackLink(
|
|
||||||
account,
|
|
||||||
plan,
|
|
||||||
recurring
|
|
||||||
),
|
|
||||||
});
|
|
||||||
setMessage('Redirecting...');
|
|
||||||
location.href = checkout;
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
setError('Something went wrong. Please try again.');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
setMessage('Your account is already subscribed. Redirecting...');
|
|
||||||
await new Promise(resolve => {
|
|
||||||
setTimeout(resolve, 5000);
|
|
||||||
});
|
});
|
||||||
jumpToIndex(RouteLogic.REPLACE);
|
|
||||||
|
const checkout = await subscriptionService.createCheckoutSession({
|
||||||
|
idempotencyKey,
|
||||||
|
plan,
|
||||||
|
recurring,
|
||||||
|
variant,
|
||||||
|
coupon,
|
||||||
|
successCallbackLink: generateSubscriptionCallbackLink(
|
||||||
|
account,
|
||||||
|
plan,
|
||||||
|
recurring
|
||||||
|
),
|
||||||
|
});
|
||||||
|
setMessage('Redirecting...');
|
||||||
|
location.href = checkout;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BackendError) {
|
||||||
|
setMessage(err.originError.message);
|
||||||
|
} else {
|
||||||
|
console.log(err);
|
||||||
|
setError('Something went wrong, please contact support.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}).pipe(mergeMap(() => EMPTY));
|
}).pipe(mergeMap(() => EMPTY));
|
||||||
})
|
})
|
||||||
@@ -191,10 +183,6 @@ export const Component = () => {
|
|||||||
coupon,
|
coupon,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
authService.session.revalidate();
|
|
||||||
}, [authService]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={container}>
|
<div className={container}>
|
||||||
{!error ? (
|
{!error ? (
|
||||||
|
|||||||
Reference in New Issue
Block a user