CurrentUser not working from backend code??

Hi, I’m currently using a backend HTTP function to verify a user’s login credentials using wixUsersBackend.login(). From this same function I also want to retrieve data about that specific user’s subscriptions made with the ‘Pricing Plans’ wix app. I thought I would be able to do this by retrieving the currently logged in user with ‘currentUser’ and then using getPricingPlans() but after some testing both the user ID and the pricing plans return nothing.

After I use ‘wixUsersBackend’ to login the user this is the code I have:

let user = wixUsersBackend.currentUser;

 return user.getPricingPlans()
 .then( (pricingPlans) => {

The problem is no user seems to be getting retrieved at all. Does a user have to be on a literal webpage for this to work? Does it not work when using just a HTTP function? If not how would I go about checking a user’s payment plans after logging them in like so:

return wixUsersBackend.login(email, password)
.then( (sessionToken) => {

I have no idea where to go from here so any help would be greatly appreciated. Thanks.

The same issue on to me, but no one helps this issue, I was searched for many tutorials and post comments but no one solves this issue. Please the Wix team need to reply to this type of forum.

Not enough code to figure out what you’re doing. Please show the relevant code so we can assist.

Understand that this forum is not a support site. It is a community of Corvid developers and users where coding and development topics are discussed and information shared. There is no assurance that users will find their answers here.

Your issue may very well be different than that of the original poster. Please create your own post and explain what you are trying to do, what works, and what doesn’t. Also, add any code in a code block as stated in the Forum Guidelines .

I ran into the same thing a while ago. http-functions do not work together with wixUsersBackend. And this is, unfortunately, correct: http-functions basically are triggered from outside the Wix-environment, so the backend has no clue which user is it (there even might be several logged in: who to pick?). You have to solve this by sending some kind of id, key or whatever along with your http-request (header or URL). When you get a request, you should do some checking if this id/key is allowed and then do the rest of your logic. Hope this helps.

Ok so here is the relevant code:

return wixUsersBackend.login(email, password)
.then( (sessionToken) => {
	//Our user is logged in
		
	let user = wixUsersBackend.currentUser;

	return user.getPricingPlans()
	.then( (pricingPlans) => {
		let plan = pricingPlans[0];
		let expiryDate = plan.expiryDate;
		let currentDate = new Date();

		if (plan !== undefined && expiryDate >= currentDate) {
			//Subscription is still valid.

			options.body = {
				"msg": "Client is valid."
			};

			return ok(options);
		}else {
			//Subscription not valid

			options.body = {
				"error": "User doesn't have a subscription plan."
			};
				
			return forbidden(options);
		}
	})
	.catch( (plansError) => {
		//This below code should run when published.
		options.body = {
			"error": "User has no plans/couldn't retrieve user plans."
		};

		return serverError(options);
	});
})
.catch( (err) => {
	//Error logging user in
	options.body = {
		"error": "Invalid login credentials."
	};
		
	return forbidden(options);
});

After verifying the user’s credentials by using login() I need a way to check if they have a payment plan, which will return the “Client is valid.” message. As someone else also mentioned it seems wixUsersBackend doesn’t work with http-functions so I ideally need another way of retrieving this data. If all else fails I was thinking of using a database to store the information about a user’s plan’s expiry date by using the onPlanPurchased event but this could end up quite messy as removing and updating the database would have to be initiated client-side (eg: when a user cancels a plan) so ideally I would want to avoid this and be able to retrieve this information through a HTTP function call. Any ideas?

@jacobhothersall It’s not possible to access the wixUsersBackend API from the HTTP functions file.

@yisrael-wix Yes I’ve realised now. Is there any other way of retrieving this without using wixUsers? Or would creating a separate database to query like I detailed above be the best bet?

Thanks for the suggestion, the issue is that I can’t really use a key to validate a user since proving a user’s identity isn’t the issue. I can already prove a user is authorised to use the code by using the login() function. What is stumping me is that I need to check the payment plans for that user specifically, depending on whether the user has a subscription to the site will return different things from the function. I have detailed the code and a better explanation to my response to Yisrael above. I’m thinking I might have to end up using a separate database to query but I would ideally want to avoid this as it could cause issues as I’ve also explained there. If you have any other ideas I could try it would be very much appreciated.

@yisrael-wix Any other option to login or register the record from http-function. Please help me, i need to develop the mobile app using Wix API.

@info29115 If I don’t get any other suggestions I’m thinking of just using a separate database that the backend will have access to. When it comes to things like this we might just have to create our own finnicky solutions.