Hello! I really enjoyed using wappler to start, but there’s something I cannot figure out and hoping someone here can help
We have built our own API and authentication service. I am using wappler to build the front end of the application. Right now, when someone logs in, we send an API request to an endpoint, which returns a jwt access token and refresh token. I am attempting to decode the token that I receive using wappler server actions, so that I can determine if someone is logged in or not.
Currently, I am using an API form to send to the endpoint, and then set a global session variable to store the token I receive.
On the next page (called /dashboard) I am redirected to, I use a server connect component to call my server action. the server action reads the global session variable, then in my EXEcute commands, I use jwt decode to read my session variable.
On the /dashboard page, after the server connect component finishes running (it is the first component I call for reference), I would expect to be able to parse any of the data from the server connect component that decoded the jwt token. For testing purposes, in my markup I wrote {{serverconnect1.data}} and get [object Object] to print out, but there is nothing more I can do with this. I tried the parseJSON() and toJSON() formatters, I’ve tried to drill down to the next data element that I see on jwt.io (like {{serverconnect1.data.sub}}), but nothing seems to help me understand what’s inside of this object so that I can move to my next step of using the data for verification.
I’m happy to post screenshots and code, but wonder if there’s something I’m missing easily… It’s very possible!
But the server action isn’t picking that up at all. It’s returning undefined (I figured this out by adding a Set Cookie action to my server action and trying to set the value to $_SESSION.token, and it returned undefined).
Session values aren’t exposed like that on the browser console, session values are stored server-side and the browser only has a cookie “session” with some ID to identify the session
And here, there is no cookie with anything named session or token. The all capitals _TOKEN was a test I was running previously that I’ve deleted from my server action.
I think I understand what’s going on here, but I lack the front-end experience to give you a clear direction (I’ll try anyway).
It seems you’re using “session storage” from HTML5 Web Storage, which is different from “session storage” of Wappler’s backend, and this is why you can’t access those variables on your server action.
So, two different things with the same name…
Easiest way I imagine to solve this is, when you get the token, you send it as a $_GET or $_POST parameter to a server action, which in turn uses the Set Session step to save $_SESSION.token
Now I have that set, I go to the page I want to run the server action to decode. This is a content page… not sure if that matters. It happens in serverconnect2. I even included an input parameter of test with the session1.data.token to make sure I have something to $_GET as a further test.
I use the values of $_COOKIE._TOKEN to set a new cookie tokentest for testing. I also set another cookie with my $_GET value from input parameter called get_test. Both get set by the browser, no problem! This is good:
However, now my final step in the server action, the JWT decode step is still undefined for some reason, even though I know I am feeding it a proper token. Here I am calling serverconnect2.data.identity:
@Apple, your help moved me forward to make sure I was actually sending a real value in my server action step! Thank you!! Maybe there is still something I’m missing with this decode step though…
But jit is still returning nothing unfortunately. I definitely need to decode the response I am getting, but not sure how to do that properly in a server action…
Ok, please go to the network tab of your browser developer tools, and find there the request made by serverconnect2, and look at the response, to see the fields