1
votes

I have a logout system which is created in a Phoenix app, where the logout button is in a Phoenix template, as shown by the three code snippets below. This works fine.

Phoenix template: /web/templates/layout/app.html.eex

<%= link "Logout", to: auth_path(@conn, :delete), method: :delete, class: "btn btn-danger" %>

Phoenix Routes: /web/router.ex

The relevant route is the delete route.

scope "/auth", AdminApi do
  pipe_through :browser

  get "/:provider", AuthController, :index
  get "/:provider/callback", AuthController, :callback
  delete "/logout", AuthController, :delete
end

Phoenix controller: /web/controllers/auth_controller.ex

def delete(conn, _params) do
  conn
  |> put_flash(:info, "You have been logged out!")
  |> configure_session(drop: true)
  |> redirect(to: "/")
end

I am trying to implement the same thing, but with a separate frontend and using Phoenix as a JSON API.

The idea is that the frontend makes an AJAX DELETE request to the relevant API endpoint, and the route then catches that call and fires the delete function in the auth_controller.

How do I configure the route, so that it will fire the relevant function in the controller?

PS I'm certain it is not a CORS issue, as the CORS plug is working for other routes such as "/auth/:provider"

Below is what I have so far:

AJAX Request from frontend

var url = ENV.apiProtocol + ENV.apiHost + "/api/auth";
Ember.$.ajax({
    url : url,
    type: "DELETE",
  success: function(response){
    console.log(response)
  },
  error: function(response) {
    console.log(response)
  }
});

Phoenix routes: /web/router.ex

scope "/api", AdminApi do
    pipe_through: api

    resources "/auth/:provider", AuthController, except: [:new, :edit]
    resources "/auth", AuthController, except: [:new, :edit]

end

Phoenix controller: /web/controllers/auth_controller.ex

def delete(conn, _params) do
  conn
  |> put_flash(:info, "You have been logged out!")
  |> configure_session(drop: true)
  |> redirect(to: "/")
end
2
What part of this is not working? Are you getting any error that you could share?Justin Wood
The error says "no route found for DELETE /api/auth (AdminApi.Router)"AJP
If you run mix phx.routes is there one listed? Have you tried restarting your phoenix server?Justin Wood
Yes it shows auth_path DELETE /api/auth/:id AdminApi.AuthController :delete Essentially I'd like to customise the path /api/auth/:id so that I can make a request to a fixed endpoint and fire the delete function.AJP
That is not the same route. You are not including the :id portion, so it will not match.Justin Wood

2 Answers

1
votes

I think you have a conflicting route. By using resources in that way, you're creating a route which expects an ID to be appended to the end of the path. I.e. DELETE /api/auth/3.

You would benefit from having a normal DELETE /api/auth or DELETE /api/session route where the token you're passing is indicative of the user whose session you'd like to destroy.

0
votes

I found the solution. I was defining a resource in the route, and so Phoenix as expecting a DELETE request to be made to the endpoint /api/auth:id.

In web/router.ex I changed

resources "/auth", AuthController, except: [:new, :edit]

to

delete "auth/logout", AuthController, :delete

Now an AJAX DELETE request to the endpoint /api/auth/logout fires the logout function in auth controller.