-
Notifications
You must be signed in to change notification settings - Fork 49
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
Losing Cookies/Session after redirect #51
Comments
The same. How to keep session like with other passports (google, facebook) with keepsessioninfo? |
@ananay is that doable to fix it? |
the issue will not fix by I did below work around to fix this. Save the session in _token cookie temporarily. It will not have sameSite property so that you can access it after redirect from apple. Notice I have added a path to this cookie so that it only get send to this path and also maxAge is very short.
When Apple call us back you can access this _token cookie and load session back. You can use below
|
I found a solution to this. As mentioned by others, Apple wants to "think different" and use POST in their callback, however browser's SameSite policy doesn't allow cookies to be included in (POST) requests originating from a different site (in this case Apple). So the session does not exist and the successfuly authentication result ( A solution is to first redirect the POST request to a GET request, because a GET request can access cookies. And we include all the request body parameters as query params to the GET request: // initial request, redirects to Apple website
router.get('/apple', passport.authenticate('apple'));
// this is the callback from Apple. Now redirect to GET with query params:
router.post('/apple/callback', express.urlencoded({ extended: true }), (req, res) => {
const { body } = req;
const sp = new URLSearchParams();
Object.entries(body).forEach(([key, value]) => sp.set(key, String(value)));
res.redirect(`/apple/callback?${sp.toString()}`);
});
// Here we handle the GET request after the redirect from the POST callback above
router.get('/apple/callback', passport.authenticate('apple', {
successReturnToOrRedirect: '/success',
failureRedirect: '/failure',
})); This works even with redirectmeto.com on localhost (without https) now! |
When apple redirects back with post request, I lose the current user session due sameSite Lax because it does not include cookie in callback with cross site post request.
The text was updated successfully, but these errors were encountered: