I have a simple authentication scheme (it’s dead simple, just an email with no password) and I am setting a session on form submission. For some reason when I try to retrive the session cookie information Plug.Conn.get_session
returns nil. I want to know why and what I am doing wrong.
Here is the form submission controller:
defmodule AppxWeb.SessionController do
use AppxWeb, :controller
alias Appx.Accounts.User
alias Appx.Repo
def index(conn, _params) do
render(conn, "index.html")
end
def create(conn, params) do # form post to create session
IO.inspect("created fake session")
user = Repo.get_by(User, email: params["email"])
IO.inspect("____USER ID")
IO.inspect(user.id) # id displays as expected
Plug.Conn.put_session(conn, :user_id, user.id)
render(conn, "index.html")
end
end
Here is a controller for a page where I attempt to get the session id.
It doesn’t work and returns nil.
defmodule AppxWeb.VideoController do
use AppxWeb, :controller
alias Appx.Accounts.User
alias Appx.Repo
def index(conn, _params) do
id = Plug.Conn.get_session(conn, :user_id)
IO.inspect(id) # nil ... ???
render(conn, "index.html")
end
end