How to protect routes using oidcc in phoenix? #357
-
Hi, I'm trying to setup a project using this library as auth solution. I have a phoenix project that I used I would like to redirect not logged in users to a login page. The plugs provided by How would I build a pipeline/plug to use in my Thank you for your time. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hey @Lionstiger There's two ways how oidcc plugs help you with your application:
Judging by you using The generator of You can create your own plug checking if the claims are present in the session and include it in your router or controller for all the protected routes. defmodule MyAppWeb.Router do
use Phoenix.Router
pipeline :browser do
plug :fetch_session
plug :accepts, ["html"]
end
pipeline :protected do
plug MyAppWeb.CheckSession
end
scope "/public" do
pipe_through :browser
get "/", MyAppWeb.Controller, :public
end
scope "/protected" do
pipe_through [:browser, :protected]
get "/", MyAppWeb.Controller, :protected
end
end In
Since this is a common use case, it would be great to have this in the documentation. Would you be open to send a documentation PR for |
Beta Was this translation helpful? Give feedback.
Hey @Lionstiger
There's two ways how oidcc plugs help you with your application:
Judging by you using
phx_gen_oidcc
, you want to do the latter.The generator of
phx_gen_oidcc
puts the OpenID claims into the session:https://github.com/erlef/phx_gen_oidcc/blob/2b1b75296d5f8d758f2d61894edbf04319d40455/priv/templates/oidcc_controller.exs#L32
You can create your own plug checking if the claims are present in the session and include it in your router or controller for all the protected routes.