the_dude

the_dude

Trying to load user data from the reddit api which is working. However, on mount it appears to be stuck in a rendering loop repeatedly making requests to the api. This causes it to error out and redirect to ‘/’. This is what I have below so far… running latest versions of phoenix and elixir 1.10.0. using Tesla for the api requests. i have also tried moving this logic to handle_params instead but does same thing. Any ideas why this is happening? I thought maybe it was making the call twice possibly since it connects to socket twice on initial mount but doesn’t seem to be it. Is it possible this might be due to the way I implemented my api requests?

Other weird thing is that temporary_assigns will not work either…i get an @saves not available in eex template error. I need this to work b/c plan is to append additional items to list w/ “load more” button using phx-update.

Any help appreciated. been digging at this all day and getting nowhere. Also, if there’s a better/cleaner way do this please let me know. a bit green.

def mount(params, _session, socket) do
      case Reddit.request_token(params["code"]) do
        "invalid_grant" ->
          {:ok,  
            socket
            |> put_flash(:error, "Token Invalid!")
            |> redirect(to: "/")
          }

        token ->
          {:ok, response} = Reddit.get_user_saves(token)
          {:ok, assign(socket, :saves, response.body)}
      end
end

Showing Posts 1 to 7

jhefreyzz

jhefreyzz

Hi, have you tried using connected? function since liveview is mounted twice.

def mount(params, _session, socket) do
  if connected?(socket) do
    case Reddit.request_token(params["code"]) do
     "invalid_grant" ->
       {:ok,  
         socket
         |> put_flash(:error, "Token Invalid!")
         |> redirect(to: "/")
       }

      token ->
       {:ok, response} = Reddit.get_user_saves(token)
       {:ok, assign(socket, :saves, response.body)}
    end
  end
end
the_dude

the_dude OP

I have but, still doesn’t work correctly.

Qqwy

Qqwy

TypeCheck Core Team

Could you maybe share what kind of logging messages are shown in the console that is running Phoenix while someone connects to it?

That would make it maybe easier to help with debugging. Even better would be if you could create a minimal example application that has the same problem, but of course that might take some more effort.

the_dude

the_dude OP

The logging is just reddit returning a json object w/ token: “invalid_grant” instead of the auth code. This is happening b/c the initial request for the token is sent w/ a one-time use code. so on the repeat request it’s returning “invalid grant” for the token.

behavior of the app is basically, initial request occurs and i see the data load on the page as it should and then within a second it redirects to my homepage based on the first case b/c it’s trying to request a token again w/ the expired code.

i am going to try this w/ another api that just needs api key and not oauth authorization and see if it causes the same issue or not.

I have implemented previously not using liveview and had no issues.

egze

egze

Your mount code will always run twice. See the docs about LiveView lifecycle. The advice about is_connected? was on the right track I think.

the_dude

the_dude OP

Tried running the reddit api code inside connected? again. it will run the request only once but it does not update the page w/ data from the request. IO.inspect confirms the request data being grabbed though.

I also built a quick liveview using another api(one that doesn’t require a key or auth and I do not have a problem at all. makes one request, loads the data on the page and that’s it. did not need to check if connected first or anything. HOWEVER, if i run this inside connected? conditional it does the same behavior as reddit api above. does not update page w/ data from request…

Looking at the docs it seems like all my request logic should be in handle_info and that would get triggered by the connected? conditional and then should update the page… Tried this too and technically it does update the data but, it also loops requests. maybe i am triggering it incorrectly though.

I’m wondering if reddit api request is doing weird stuff since it’s doing a token request then an auth/data request and then redirecting back to my uri. and this is maybe triggering renders? Maybe I need to bury the call for the token request inside the api wrapper?

the_dude

the_dude OP

Wanted to give an update on this:

I moved the case for the token validation into the api wrapper so there was only one call inside mount/3. Still caused the same problems. So I ended up just putting the api request in a regular controller then, called live_render/3 and passed my data through to the liveview w/ session%{}.

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews