How to get csrf_token in view.

I tried same resolve following.

However It does not work.

| <div>
3 | <form action="/register/confirm" method="post">
4 |     <input type="hidden" name="csrf_token" value="<%= Plug.Conn.get_session(@conn, :csrf_token) %>">
  |                                                   ^

Is there any change of Phoenix how to write code?

Hello and welcome.

When You set attributes, You should use the new syntax…

<input type="hidden" name="csrf_token" value={Plug.Conn.get_session(@conn, :csrf_token)}>

Thank you. However I can not get any csrf_token. I still have following error message.

Plug.CSRFProtection.InvalidCSRFTokenError at POST /register/confirm
invalid CSRF (Cross Site Request Forgery) token, please make sure that:

  * The session cookie is being sent and session is loaded
  * The request include a valid '_csrf_token' param or 'x-csrf-token' header

And I would like to know where I can see the new syntax manual as well.


In the upgrade guide to Phoenix 1.6

Thank you!! Following code works.

<input type="hidden" name="_csrf_token" value={Plug.CSRFProtection.get_csrf_token()}>

You can just use get_csrf_token()