Error on when Trying to redirect logged in User on Login page

When I am adding the :auth pipe in the following code

  scope "/", ApolloWeb do
    pipe_through([:browser, :auth])
    get("/.well-known/acme-job_opening/:id", LandingPageController, :letsencrypt)

    resources("/", LandingPageController, only: [:index])
    get("/admin/signup", AdminController, :new)
    get("/attempt_exit", AttemptController, :exit)
    get("/attempt_exit_mobile", AttemptController, :exit_mobile)
    get("/submit", AttemptController, :test_submit)
    get("/admin/request_demo", AdminController, :request_demo)
    post("/admin/request_demo", AdminController, :send_request)
    resources("/admin", AdminController, only: [:create])

    get("/admin/login", AdminSessionController, :new)
    post("/admin/session", AdminSessionController, :create)
    get("/admin/forgot_password", AdminSessionController, :forgot_password)
    post("/admin/forgot_password", AdminSessionController, :reset_password_email)
    get("/admin/reset_password", AdminSessionController, :reset_password)
    post("/admin/reset_password", AdminSessionController, :reset_password_confirmation)

    # routes for addon_auth
    get "/addon_auth", AdminSessionController, :addon_auth
    post "/addon_auth", AdminSessionController, :create_addon
  end

I am getting following error when hitting http://localhost:4000/admin/login

This page isn’t working
localhost redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

Even I am trying the following still, the Same error

  scope "/", ApolloWeb do
    pipe_through([:browser, :auth])

    get("/admin/login", AdminSessionController, :new)
    post("/admin/session", AdminSessionController, :create)
  end

  scope "/", ApolloWeb do
    pipe_through(:browser)
    get("/.well-known/acme-job_opening/:id", LandingPageController, :letsencrypt)

    resources("/", LandingPageController, only: [:index])
    get("/admin/signup", AdminController, :new)
    get("/attempt_exit", AttemptController, :exit)
    get("/attempt_exit_mobile", AttemptController, :exit_mobile)
    get("/submit", AttemptController, :test_submit)
    get("/admin/request_demo", AdminController, :request_demo)
    post("/admin/request_demo", AdminController, :send_request)
    resources("/admin", AdminController, only: [:create])

    get("/admin/forgot_password", AdminSessionController, :forgot_password)
    post("/admin/forgot_password", AdminSessionController, :reset_password_email)
    get("/admin/reset_password", AdminSessionController, :reset_password)
    post("/admin/reset_password", AdminSessionController, :reset_password_confirmation)

    # routes for addon_auth
    get "/addon_auth", AdminSessionController, :addon_auth
    post "/addon_auth", AdminSessionController, :create_addon
  end

/admin/login requires you to be authenticated and therefore redirects you to /admin/login, which requires you to beeing authenticated and therefore redirects you to /admin/login, which requires you to beeing authenticated and therefore redirects you to /admin/login/ which causes eventually to many redirects.

Remove that route from the scope that gets piped through :auth.

If I remove following route from the :auth

    get("/admin/login", AdminSessionController, :new)

then how will I achieve authenication on the login Page. See, finally I want to achieve following-

If I am logged in then clicking on the admin login page on the main page should redirect me to the dashboard page and not login page if I have a logged in session already.

Your help will be appreciated.

:wave:

Try separating the logic into two plugs / pipelines. One (for the logic routes) loads the user id from the session in the conn.assigns, and the other (for the protected routes) redirects to the login route if the user id is not found in the assigns.

1 Like

Could you share some example or blog?