Domnyk

Domnyk

CSRF exception when using Angular with Phoenix

Hi there!

Using Phoenix I have created json api which is being consumed by Angular. Now I’m trying to add protection against csrf attacks.

In router, inside my api pipeline I have added this:

plug Plug.CSRFProtection, allow_hosts: "https://localhost:8080/"

This plug is called after session is fetched and address under allow_hosts is where angular app is running.

When user wants to sign in he fills the form and it is being send to api. Here is fragment of controller method which handles session creation:

 token = Plug.CSRFProtection.get_csrf_token()

   conn_with_fetched_session
   |> put_session(:current_user_id, complexes_owner.id)
   |> put_session(:current_user_type, :owner)
   |> put_resp_cookie("XSRF-TOKEN", token, http_only: :false)
   |> render("ok.create.json", complexes_owner: complexes_owner)

As you can see I set cookie which can be read by javascript (http_only set to false). When I perform other post requests to api Angular copies content of “XSRF-TOKEN” cookie to “x-csrf-token” header.

Unfortunately, for any request requiring csrf token api returns Plug.CSRFProtection.InvalidCSRFTokenError.

What am I doing wrong? Thank you for any response.

Most Liked

calypso

calypso

I would also be interested in the solution of the problem your were facing :slight_smile:

Where Next?

Popular in Questions Top

New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New

We're in Beta

About us Mission Statement