Prevent unauthorized access to user sessions.
defmodule MyApp.SessionController do
use MyApp, :controller
def create(conn, %{"user" => user_params}) do
jwt = MyApp.SessionContext.create_jwt(user_params)
redirect(conn, to: "/welcome?jwt=#{jwt}")
end
end
This Elixir/Phoenix code does not handle JWT session tokens securely. It sends the JWT as a URL parameter which can be cached by the browser or logged in server logs. This exposes the token to potential theft.
defmodule MyApp.SessionController do
use MyApp, :controller
def create(conn, %{"user" => user_params}) do
jwt = MyApp.SessionContext.create_jwt(user_params)
conn = put_resp_header(conn, "authorization", "Bearer #{jwt}")
redirect(conn, to: "/welcome")
end
end
This secure Elixir/Phoenix code example sends the JWT as an HTTP header. This prevents it from being exposed in the URL or cached by the browser.