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
mix phx.routes
is there one listed? Have you tried restarting your phoenix server? – Justin Wood:id
portion, so it will not match. – Justin Wood