Extracting a string out of JSON returned

I’m saving my auth tokens to local storage for persistence, but when I get it back, the item is returned as JSON and I have no idea how to stringify it or extract the string to store in appVar.auth which only accepts string. Moreso to get the currentUser, i need to authorize using the token to get back the owner of the token and it’s usually “bearer tokenkey” - a string.

Please someone familiar with this help out. Also new here, barely 36 hours in.

Thanks

You could try using a formula In a ‘Set App Variable’ logic element.

There are functions that will help you such as STRING - and depending on whether you need to extract smaller chunks of the data, SUBSTRING… - you can also create strings by joining other strings together using +

The descriptions in the formula editor are pretty helpful.

I hope that helps.

Thanks @Oli_Clark. TBH, I have tried every formula in the functions. There is no function for extensively manipulating or transforming JSON in function libraries. I already tried STRING, UTILITY, OBJECT, ARRAY, pretty much everything there.

So to reproduce my experience, search for ‘Set item to storage’ and ‘Get item from storage’ in the Flow Function Market and install. Then set a random string to storage with any Key of your choice. Next use the same Key to get the item from storage. When it returns, it’s no longer a string but a JSON type. You are not able to add it to say appVars.randomString which stores only string. See what I mean. Now there are no tools that allow you much like you would use schema to extract the string you want in the Data tool.

Finally, if anyone can point me to how I might persist a user token in Appgyver without banging my head against the wall will be very much appreciated.

Thanks

Not quite sure what is the problem here. If you have valid json in the storage you should be able to access any property with simple dot notation. e.g. appVar.auth.token or what ever its key is defined as.

if its stringified you can use ENCODE_JSON

Please elaborate if there’s something I’m missing here.

Hi @Sasu_Makinen, I am unable to ENCODE_JSON or apply dot notation.

Perhaps can you walk me through how you would store a token to a user’s browser and retrieve it back on page load into string appVars for use until user refreshes or closes app. I have struggled with this problem for the past 12 hours or so. If there is a better way, I’m pretty sure I’m way so off at this point.

Thanks

I have logic like this which returns the token string as it should to my page variable.
The Get item from storage could be in app launched event logic just as well.

If there’s some odd specific bug going on you could give your app id and consent for us to check it out in your app, if you want to.

Thanks @Sasu_Makinen. Please find my addId below: I have a flow setup on the Global Canvas. Thanks

appId: “97647”

I trust we had a great weekend, anyone able to help look through my app?

Can’t really reproduce the issue as I don’t have any authentication methods for your app. I would try to see what happens right after you call login in your log in page. Try to use debugger to see if outputs are what you expect, or just do an alert box with ENCODE_JSON(outputs['my auth flow'].response and see if there’s something wrong, also get the storage item right after setting it and see if it’s there. Looking at the logic I can’t find any clear issues.

Hello @Sasu_Makinen. Apologies for the delays in responding.

Please use the user to authenticate

email: user+3@gmail.com
password: pass

I implemented the same logic you have in your login canvas and an API that gives an exact response as the auth API you’re using with an example value.

I have no issues in saving or requiring my token. Are you using Web/ios/android? which model or browser? Any plugins or extensions that could have an effect?


In the setup presented in the video everything works as expected.
Is there any errors presented to you when running? In browser console or mobile logs?

Thanks again @Sasu_Makinen

I finally found where the issue was. I had the Get Data too early in my flow and for some reason I just never caught it. I placed it after set appVars.auth and it fixed it like magic.

Thanks for your patience and support

1 Like

Hi @Sasu_Makinen thanks for all the help.

Unfortunately i need more help. How do I set up so that if a user has a valid auth token, they skip the login page and if a user doesn’t have one is routed to the login page. I have checked the docs but can’t find anything relating to this.

Thanks

You set login page as the “initial page” in the navigation tab, and then at the login view, or global canvas, you check for the token in the storage and open a different page if its ok.