Hello
I have a very weird Session bug in Actix-Web I can't explain. I have a route mysite.com/my-account/orders
which triggers the following function:
pub async fn my_orders(session: Session, mysql: web::Data<MySQL>) -> Result<HttpResponse> {
// Check if logged in
let user = match session.get::<User>("user").unwrap() {
Some(u) => u,
None => {
return Ok(HttpResponse::Found()
.append_header(("Location", "/login-or-register"))
.finish());
}
};
// (Removed irrelevant code)...
let s = templates::user_account::MyOrders {
user: Some(user),
orders: order_with_orderlines,
}
.render()
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
Basically the function first checks if the user is logged in. If the user is not logged in he gets redirected to the login-page. Otherwise the page is just displayed.
This works perfectly fine in my browser. I am currently logged in, so when I visit mysite.com/my-account/orders
directly in my browser I see the required page. If I logout and try the same I get redirected to the login-page as intended.
However, I just noticed, if I have have a page outside my website (e.g mail HTML or a simpel HTML page) with the following anchor tag
<a href="https://mysite.com/my-account/orders">test</a>
and I click it, no matter if I am logged in or not, I always get redirected to the login-page. If I refresh the page (when logged in) I finally see the required page.
This does not seem to be a bug in my code, because: The authentication logic works perfectly fine when visiting or testing the URL's in my browser. But the session somehow is not found directly when the URL is visited by clicking on it from another page.
How to fix this?
Edit:
So to be clear: If I make a HTML file with this code <a href="https://mysite.com/my-account/orders">test</a>
. And I click the link, the session is not found and I get redirected to the login-page until I refresh the page. BUT, if I right-click the URL and select "Open URL in new tab", it works perfectly like intended. So the session is only not found if I click on the URL. Copy/pasting the URL in my browser also makes it work perfectly as intended. Seems like Actix-web is somehow not fetching the session when the previous resource was from somewhere outside my own website?
Edit 2:
This only seems to be the case in Safari.