GithubHelp home page GithubHelp logo

Get/Set Browser Cookies about nitro HOT 6 CLOSED

synrc avatar synrc commented on August 15, 2024
Get/Set Browser Cookies

from nitro.

Comments (6)

etxemag75 avatar etxemag75 commented on August 15, 2024 1

I pulled latest NITRO/N2O and tested your added interfaces, nitro:cookie/2 and cookie/3, seems to work fine! (thanks for adding) Thx also for fixing #api callback issue!

For completeness, would be good if you could add this function to get a certain cookie also since they are already in the context.

cookies() -> cowboy_req:parse_cookies((get(context))#cx.req).
cookie(Key) -> case lists:keyfind(Key, 1, cowboy_req:parse_cookies((get(context))#cx.req)) of false -> undefined; {_, Value} -> Value end.

Worth mentioning that the set cookie is not visible until next page reload.

Regarding moving n2o_nitro from N2O to NITRO, seems fine to me.

from nitro.

5HT avatar 5HT commented on August 15, 2024

We removed cookies from N2O since 5.11, but added into NITRO 4.7.3 the following API:

iex(1)> :nitro.cookie 'hello', 'world'
[]
iex(2)> :erlang.get :actions
[
  {:wire, :action, [], [], :action_wire,
   'document.cookie=\'hello=world; path=/; expires=Sat, 26-Jul-2087 00:56:15 GMT\';',
   []}
]

you can use in inside your pages. The full example here: o7/plm@e262877

defmodule PLM.Login do
  use N2O, with: [:n2o, :nitro]
  use FORM
  require Logger
  def event(:init) do
    NITRO.clear(:stand)
    NITRO.cookie 'hello', 'world'
    NITRO.wire(api(name: 'getCookies', delegate: PLM.Login))
    mod = PLM.Forms.Pass
    NITRO.insert_bottom(:stand, FORM.new(mod.new(mod, mod.id()), mod.id()))
  end
  def event({:SMS, _}), do: NITRO.wire("document.getCookies('sample string');")
  def event(any), do: IO.inspect any
  def api_event(_,_,_), do: IO.inspect "API EVENT~n"
end

from nitro.

5HT avatar 5HT commented on August 15, 2024

Maybe it's time to move n2o_nitro to NITRO as well. Let me know what do you think!

from nitro.

5HT avatar 5HT commented on August 15, 2024

Worth mentioning that the set cookie is not visible until next page reload.

It was to be in sync with cookies ETS table in 5.11 I just removed this. We had server-side controlled cookies, that's why we had infinite expire time. If you want server controlled cookies we need an additional table not mix them with session variables n2o:session (sessions), nitro:cookie (cookies).

from nitro.

etxemag75 avatar etxemag75 commented on August 15, 2024

In my use cases, it is enough that they are visible after page reload, i.e. browser controlled cookies.

Would be nice if you could add to nitro.erl :

cookies() -> cowboy_req:parse_cookies((get(context))#cx.req).
cookie(Key) -> case lists:keyfind(Key, 1, cowboy_req:parse_cookies((get(context))#cx.req)) of false -> undefined; {_, Value} -> Value end.

You could close this ticket as solved anyway.

from nitro.

5HT avatar 5HT commented on August 15, 2024

It's added in 4.7.4 eb3fe4b

from nitro.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.