I’m implementing Facebook OAuth login in my web application and trying to set a session cookie after the OAuth callback. The flow works correctly on my local environment (localhost), but in beta and dev environments (my beta and dev domain), the cookie is being stored under facebook.com instead of my domain. This prevents my frontend from accessing the cookie.
let cookie = cookie::Cookie::build("SocialLoginToken", session_id)
.http_only(true) // Prevent JavaScript access
.secure(true) // Only send over HTTPS
.same_site(cookie::SameSite::Lax) // Prevent CSRF
.domain("beta.mydomain.com") // Applies to the beta environment
.path("/") // Cookie applies to the entire domain
.build();`
// Create a response that includes the Set-Cookie header and the redirect
let mut headers = HeaderMap::new();
headers.insert(header::SET_COOKIE, cookie.to_string().parse().unwrap());
let redirect_url = "xyz";
(headers, Redirect::to(redirect_url)).into_response()````
Expected Behavior:
The SocialLoginToken cookie should be set under my domain (xyz) after Facebook redirects to my OAuth callback endpoint.
Actual Behavior:
The cookie is being set under facebook.com instead of mydomain, making it inaccessible to my frontend.
New contributor
Nirav Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1