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

User gets logged out after setting up stripe payment #8

Open
jan10 opened this issue Sep 16, 2024 · 6 comments
Open

User gets logged out after setting up stripe payment #8

jan10 opened this issue Sep 16, 2024 · 6 comments

Comments

@jan10
Copy link

jan10 commented Sep 16, 2024

When a user completes the setup for Stripe payment, they are unexpectedly logged out of their account.

Steps to Reproduce:

  1. Log in to the application.
  2. Navigate to the payment settings section.
  3. Select the option to set up Stripe payment.
  4. Complete the Stripe payment setup process.
  5. Observe that the user is logged out immediately after the setup is completed.

Screenshare

https://jam.dev/c/132a5f62-4b41-43c6-a328-4b6252ccd212

@MhemedAbderrahmen
Copy link

MhemedAbderrahmen commented Sep 16, 2024

User is being redirected to the /signin page but he is not logged out, this can be fixed by either:

  • redirecting to the home page
  • when user is on /signin , if he is already authorized redirects to /dashboard ( i feel like this is the best option since /signin should only be accessible when you are logged out )

PS: I can create a PR on this later on today

@leerob
Copy link
Collaborator

leerob commented Sep 16, 2024

Huh, seems I introduced a regression here: https://github.com/leerob/next-saas-starter/pull/2/files. But I'm not really sure what it is.

It seems like after setting the cookie in the Route Handler for Stripe Checkout, it's empty when being read in the Middleware now when you redirect to /dashboard. If you look at dev tools, the cookie is successfully saved. If you reload the page, you see it.

This doesn't seem to be working either:

const sessionCookie = await setSession(user[0]);
const response = NextResponse.redirect(new URL('/dashboard', request.url));
response.headers.set('Set-Cookie', sessionCookie);
return response;

I'll have to dig more.

@mono300genuine
Copy link

User is being redirected to the /signin page but he is not logged out, this can be fixed by either:

  • redirecting to the home page
  • when user is on /signin , if he is already authorized redirects to /dashboard ( i feel like this is the best option since /signin should only be accessible when you are logged out )

PS: I can create a PR on this later on today

I had same issue but your article helped me.

@Taimoor2500
Copy link

const sessionCookie = await setSession(user[0]);
const response = NextResponse.next();
response.headers.set('Set-Cookie', sessionCookie);
return response;

could you try this as some browsers might ignore Set-Cookie headers when they're part of a redirection response, you can handle redirect afterward

@mono300genuine
Copy link

const sessionCookie = await setSession(user[0]); const response = NextResponse.next(); response.headers.set('Set-Cookie', sessionCookie); return response;

could you try this as some browsers might ignore Set-Cookie headers when they're part of a redirection response, you can handle redirect afterward

yes

@sanjomathew09
Copy link

import { getSession } from 'next-auth/react';

export async function getServerSideProps(context) {
// Retrieve the session
const session = await getSession(context);

// If there is no session, redirect to the login page
if (!session) {
return {
redirect: {
destination: '/login',
permanent: false, // Set to false because it's not a permanent redirect
},
};
}

// If the session exists, pass it to the page as props
return {
props: { session },
};
}

export default function ProtectedPage({ session }) {
return (


Welcome, {session.user.name}


This is a protected page, accessible only to authenticated users.



);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants