fireproofsocks

fireproofsocks

I’m working on a custom plug to handle some signin logic for a Phoenix app. Knowing that Phoenix is built on plugs, I thought it might be nice to have this plug handle everything related to this particular request since it felt somewhat arbitrary and redundant to send the request to a controller. In my router I’ve tried a couple things:

scope "/some/path" do
  pipe_through :api
  forward "/", MyApp.CustomPlug
end

And the unscoped “naked” version of just doing this:

forward "/some/path", MyApp.CustomPlug

Those DO work, but the forward macro will forward EVERYTHING to the plug – GET’s, POST’s, as well as any deeper routes built off of the given one, e.g. /some/path/and/this/is/not/what/i/expected

I like the interface of just being to tie the plug to one route and let it handle everything, but is this misunderstanding how the plug should be used? (I may package this plug separately for use with other apps). If this is a legitimate way to use the plug, how would I restrict it to only respond to POST request, and how would I tell it to ignore any other paths other than the base path?

Of course, I put the plug in a pipeline and handle everything there, but it seems a bit redundant to rely on a pipeline for this. What might be a cleaner way to accomplish this?

Thanks!

Showing Posts 1 to 3

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You’re close! Just replace forward with Phoenix.Router — Phoenix v1.8.8

fireproofsocks

fireproofsocks OP

Hmmm, that’s not quite what I’m going for: post only works when it’s inside a scope block (that’s what I meant by “naked” and “unscoped”). forward seems to work anywhere inside your Phoenix router module, even outside a scope block.

Also, if you are just using a plug outside of any pipeline and without ever resolving to a controller, is there a trick to skipping any requests that don’t have application/json? Or would it just return the unaltered conn struct?

Gazler

Gazler

Phoenix Core Team

You can define a scope at the root level with scope "/" do:...

scope "/" do
  pipe_through :api
  post "/some/path", MyApp.CustomPlug
end
— 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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
FlyingNoodle
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

Other Trending Topics Top

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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews