Rya

Rya

I’m trying to create a controller function that returns a view of the currently logged in user. I use guardian for authentication, and save during log-in the user’s id:

  def sign_in(conn, %{"email" => email, "hashed_password" => hashed_password}) do
    case Guardian.authenticate(email, hashed_password) do
      {:ok, user, token} ->
        conn
        |> Plug.Conn.put_session(:user_id, user.id)
        |> put_status(:ok)
        |> render(:show_token, user: user, token: token)
      {:error, :unauthorized} -> raise ErrorResponse.Unauthorized, message: "email or password incorrect"

    end
  end

I then create a function that shows the user corresponding to the session

 def show_from_session(conn) do
    user_id = get_session(conn, :user_id)
    user = Users.get_user!(user_id)

    render(conn, "show.json", user: user)
  end

The json object is the standard object created by elixir:

  def show(%{user: user}) do
    %{data: data(user)}
  end

Doing this gives me the following message:

[info] GET /api/swimmer/show_logged_in
[debug] Processing with Backend24hWeb.UserController.show_from_session/2
  Parameters: %{}
  Pipelines: [:api, :swimmer_auth]
[debug] QUERY OK source="users" db=0.3ms queue=0.8ms idle=1007.8ms
SELECT n0."id", n0."email", n0."hashed_password", n0."nom", n0."prenom", n0."inserted_at", n0."updated_at" FROM "users" AS n0 WHERE (n0."id" = $1) ["46180675-91ef-455d-a5a9-8d4b1fb69b06"]
↳ Backend24hWeb.Auth.Guardian.resource_from_claims/1, at: lib/backend24h_web/auth/guardian.ex:15
[info] Sent 500 in 35ms
[error] ** (KeyError) key :assign not found in: %Plug.Conn{
  adapter: {Bandit.Adapter,
   %Bandit.Adapter{ [truncated]

I’m relatively new to Elixir so i’m not sure i interpret this message correctly, but the error seems to come from the guardian.ex file. But this file doesn’t use assign either.

here’s the ressource_from_claims/1 function:

  def resource_from_claims(%{"sub" => id}) do
    case Users.get_user!(id) do
      nil -> {:error, :not_found}
      resource -> {:ok, resource}
    end
 end

I believe some default implementation of this ressource from claims function might get called by my code, but i don’t understand when or why.

Has anyone encoutered a similar issue ?

Thanks in advance

Showing Posts 1 to 1

Rya

Rya OP

Realized my mistake here: I created a session plug that would assign a user when called. This plug used conn.assign instead of conn.assigns. This typo is not related to resource_from_claims. I just couldn’t find the typo.

— 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