Phoenix db session

Is there a plug which uses db to store sessions?

Not by default because phoenix cannot make an assumptions about your database, but it is almost trivial to make one for your own app. The most simple way would be to make a table, say with binary key and binary data, using term_to_binary/1 and such, maybe some dates so you can handle expiration, then just make a new module of the similar style of: plug/lib/plug/session/ets.ex at main · elixir-plug/plug · GitHub
You could name yours like MyServer.Session.Store.Database or something, at which point in the :store key in the Plug.Session configuration on your endpoint or config then just put MyServer.Session.Store.Database as the :store. :slight_smile:

You’d probably still want to store something on a cookie to look up a session if you want a session to persist longer than just a simple browser session though, but you could even encode that functionality into your Plug Store too. :slight_smile:

If you wanted, you could even make a library wrapping all that functionality into a single store for anyone to use, say via setting a config option for defining a user callback for storing/retrieving from their database or so, perhaps with a set of default options to generate a table if someone does not want a custom made one.

1 Like