I am adding some JS script to allow logged in users to upload a file to a server. I would like this file to be associated to a user id, hence would like to send the user_id in the form.
Is that possible? If not, how could I do that in webflow (with Javascript or whatever way)?
Itās not possible yet, but the Webfow Memberships team is aware of the user need. Iād encourage you to post this in the Memberships BETA forum on circle.so, so that it stays top of mind.
If youāve forgotten the link, check your original welcome to the BETA emails.
Memberstack has the ability to access its member ID from script.
Currently any solution based on Webflow Memberships will require a very convoluted process to capture that ID and expose it so that you can use it client-side.
Thatās very strange as some kind of user information should be there, specially if you use the CMS.
By inspecting the code with chrome I can see the webflow frontend API⦠but am still not able to expose the user id or some user information (except that the user is logged inā¦)
You should put Javascript aside for this as Iām assuming youāre speaking to Webflow Memberships when you say āallow logged in users toā¦ā.
Nothing convoluted about it, but youāll have to glue some stuff together (nothing exists out of the box).
Hereās a fairly common approach:
Do your form upload from a CMS Collection Page that has access to the users email address, or id, or something unique to them. Include that in a hidden field.
To get that unique something into a CMS item, create a Webflow Webhook that triggers on āmembership account addedā.
Thatās it.
When a new Webflow Memberships is addedā¦
This sends the email or something unique into your CMS.
You then grab that from your CMS Collection Pageā¦
And add it as a hidden field in your file upload form.
At present the MembershipID is only exposed server-side, so you need to make that accessible client-side. For you this is the most efficient approach;
Setup a Webflow webhook to fire when a new user is created. Connect that to a webhook URL on your favorite automation platform. When a new user is created, at that moment, in that webhook event, youāll have the userās ID.
Have your server side automation turn around and immediately update that same Member record, pushing that user_id into a custom Member field youāve already created. Now, that ID is accessible on the /user-account page as a custom field.
On the User Account page, add that field so itās accessible in the HTML, and then wrap it in a hidden DIV. You want Webflow to populate it, but no need for users to see their userID. You especially donāt want them to edit it. Add an HTML ID to this input element as well, like user_id so that you can reference it easily.
Now for the most awkward part; you need to write a script that will access that data.
User custom fields are currently only available on the /user-account page, which means your script has to get it from there. Your script will;
Verify the user is logged in ( check for the wf logged in cookie )
Create an IFRAME, which loads /user-account. Make sure to use a relative path, so the hostname passes the same-domain CORS test. Webflow membership sites and e-commerce sites specify self as an IFRAME ancestor restriction.
Wait roughly 3 seconds⦠this is the tricky part, the population scripts are slow and they set the value of the input. As such there is no possibility to trigger via a mutation observer. You basically have to poll it every 200ms and wait for some value to be returned.
Once itās there, grab that value.
You will want to cache that ID but thatās another layer of complexity, because you have to know when to clear it, and there is no logout page. I store it in sessionStorage ( not localStorage ), and clear it any time the user visits the /log-in page, or any time the login cookie test returns false.
Hey Chris and Michael, thanks both for the clever answers! I understands what you suggest, although is not straightforward. It makes me think that maybe changing the log in system to Firebase is possibly easier. Will work further on the topic. Thanks again!
If you utilize Firebase, you will still need to set conditional Logic. (Wized/Xano)
Xano can take user id and Firebase Auth and has a āhookedā Webflow connect. Wized will generate āsecureā user ID. If you utilize Webflowās Subscription ID to reference a status collection and then use Filters to Show Hide User lists in Template, you should be able to manage custom User Attributes in Collections with real time tagging.
Iāve been trying to do that (access memberstack data using JS) for days but havenāt been able to figure it out yetā¦
Could you pretty please help explain what Iām doing wrong?
Iāve put the following code in the site header: <script defer data-memberstack-app="app_(my-app-id)" src="https://static.memberstack.com/scripts/v1/memberstack.js" type="text/javascript"></script>
Put the following code in the page custom code area:
I havenāt worked with Memberstack at all, it wasnāt suitable for any specific client projects yet, which either required tighter security or lower cost.
But glancing at the docs, they donāt indicate the defer in your script element.
That may be causing issues for you if window.$memberstackDom is not defined at the point you are trying to access it.
If thatās not the problem, your best bet is probably Memberstackās own forums.
I put that line of code in to initiate the Memberstack SDK (at least thatās what chatGPT said I should do) is there another way to initiate it or have I already done that with the line of code above?
Anyway I removed that code and now only get errors whenever I use memberstack-specific code such as if (MemberStack.isAuthenticated()) {...
which makes me think I havenāt loaded the SDK properly.
Also, do you think having the Memberstack App ID visible is a security concern?