axum-login icon indicating copy to clipboard operation
axum-login copied to clipboard

Postgres example

Open thor314 opened this issue 11 months ago • 2 comments

I'm working on adding an example for postgres, since it seems to differ a bit from the sqlite example provided. I'm currently debugging why the session authentication here:

// todo: currently returning Err
        let user = match auth_session.authenticate(creds.clone()).await {
            Ok(Some(user)) => user,
            Ok(None) => {
                messages.error("Invalid credentials");

                let mut login_url = "/login".to_string();
                if let Some(next) = creds.next {
                    login_url = format!("{}?next={}", login_url, next);
                };

                return Redirect::to(&login_url).into_response();
            }
            Err(_) => return StatusCode::INTERNAL_SERVER_ERROR.into_response(),
        };

is failing. Marking as draft PR.

thor314 avatar Mar 26 '24 19:03 thor314

I wrote this postgres example to document a bug I'm running into in my own repo. This PR creates an example for postgres, and documents a failing assumption--that a table called session exists in the database.

The following login method fails accordingly:

    pub async fn login(
        mut auth_session: AuthSession,
        messages: Messages,
        Form(creds): Form<Credentials>,
    ) -> impl IntoResponse {
        // todo: see auth session:
        // auth session thinks a table called "session" exists:
        // schema_name: "tower_sessions",
        // table_name: "session",
        #[cfg(test)]
        dbg!(auth_session.clone());

        let user = match auth_session.authenticate(creds.clone()).await {

This is either a bug in axum-login, or I may otherwise misunderstand how the login function should accordingly be changed from the sqlite example.

thor314 avatar Mar 26 '24 19:03 thor314

with the last 3 commits, I've updated the postgres example to pass unit tests.

thor314 avatar Mar 28 '24 23:03 thor314

@thor314 is there any news?

frederikhors avatar Jun 28 '24 09:06 frederikhors

This should probably be an independent repository, perhaps even a fully-fledged template others could build from. I don't think it's prudent to increase the maintenance burden of axum-login itself so I'm going to close this. If you end up establishing a template repo, please feel free to link to it from the axum-login README.

maxcountryman avatar Jul 18 '24 18:07 maxcountryman