State isn't saved when using axum-login with leptos #145
-
I'm trying to use axum-login in a leptos/axum project. I'm using leptos's extract API to get access to the AuthSession variable, but when I mutate that variable (i.e. when a user tries to login), that new state isn't saved. The next time a user tries to make a request, the server treats them like they haven't logged in. First, this server function is called: #[server(Login)]
pub async fn login(email: String, password: String) -> Result<(), ServerFnError> {
let creds = Credentials::Email { email, password };
let user = extract(|auth_session: AuthSession<PgPool>| async move {
match auth_session.authenticate(creds).await {
Ok(Some(user)) => Ok(user),
Ok(None) => Err(ServerFnError::ServerError("Unauthorized".into())),
Err(_) => Err(ServerFnError::ServerError("Internal Server Error".into())),
}
})
.await
.map_err(|_| {
ServerFnError::ServerError("Could not extract method and query...".to_string())
})??;
let res =
extract(
|mut auth_session: AuthSession<PgPool>| async move { auth_session.login(&user).await },
)
.await
.map_err(|_| {
ServerFnError::ServerError("Could not extract method and query...".to_string())
})?;
if res.is_err() {
return Err(ServerFnError::ServerError("Internal Server Error".into()));
}
Ok(())
} After the user has logged in, this function is called: #[server(GetUser, "/api")]
pub async fn get_user() -> Result<Option<User>, ServerFnError> {
extract(|auth_session: AuthSession<PgPool>| async move { auth_session.user })
.await
.map_err(|_| {
ServerFnError::ServerError("Could not extract method and query...".to_string())
})
} But it returns The code base I'm working on can be found here: https://gitlab.com/wptcsu/my-wpt. The login logic is here: https://gitlab.com/wptcsu/my-wpt/-/blob/main/src/user.rs?ref_type=heads#L288 and the setup of the axum server can be found here: https://gitlab.com/wptcsu/my-wpt/-/blob/main/src/main.rs?ref_type=heads#L111. Is this an issue with my code, or is it a bug in axum-login? Please note that I am using axum-login version 0.9 because Leptos is still using version 0.2.11 of the http crate (axum-login version 0.10 uses http version 1.0, which is incompatible with http version 0.2.11). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Unfortunately I'm not too familiar with Leptos. Can you reproduce the issue without Leptos? |
Beta Was this translation helpful? Give feedback.
-
I found the issue; when implementing the After including it here: https://gitlab.com/wptcsu/my-wpt/-/blob/main/src/user.rs?ref_type=heads#L242, the login session worked as expected. |
Beta Was this translation helpful? Give feedback.
I found the issue; when implementing the
get_user
function, I didn't include the hash that was being used as thesession_auth_hash
. Because of that, the session was being invalidated.After including it here: https://gitlab.com/wptcsu/my-wpt/-/blob/main/src/user.rs?ref_type=heads#L242, the login session worked as expected.