PricingBlog

Help with authentication and storing user information

  • benjamin.h-1275307192450289774

    Ben H

    1 year ago

    Hey folks, I’m trying to get some authentication related issues sorted. My auth works like this:

    1. I get the user access and refresh tokens from my backend (Fastgen) in a redirect URL that goes to my toddle “PostLogin” page.

    2. This PostLogin page runs a workflow on load that checks the url for an access token, and if it finds one, it stores the access token as a cookie.

    So far, so good—I can see the access_token in my cookies. Here's where the trouble begins:

    1. After logging in on the preview pages and getting the access token saved in my cookies, I still can’t make authenticated requests in the editor. I thought I should be able to do that because I have the latest toddle plugin installed (2.1). When I add the authorization in the header manually, the API requests work.

    2. Relatedly, the Get Cookie formula still returns Null in the editor after logging in on the preview page. Even in the preview pages themselves I can’t seem to display the access token using the Get Cookie formula.

    3. Without authorization in the editor, my APIs can’t fetch the data I need. Without this data, it makes it difficult to build formulas. Sometimes I know the path to get the info from the JSON, but I can’t click the path in the formula builder because the data is not populated. Is there a way to manually input a path (e.g., Fetch_user.data.Email) in these cases where I don’t have the data loaded in the editor?

    4. Despite those issues, I’m still successfully storing the access_token as a cookie in my workflow. That success triggers another workflow where I fetch the username and email from my backend and stores this data in local storage. While the fetch request is working, for some reason the user information is not being saved in local storage.

    5. Oddly, if I login a second time, the user information does get saved in local storage. So I have to login twice for my on success action to save the user data to local storage.

    Okay, that’s what I’ve been pulling my hair about! 😉
    1275307192752406628-page_load_workflow.png
    1275307193138151534-success_workflow.png
  • prettycold_-1275309802863460405

    Sam B

    1 year ago

    Hey Ben,

    Yesterday I opened a post as I was facing a similar issue. Andreas has shown how to troubleshoot session cookies. Here's my thread, goto the fag end to see his video.

    https://discord.com/channels/972416966683926538/1275049276837331048

    Fyi, I'm yet to watch and fix my issue. The moment I saw your post, I thought this will be of some help. Check it out!
    👍1
  • max.kayr-1275311009321717792

    Max

    1 year ago

    Did you refresh your editor after logging in on the preview page?
    Just to be sure, the preview page is the one that opens in a new tab.
    👍1
  • max.kayr-1275311275747836049

    Max

    1 year ago

    The Get Cookie action will always return null. The cookie that Toddle sets is a http only cookie. You cannot access it from the browser's javascript thread
  • lucasg-1275315730174443611

    Lucas G

    1 year ago

    It should work server side for those
    👍1
  • Though it probably won't work in the editor
  • max.kayr-1275316906923921409

    Max

    1 year ago

    Guess so, too. But haven't tried it yet
    👍1
  • benjamin.h-1275326311631683638

    Ben H

    1 year ago

    @Max Okay, refreshing the editor and now the auth is working. I swear I tried that before 😅 . And if the Get Cookie action returning null is expected behavior then that would explain why I can't display the access token.

    I guess now I just have to figure out why my user information is not getting stored in local storage during the first time my login workflow runs
  • max.kayr-1275328118776660040

    Max

    1 year ago

    Glad it is working 😊
    Can you share what exactly you are saving to local Storage? So the formula in the "Save to Local Storage"
  • benjamin.h-1275501164083023932

    Ben H

    1 year ago

    Here's a quick video which shows what I'm saving to local storage and also illustrates the problem. It's not a big deal I am just trying to figure what is going on for the sake of learning 🙂 https://www.loom.com/share/0df16f5371a640a4bc5fce329c742c1f?sid=98a72be4-0a39-4e0f-bfec-b4d0580deb21
  • max.kayr-1275507183634743390

    Max

    1 year ago

    Is your fetch user set to auto fetch? If yes, it will be executed on the server. When you login the first time, you don't have your access token, yet. On the second login, you are actually already logged in and the fetch works
    🙌1
  • max.kayr-1275507517325185139

    Max

    1 year ago

    If you want to do that in the right order, the fetch user has to happen in the success flow of the set cookie action. The save to local Storage has to happen in the success flow of the fetch action
    🙌1
  • max.kayr-1275507881743220797

    Max

    1 year ago

    Also, you should not bind the Get from local Storage directly to your output text field. This value is not reactive, meaning that it is not updated when the local Storage is updated.
    👍1
  • benjamin.h-1275569132686938193

    Ben H

    1 year ago

    That did the trick, thanks @Max
    💪1
  • Tod-1275569134373179493

    Tod

    1 year ago

    Great energy @Ben H! Your continuous contribution to the toddle Community just made you advance to Community Level 1!