micahsoftdotexe

micahsoftdotexe

Greetings,
I am attempting to implement a jwt pattern in phoenix using guardian. What I am doing is that I am returning a short lived access token in the response and a long lived refresh token in the cookies. So far, I am able to send back a token back in a cookie using the following code:

with({:ok, jwt, _full_claims} <- Token.encode_and_sign(user, %{}, ttl: {1, :minute}),
      {:ok, cookieJWT, _full_claims} <- Token.encode_and_sign(user, %{}, [ttl: {1, :week}, type: :refresh])) do
        conn
        |> put_resp_cookie("refresh_token", cookieJWT, http_only: true)
        |> put_resp_content_type("application/json")
        |> send_resp(200, Jason.encode!(%{token: jwt}))

and I am trying to create a refresh method and guarding it with a pipeline that looks like the following:

plug Guardian.Plug.VerifySession, [refresh_from_cookie: true, key: "refresh_token"]
  plug Guardian.Plug.EnsureAuthenticated
  plug Guardian.Plug.LoadResource

The issue is that VerifySession does not appear to be able to verify the cookie (even though it appears to be sent) because EnsureAuthenticated returns an error. Is there something that I am doing wrong? I am pretty new to Phoenix and Elixir in general so any help would be greatly appreciated.

Showing Posts 1 to 10

HappyBee

HappyBee

I’ve faced this earlier, but not using Guardian, What kinda web browser are you using ?? Chrome??
Normally backend has to use CORS header enabled
Chrome has some issues when using local computer(ie., backend & frontend on same machine). You can use NGINX for reverse proxy. For more info read this - https://medium.com/@dmadan86/run-chrome-browser-without-cors-by-disabling-web-security-d124ad4dd2cf

micahsoftdotexe

micahsoftdotexe OP

At the moment I am just attempting to use insomnia to hit the endpoints while I am in the early stages of developing. Once I get some of the backend done, I will attempt to use Firefox to test my frontend and how it interacts with the phoenix API.

HappyBee

HappyBee

Sounds good!

micahsoftdotexe

micahsoftdotexe OP

I also realized that using the :refresh_from_cookie is supposed to refresh the token using the refresh token found in the key of the cookie so I attempted using the deprecated Guardian.Plug.VerifyCookie instead so that I could implement my own refreshing functionality and I am having the same issue. I did try the following in the controller and it could see the cookie: IO.puts(conn.req_cookies["refresh_token"]). I am just not sure what might be wrong.

HappyBee

HappyBee

Hi Micah, what are you actually trying to do? Are you trying to build a single API and use it on mobile device and web browser?

I highly recommend using Pow if you’re just beginning .. Guardian was bit confusing for me, so I switched to Pow. There’s much support for API building, also it has got great Mnesia(An inbuilt db/cache) for storing API access token and refresh token.

Would you mind If I send a private message here on the forum?

muelthe

muelthe

What is the error that you’re seeing from EnsureAuthenticated?

micahsoftdotexe

micahsoftdotexe OP

It returns the error of unauthenticated as a response (with a 401).

micahsoftdotexe

micahsoftdotexe OP

I was able to craft my own plug that is able to both grab the correct token and verifies it with the following:

defmodule MyApp.Auth.CookieTokenValidator do
  import Plug.Conn
  alias Guardian.Plug.Pipeline

  @behaviour Plug
  @impl Plug
  def init(opts), do: opts
  @impl Plug
  def call(conn, opts) do
    with {:ok, token} <- get_token_from_cookie(conn, opts),
    module <- Pipeline.fetch_module!(conn, opts),
    {:ok, claims} <- Guardian.decode_and_verify(module, token, %{}, opts) do
      conn
        |> Guardian.Plug.put_current_token(token, key: "default")
        |> Guardian.Plug.put_current_claims(claims, key: "default")
    else
     _error -> conn
      |> send_resp(401, Jason.encode!("Could not validate token"))
      |> halt()
    end
  end

  defp get_token_from_cookie(conn, opts) do
    key = Keyword.get(opts, :key, "token")
    token = conn.req_cookies[key]
    if token, do: {:ok, token}, else: :no_token_found
  end
end

but I would like to find the proper way to do it.

muelthe

muelthe

I had been working through an issue mself (hence the original question). I have a very simple setup (almost identical to the docs in conjunction with Ueberauth for an msoauth setup) and the problem for me was misconfiguration of the endpoint, specifically the SameSite config.

micahsoftdotexe

micahsoftdotexe OP

Hmmm. That is interesting. I had followed several tutorials but it appeared like it was not able to fetch the cookie and that was why I decided to attempt to fetch it myself and utilize some of the functions within guardian to validate the value within the cookie.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews