Skip to content
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

Creating a custom SessionStore. #61

Closed
BurntNail opened this issue May 30, 2023 · 5 comments
Closed

Creating a custom SessionStore. #61

BurntNail opened this issue May 30, 2023 · 5 comments

Comments

@BurntNail
Copy link

Hey,

I'm trying to write a custom MemoryStore that works with a sqlx postgres database to store sessions for axum-login.
I've got the following SQL, which has space for all the non-serde-skipped parts of Session:

CREATE TABLE auth_sessions (
    id TEXT PRIMARY KEY NOT NULL,
    expiry TIMESTAMP
);

CREATE TABLE auth_session_data (
    k TEXT NOT NULL,
    v TEXT NOT NULL,

    session_id TEXT NOT NULL,
    CONSTRAINT fk_session_id
        FOREIGN KEY (session_id)
        REFERENCES auth_sessions(id)
        ON DELETE CASCADE
)

Unfortunately, I can't work out a way to get the session contents to/from SQL, as I can't seem to find a constructor for making a Session, or getting all of the data - I can get values from keys but if I don't know what keys are being used (and would rather not have to hardcode a list from axum-login), and I can make a new cookie and add data but I can't do things like set the ID.

@BurntNail
Copy link
Author

Whilst it'd be nice to have a better solution, a backup is always to just use serde. I still feel that this is wrong, in that I can't create a session without going via serde, which could break any invariants and is another dependency when I have to add to get a deserialised/serialised form.

See example:
https://docs.rs/async-sqlx-session/latest/src/async_sqlx_session/pg.rs.html#270-330

@jbr
Copy link
Member

jbr commented May 30, 2023

This was not a use case I imagined when writing the current version, so it is not possible. However, the next version should be able to support this, and will be substantially less prescriptive in usage. I'm surprised that it would be preferable to store key-value pairs in a joined table instead of serializing to a string or json column, but async-session should support whatever session storage model a particular backend needs. I'll leave this issue open until the next minor release, which should be soon

@BurntNail
Copy link
Author

Thanks! Looking forward to that next release - any way I can help?

@jbr
Copy link
Member

jbr commented May 30, 2023

Sure! Take a look at #57 and provide any feedback over there.

@BurntNail
Copy link
Author

Having had another look over the docs, for anyone coming across the future, I'll be using sqlx json, as this seems like the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants