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.
Trending in Questions
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
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
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
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
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
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
NGINXfor reverse proxy. For more info read this - https://medium.com/@dmadan86/run-chrome-browser-without-cors-by-disabling-web-security-d124ad4dd2cfmicahsoftdotexe
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
Sounds good!
micahsoftdotexe
I also realized that using the
:refresh_from_cookieis supposed to refresh the token using the refresh token found in the key of the cookie so I attempted using the deprecatedGuardian.Plug.VerifyCookieinstead 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
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 APIaccess tokenandrefresh token.Would you mind If I send a private message here on the forum?
muelthe
What is the error that you’re seeing from
EnsureAuthenticated?micahsoftdotexe
It returns the error of
unauthenticatedas a response (with a401).micahsoftdotexe
I was able to craft my own plug that is able to both grab the correct token and verifies it with the following:
but I would like to find the proper way to do it.
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
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.