Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session ID not always updating after subscription is created or deleted #686

Open
ghost opened this issue Sep 14, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@ghost
Copy link

ghost commented Sep 14, 2024

Describe the bug A clear and concise description of what the bug is.

When I create or delete a subscription, I sometimes need to logout and log back in before the subscription will take effect.

To Reproduce Steps to reproduce the behavior:

  1. Follow the instructions in the README to setup this project with Github Auth and Stripe.
  2. Upgrade your account
  3. Go to the /account page and see that the subscription has not updated.
  4. Log out, log back in, head to /account, and see that your subscription has updated.

Sometimes, you'll not see the subscription being upgraded. I'm not sure why it's only sometimes.

The same thing occurs for me when I cancel a subscription. I also need to log out and in to see the change reflected.

Expected behavior A clear and concise description of what you expected to
happen.

I expect the subscription status to be updated.

Desktop (please complete the following information):

  • OS: iOS
  • Browser: Arc (Chromium based), Safari
  • Version 1.60.0, 17.6 (19618.3.11.11.5)

Additional context Add any other context about the problem here.

I added logging statements in routes/api/stripe-webhooks.ts and plugins/session.ts to see what's going on.

It seems that the session ID is different. I suspect the request has the old session ID.

customer.subscription.deleted - user = {
  login: "RealScrappyDev",
  sessionId: "7940bacf-9e00-4238-80b1-079c72e8eab8",
  isSubscribed: false,
  stripeCustomerId: "cus_QqYvnSqI1OeGqo"
}
setSessionState - user = {
  login: "RealScrappyDev",
  sessionId: "3651b8e0-ba96-4065-89a6-5a1f1d2f7ad2",
  isSubscribed: true,
  stripeCustomerId: "cus_QqYvnSqI1OeGqo"
}

I can work around this by using the session ID to get the user's login and then getting the user from there, but this doesn't seem ideal. Specifically, this works for me in plugins/session.ts's setSessionState function:

  const sessionId = getSessionId(req);
  if (sessionId === undefined) return await ctx.next();
  const user = await getUserBySession(sessionId);
  if (user === null) return await ctx.next();
  const latestUser = await getUser(user.login);
  if (latestUser === null) return await ctx.next();

  ctx.state.sessionUser = latestUser;

It is definitely possible that I'm doing something wrong here. I've tried to start fresh with a new database and stripe init.

@ghost ghost added the bug Something isn't working label Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

0 participants