wolfiton

wolfiton

Hello,

I was searching the documentation to find a way to set multiple session cookies from my pheonix api to my frontend, in my case a nextjs app.

I need to set a cookie with my userid and another one with my user role.

How can i set my cookies?

Also I am not looking for JWT or local storage.

Thank you in advance

Showing Posts 1 to 9

NobbZ

NobbZ

I think you are searching for Plug.Conn.put_session/3, or–if you need more detailed control over the cookie itself–Plug.Conn.put_resp_cookie/4.

wolfiton

wolfiton OP

Thanks that show how i can plug it in, but i am wondering how I can set them in my endpoint.

The documentation only gives this example

defmodule HelloWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :hello
. . .
  plug Plug.Session,
    store: :cookie,
    key: "_hello_key",
    signing_salt: "Jk7pxAMf"
. . .
end

Do I redo this plug again with my new key like this?


defmodule HelloWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :hello

  plug Plug.Session,
    store: :cookie,
    key: "_hello_key",
    signing_salt: "Jk7pxAMf"


  plug Plug.Session,
    store: :cookie,
    key: "_second_cookie_key",
    signing_salt: "Jk7pxAMf"

end

Then i add them like this?

put_resp_cookie(conn, “_hello_key”)
put_resp_cookie(conn, “_second_cookie_key”)

Or there is another way?

NobbZ

NobbZ

Well, Plug.Session actually just stores a single value which again is a map serialized, signed and encrypted.

If you really want to have distinct cookie values, you have no choice but to use Plug.Conn.put_resp_cookie/4 .

wolfiton

wolfiton OP

Can you show me an example on how to use this Plug.Conn.put_resp_cookie/4 ?

Because i can’t imagine how it looks and the documentation doesn’t provide an example in this case?

Thanks

NobbZ

NobbZ

conn = put_resp_cookie(conn, "foo", "bar")

You may need to tweak the options.

thojanssens1

thojanssens1

I think you don’t need that additional Plug.Session plug to add cookies with put_resp_cookie as @wolfiton showed in his first reply?

Also I was wondering, why do we have put_resp_cookie but no get_resp_cookie, what is the idiomatic way to retrieve one of the cookies then?

wolfiton

wolfiton OP

Hi @thojanssens1

We use the plug so that we can attach our cookie with the conn that will be send in to the view.
This article should explain better how plug works Plug · Elixir School
Plug — Phoenix v1.8.8

alvises

alvises

so you manually sign and encrypt the value you pass to put_resp_cookie/4 right?

wolfiton

wolfiton OP

You can use tokens for this and create them with Phoenix token.

Then you use them in your cookie

Here is an example form the documentation

user_id = 1
token = Phoenix.Token.sign(MyApp.Endpoint, "user salt", user_id)
Phoenix.Token.verify(MyApp.Endpoint, "user salt", token, max_age: 86400)
{:ok, 1}

Here is the documentation Phoenix.Token — Phoenix v1.8.8

Also a good read https://phoenixframework.org/blog/sessions

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews